codeforces1028E: Restore Array

小清新构造题。

选择一个位置$b_{i-1}<b_i且b_i=max_b$,然后把这个位置循环位移到$n$。

然后构造$a_n=b_n$。

$a_i=\sum_{j=i}^{n-1}b_j+2*b_n$。

带回去检验一下,发现是对的。

思路大概就是往$a_i-a_{i+1}=b_i$的方向想吧。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define M 150000
 4 #define LL long long
 5 #define Next(x) x % n + 1
 6 #define pre(x) (x == 1 ? n : x - 1)
 7 int b[M], c[M];
 8 LL a[M];
 9 int n;
10 int main() {
11     scanf("%d", &n);
12     for(int i = 1; i <= n; ++ i) {
13         scanf("%d", &b[i]);
14     }
15     b[0] = -1;
16     int mx = 0;
17     for(int i = 1; i <= n; ++ i) {
18         if(b[i] > b[mx] && b[pre(i)] < b[i]) {
19             mx = i;
20         }
21     }
22     if(mx == 0) {
23         if(b[1] != 0) puts("NO");
24         else {
25             puts("YES");
26             for(int i = 1; i <= n; ++ i) {
27                 printf("1 ");
28             }
29             puts("");
30         }
31         return 0;
32     }
33     int st;
34     mx = Next(mx);
35     for(int i = 1; i <= n; ++ i, mx = Next(mx)) {
36         if(mx == 1) st = i;
37         c[i] = b[mx];
38     }
39     a[n] = c[n];
40     a[n - 1] = 2 * c[n] + c[n - 1];
41     for(int i = n - 2; i >= 1; -- i) {
42         a[i] = a[i + 1] + c[i];
43     }
44     puts("YES");
45     for(int i = st, j = 1; j <= n; ++ j, i = Next(i)) {
46         printf("%lld ", a[i]);
47     }
48 }

 

转载于:https://www.cnblogs.com/iamqzh233/p/9556714.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值