Codeforces 1028E. Restore Array

题目直通车:http://codeforces.com/problemset/problem/1028/E

解法:设原数组为ar[],求ar中的最大值的下标ins,依次向前遍历一遍,每一个答案值都为前一个答案值+余值(ar[i]),这样可以达到题意的要求

输出为NO的情况就是所有ar元素相等且不为0

麻烦的地方在于0值得特判,代码中有说明

 

 

#include<iostream>
#include<cstdio> 
#include<cmath>
#include<queue>
#include<vector>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<climits>
#include<bitset>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("input.in", "r", stdin);freopen("output.in", "w", stdout);
#define left asfdasdasdfasdfsdfasfsdfasfdas1
#define tan asfdasdasdfasdfasfdfasfsdfasfdas
typedef long long ll;
typedef unsigned int un;
const int desll[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
const ll mod=1e9+7;
const int maxn=4e5+7;
const int maxm=1e6+7;
const double eps=1e-4;
int m,n;
int ar[maxn];
ll ansl[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d",&ar[i]);
    int ins=0,mx=0;
    for(int i=0;i<n;i++){
        if(mx<ar[i]){
            mx=ar[i];
            ins=i;
        }
    }
    int l=0,r=n-1;
    while(l<n && ar[l]==mx)l++;
    while(r>=0 && ar[r]==mx)r--;
    if(l==n){
        if(ar[0]>0){
            printf("NO\n");
            return 0;
        }
    }
    if(l>0 && r<n-1){
        ins=r+1;
    }
    int i=ins;
    ins=0;
    ansl[i]=ar[i];
    if(mx==0 && l==n)ansl[i]=1;
    while(++ins<n){
        int j;
        if(i==0)j=n-1;
        else j=i-1;
        ansl[j]=ansl[i]+ar[j];
        if(ar[j]==0 && ansl[j]==mx)ansl[j]*=2;//特殊处理0
        i=j;
    }
    if((ar[i]==0 && ansl[i]==ansl[(i+1)%n]) || ansl[i]%ansl[(i+1)%n]==ar[i]){//前一部分特殊处理0
        printf("YES\n");
        for(int i=0;i<n;i++){
            printf("%I64d%c",ansl[i],i!=n-1?' ':'\n');
        }
    }
    else printf("NO\n");


    return 0;
}

 

posted @ 2018-08-29 21:55  wa007  阅读(455)  评论(0编辑  收藏  举报