Magic Pen 6
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 840 Accepted Submission(s): 304
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.
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 Input
2 3
1 6
3 3
2 3 6
2 5
1 3
Sample Output
1
2
0
View Code
Hint
The magic pen can be used only once to scratch out consecutive students.
参考别人代码先贴这了
n^2的做法还是可以优化的
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <cassert> 11 #include <set> 12 #include <sstream> 13 #include <map> 14 using namespace std ; 15 #ifdef DeBUG 16 #define bug assert 17 #else 18 #define bug // 19 #endif 20 #define zero {0} 21 #define INF 2000000000 22 #define eps 1e-6 23 const int V=100000+50; 24 const int MaxN=80+5; 25 const int mod=10000+7; 26 const __int64 INf = 0x7FFFFFFFFFFFFFFFLL; 27 const int inf = 0x7fffffff; 28 int n,m,sum[V],ans; 29 int main() 30 { 31 #ifdef DeBUG 32 freopen("C:\Users\Sky\Desktop\1.in","r",stdin); 33 #endif 34 35 int i,j; 36 while(scanf("%d%d",&n,&m)+1) 37 { 38 ans=0; 39 for(i=1;i<=n;i++) 40 { 41 int temp; 42 scanf("%d",&temp); 43 sum[i]=(sum[i-1]+temp)%m; 44 } 45 for(i=n;i>=1&&i>ans;i--) 46 for(j=1;j+i-1<=n;j++) 47 { 48 if((sum[j+i-1]-sum[j-1])%m==0) 49 { 50 ans=max(ans,i); 51 break; 52 } 53 } 54 printf("%d ",ans); 55 } 56 return 0; 57 }