2018-03-03
http://codeforces.com/problemset/problem/939/C
In distant future on Earth day lasts for n hours and that's why there are n timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to n are used, i.e. there is no time "0 hours", instead of it "n hours" is used. When local time in the 1-st timezone is 1 hour, local time in the i-th timezone is i hours.
Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are ai people from i-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than s hours 00 minutes local time and ends not later than f hours 00 minutes local time. Values s and f are equal for all time zones. If the contest starts at f hours 00 minutes local time, the person won't participate in it.
Help platform select such an hour, that the number of people who will participate in the contest is maximum.
The first line contains a single integer n (2 ≤ n ≤ 100 000) — the number of hours in day.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 10 000), where ai is the number of people in the i-th timezone who want to participate in the contest.
The third line contains two space-separated integers s and f (1 ≤ s < f ≤ n).
Output a single integer — the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them.
3
1 2 3
1 3
3
5
1 2 3 4 1
1 3
4
In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate.
In second example only people from the third and the fourth timezones will participate.
题意:在未来,一天一共有n个小时,也一共有n个时区,每两个相邻的时区之间差是一个小时。现在有一些网站想要举办在线编程比赛,希望有最多的人能参与。因为人们以自己的时区的时间为标准,只参加在s点以后(包括s点)开始的且在f点之前(包括f点)的比赛。每场比赛的时间为1个小时,让你求出比赛在什么时间开始会有最多的人参加,输出以第一个时区的时间为标准的开始时间。
感想:错了好几次,主要问题在 数据类型不对导致的RE、TLE以及一个坑点:没考虑到可以是后面的时区可以比开头的时区要早一点开始比赛。其他还有一些问题在cf提交的地方看。关于TLE的反思,以后做题先计算复杂度再做题,不要想当然。有一些循环是可以变化为等式降低复杂度的,就不用写得跟之前一样麻烦了...
Code
#include<string.h> #include<cmath> #include<cstdio> #include<algorithm> #include<iostream> #include<vector> #include<queue> using namespace std; #define MAX 0x3f3f3f3f #define fi first #define se second #define LL long long int a[2000100]; int main() { int n,s,f; int ans; /*int ans,p1,p2,p3,p4;*/ while(cin>>n) { for(int i=1;i<=n;i++) cin>>a[i]; cin>>s>>f; int t=f-s; LL maxn=0; // for(int i=1;i<=t;i++) maxn+=a[i]; /*int flag=1;*/ LL cc=maxn; // /*p2=s*/; ans=s; //这个地方又是一个坑点,错在样例28 for(int i=2;i<=n-t+1;i++) { cc=cc-a[i-1]+a[i+t-1]; if(cc>maxn) { maxn=cc; /*flag=i; p1=(flag-1)%n; p2=s; while(p1--) { if(p2==1) p2=n; else p2--; } ans=p2;*/ ans=s-i+1; if(ans<=0) ans+=n; } else if(cc==maxn) { /*p3=(i-1)%n; p4=s; while(p3--) { if(p4==1) p4=n; else p4--; } if(p4<p2) {ans=p4;p2=p4;}*/ int tmp=s-i+1; if(tmp<=0) tmp+=n; //其实修正的时候 加n减n结合前面的等式是等价的 ans=min(ans,tmp); } } int q=1; for(int i=n+1;i<=n+t-1;i++) { a[i]=a[q++]; } for(int i=n-t+2;i<=n+t-1;i++) { cc=cc-a[i-1]+a[i+t-1]; if(cc>maxn) { maxn=cc; /* flag=i; p1=(n+1)-flag; p2=s; while(p1--) { if(p2==n) p2=1; else p2++; } ans=p2;*/ ans=s+n-i+1; if(ans>n) ans-=n; } else if(cc==maxn) { /*flag=i; p3=n+1-flag; p4=s; while(p3--) { if(p4==n) p4=1; else p4++; } if(p4<p2) {ans=p4;p4=p2;} */ int tmp=s+n-i+1; if(tmp>n) tmp-=n; if(tmp<ans) ans=tmp; } } cout<<ans<<endl; } }
其他人的写法