hdu 6205 card card card
题意:长度为n的数列A和B ,可看作一个环 ,例如 1 2 3 4移动2次 可变为 3 4 1 2,问最少移动次数,保证(a[i]-b[i] + ....a[j]-b[j] >0 && max(a[i]+....a[j]) )
思路:尺取
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=1e6+10; 5 6 int a[N*2]; 7 int b[N*2]; 8 int c[N*2]; 9 10 int main(){ 11 int n; 12 while(~scanf("%d",&n)){ 13 for(int i=1;i<=n;i++){ 14 scanf("%d",&a[i]); 15 a[i+n]=a[i]; 16 c[i]=c[i-1]+a[i]; 17 18 } 19 for(int i=n+1;i<=2*n;i++){ 20 c[i]=c[i-1]+a[i-n]; 21 } 22 for(int i=1;i<=n;i++){ 23 scanf("%d",&b[i]); 24 b[i+n]=b[i]; 25 } 26 27 int sum=0; 28 int l=1; 29 int Max=0,s=n+1; 30 for(int i=1;i<=2*n;i++){ 31 sum+=a[i]-b[i]; 32 while(sum<0&&l<=i){ 33 sum-=(a[l]-b[l]); 34 l++; 35 } 36 // if(i==9) cout<<l<<" "<<sum<<endl; 37 if(c[i]-c[l-1]>Max){ 38 Max=c[i]-c[l-1]; 39 s=l; 40 } 41 else if(c[i]-c[l-1]==Max)
42 s=min(s,l); 43 } 44 cout<<s-1<<endl; 45 } 46 } 47 /* 48 5 49 4 6 2 8 4 50 1 5 7 9 2 51 */