hdoj4648(Magic Pen 6)(前缀和)
生活随笔
收集整理的這篇文章主要介紹了
hdoj4648(Magic Pen 6)(前缀和)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4648
Total Submission(s): 2565????Accepted Submission(s): 913
Problem Description In HIT, many people have a magic pen. Lilu0355 has a magic pen, darkgt has a magic pen, discover has a magic pen. Recently, Timer also got a magic pen from seniors.
At the end of this term, teacher gives Timer a job to deliver the list of N students who fail the course to dean's office. Most of these students are Timer's friends, and Timer doesn't want to see them fail the course. So, Timer decides to use his magic pen to scratch out consecutive names as much as possible. However, teacher has already calculated the sum of all students' scores module M. Then in order not to let the teacher find anything strange, Timer should keep the sum of the rest of students' scores module M the same.
Plans can never keep pace with changes, Timer is too busy to do this job. Therefore, he turns to you. He needs you to program to "save" these students as much as possible.
Input There are multiple test cases.
The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a1,a2,...an (-100000000 <= a1,a2,...an <= 100000000) denoting the score of each student.(Strange score? Yes, in great HIT, everything is possible)
Output For each test case, output the largest number of students you can scratch out.
Sample Input2 3 1 6 3 3 2 3 6 2 5 1 3
Sample Output1 2 0 Hint The magic pen can be used only once to scratch out consecutive students.
思路:大致是https://www.2cto.com/kf/201308/234446.html這位博主的思路,先把前綴和求出來,再枚舉0~M-1的模數,a[i~j]=sum[j]-sum[i], 求出最大區間長度. 見代碼: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int sum[100010]; int Hash1[10010]; int Hash2[10010]; int main() {int n,m,t;while(scanf("%d%d",&n,&m)!=EOF){memset(sum,0,sizeof(sum));memset(Hash1,-1,sizeof(Hash1));memset(Hash2,-1,sizeof(Hash2));for(int i=1;i<=n;i++){scanf("%d",&t);sum[i]=(sum[i-1]+t)%m;if(sum[i]<0)sum[i]=(sum[i]+m)%m;Hash1[0]=0;if(Hash1[sum[i]]==-1){Hash1[sum[i]]=i; }else{Hash2[sum[i]]=i; }}int len=0;for(int i=0;i<=m-1;i++){if(Hash2[i]==-1)continue;else{len=max(Hash2[i]-Hash1[i],len);}}printf("%d\n",len);}return 0; }
Magic Pen 6
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2565????Accepted Submission(s): 913
Problem Description In HIT, many people have a magic pen. Lilu0355 has a magic pen, darkgt has a magic pen, discover has a magic pen. Recently, Timer also got a magic pen from seniors.
At the end of this term, teacher gives Timer a job to deliver the list of N students who fail the course to dean's office. Most of these students are Timer's friends, and Timer doesn't want to see them fail the course. So, Timer decides to use his magic pen to scratch out consecutive names as much as possible. However, teacher has already calculated the sum of all students' scores module M. Then in order not to let the teacher find anything strange, Timer should keep the sum of the rest of students' scores module M the same.
Plans can never keep pace with changes, Timer is too busy to do this job. Therefore, he turns to you. He needs you to program to "save" these students as much as possible.
Input There are multiple test cases.
The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a1,a2,...an (-100000000 <= a1,a2,...an <= 100000000) denoting the score of each student.(Strange score? Yes, in great HIT, everything is possible)
Output For each test case, output the largest number of students you can scratch out.
Sample Input2 3 1 6 3 3 2 3 6 2 5 1 3
Sample Output1 2 0 Hint The magic pen can be used only once to scratch out consecutive students.
思路:大致是https://www.2cto.com/kf/201308/234446.html這位博主的思路,先把前綴和求出來,再枚舉0~M-1的模數,a[i~j]=sum[j]-sum[i], 求出最大區間長度. 見代碼: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int sum[100010]; int Hash1[10010]; int Hash2[10010]; int main() {int n,m,t;while(scanf("%d%d",&n,&m)!=EOF){memset(sum,0,sizeof(sum));memset(Hash1,-1,sizeof(Hash1));memset(Hash2,-1,sizeof(Hash2));for(int i=1;i<=n;i++){scanf("%d",&t);sum[i]=(sum[i-1]+t)%m;if(sum[i]<0)sum[i]=(sum[i]+m)%m;Hash1[0]=0;if(Hash1[sum[i]]==-1){Hash1[sum[i]]=i; }else{Hash2[sum[i]]=i; }}int len=0;for(int i=0;i<=m-1;i++){if(Hash2[i]==-1)continue;else{len=max(Hash2[i]-Hash1[i],len);}}printf("%d\n",len);}return 0; }
總結
以上是生活随笔為你收集整理的hdoj4648(Magic Pen 6)(前缀和)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux中构建支持lighttpd +
- 下一篇: 变异检测:vcf文件合并