Codeforces Round #464 (Div. 2) C Convenient For Everybody

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lfhase/article/details/79333541

题目链接:点击打开链接

题意:一天n个小时,全球n个时区,当第一个时区当地时间为1时,第i个时区当地时间为i。现在要举办某种比赛,历时1小时,每个时区有a_i个人参加。给定s和f,如果对于某个时区i,(均为当地时间)比赛开始时间不早于s且结束时间不迟于f则当地选手都会参加这个比赛。现在要求某个对第1时区的时间,使得比赛在这个时间开始时全球参加的人数最多。

思路:时区换算比较麻烦,比如n=5时,时间的推移为(1,2,3,4,5)->(2,3,4,5,1)->(3,4,5,1,2)->(4,5,1,2,3)->(5,1,2,3,4)->(1,2,3,4,5)。对于时区i来说,若当地人能参加比赛,则对应的最大值为从a_i开始的f-s个数的和。使用滚动数组的方法从1到n遍历,每次删去尾加上头,求得对应的最大值。若在时区i求得最大值,则对时区1而言时间为n+2-i。

AC代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <vector>

using namespace std;

#define FSIO  ios::sync_with_stdio(0);cin.tie(0);
#define DEBUG(a)   cout<<"DEBUG: "<<(a)<<endl;

int cnt[1000005];
int a[1000005];
int n;

int main()
{
    int s, f;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;++i)   scanf("%d",&a[i]);
        scanf("%d %d",&s,&f);
        memset(cnt,0,sizeof(cnt));
        int maxn = 0;
        int res = 1;
        for(int j=s-1;j<f-1;++j)
        {
            if(1+j>n)   maxn += a[1+j-n];
            else   maxn += a[1+j];
        }
        int temp = maxn;
        for(int i=2;i<=n;++i)
        {
            if(i-2+s>n) temp-=a[i-2+s-n];
            else    temp-=a[i-2+s];
            if(i+f-2>n) temp+=a[i+f-2-n];
            else    temp+=a[i+f-2];
            if(temp>=maxn)
            {
                maxn = temp;
                res = n+2-i;
            }
        }
        printf("%d\n",res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Lfhase/article/details/79333541