CF 159 div2 d

a[i] <= a[i+1] <= 2*a[i] 可得,0 <= a[n] - a[n-1] <= a[n-1],这样记a[n]-a[n-1] 为tem,
则 若tem-a[n-2] >= 0,则令tem = tem - a[n-2],此时 0 <= tem <= a[n-2](a[n-1] - a[n-2] <= a[n-2]),
若 tem-a[n-2] < 0 ,则令tem = a[n-2] - tem,此时 0 <= tem <= a[n-2],这样不停的维护使 0 <= tem <= a[i]
,最后直到a[1] 都满足,解就构造出来了。 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 5;
const int INF = 1000000000;

LL a[maxn];
int ans[maxn];
int tag[maxn];

int main(){
    int n;
    while(cin >> n){
        for(int i = 0;i < n;i++) cin >> a[i];
        LL tem = a[n-1] - a[n-2];
        ans[n-1] = 1;
        ans[n-2] = 0;
        memset(tag,0,sizeof(tag));
        for(int i = n - 3;i >= 0;i--){
            if(tem - a[i] >= 0){
                ans[i] = 0;
                tem = tem - a[i];
            }
            else{
                ans[i] = 1;
                tag[i] = 1;
                tem = a[i] - tem;
            }
        }
        int cnt = 0;
        for(int i = 0;i < n;i++){
            if((ans[i]+cnt)%2 == 1) cout << '+';
            else cout << '-';
            cnt += tag[i];
        }
        cout << endl;
    }
    return 0;
}


 

你可能感兴趣的:(CF 159 div2 d)