HDU-4648 Magic Pen 6 简单题

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4648

  求遍前缀和,然后扫描标记下就可以了。。。

  1 //STATUS:C++_AC_453MS_1792KB
  2 #include <functional>
  3 #include <algorithm>
  4 #include <iostream>
  5 //#include <ext/rope>
  6 #include <fstream>
  7 #include <sstream>
  8 #include <iomanip>
  9 #include <numeric>
 10 #include <cstring>
 11 #include <cassert>
 12 #include <cstdio>
 13 #include <string>
 14 #include <vector>
 15 #include <bitset>
 16 #include <queue>
 17 #include <stack>
 18 #include <cmath>
 19 #include <ctime>
 20 #include <list>
 21 #include <set>
 22 #include <map>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,102400000")
 25 //using namespace __gnu_cxx;
 26 //define
 27 #define pii pair<int,int>
 28 #define mem(a,b) memset(a,b,sizeof(a))
 29 #define lson l,mid,rt<<1
 30 #define rson mid+1,r,rt<<1|1
 31 #define PI acos(-1.0)
 32 //typedef
 33 typedef __int64 LL;
 34 typedef unsigned __int64 ULL;
 35 //const
 36 const int N=100010;
 37 const int INF=0x3f3f3f3f;
 38 const int MOD=10007,STA=8000010;
 39 const LL LNF=1LL<<55;
 40 const double EPS=1e-9;
 41 const double OO=1e30;
 42 const int dx[4]={-1,0,1,0};
 43 const int dy[4]={0,1,0,-1};
 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 45 //Daily Use ...
 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
 50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 56 //End
 57 
 58 struct Node{
 59     int s,id;
 60     bool operator < (const Node& a)const {
 61         return s!=a.s?s<a.s:id<a.id;
 62     }
 63 }st[N];
 64 int num[N],sum[N];
 65 int n,m;
 66 
 67 int binary(int l,int r,int tar)
 68 {
 69     int mid;
 70     while(l<r){
 71         mid=(l+r)>>1;
 72         if(st[mid].s<=tar)l=mid+1;
 73         else r=mid;
 74     }
 75     return l;
 76 }
 77 
 78 int main(){
 79  //   freopen("in.txt","r",stdin);
 80     int i,j,ans,w;
 81     while(~scanf("%d%d",&n,&m))
 82     {
 83         sum[0]=0;
 84         for(i=1;i<=n;i++){
 85             scanf("%d",&num[i]);
 86             sum[i]=((sum[i-1]+num[i])%m+m)%m;
 87             st[i].s=sum[i];
 88             st[i].id=i;
 89         }
 90         sort(st+1,st+n+1);
 91         ans=0;
 92         for(i=0;i<n && ans<n-i;i++){
 93             w=binary(1,n+1,sum[i]);
 94             if(st[w-1].s==sum[i] &&  st[w-1].id>i)ans=Max(ans,st[w-1].id-i);
 95         }
 96 
 97         printf("%d\n",ans);
 98     }
 99     return 0;
100 }

 

posted @ 2013-08-07 01:04  zhsl  阅读(283)  评论(0编辑  收藏  举报