Codeforces 847 H. Load Testing (技巧)
2017-09-30 16:17
302 查看
Description
Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send ai requests.Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.
Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.
For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.
Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.
Input
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 10^9), where ai is the number of requests from friends in the i-th minute of the load testing.
Output
Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.Examples input
5 1 4 3 2 5
Examples output
6
题意
给出一个数组,现在我们想让它前半部分递增,后半部分递减,问需要改变数的大小之和。思路
统计原数组距离最小递增序列的前缀距离与原数组距离最小递减序列的后缀距离。然后从左到右枚举一遍即可。
AC 代码
#include <bits/stdc++.h> using namespace std; typedef __int64 LL; const int maxn = 1e5+10; LL a[maxn],b[maxn]; LL c[maxn],d[maxn]; LL e[maxn],f[maxn]; LL n; int main() { cin>>n; for(int i=0; i<n; i++) cin>>a[i]; LL lmaxf = a[0]; e[0] = lmaxf; for(int i=1; i<n; i++) { c[i] = c[i-1]; if(a[i]<=lmaxf) { c[i] += lmaxf - a[i] + 1; lmaxf += 1; } else lmaxf = max(lmaxf,a[i]); e[i] = lmaxf; } lmaxf = a[n-1]; f[n-1] = lmaxf; for(int i=n-2; i>=0; i--) { d[i] = d[i+1]; if(a[i]<=lmaxf) { d[i] += lmaxf - a[i] + 1; lmaxf += 1; } else lmaxf = max(lmaxf,a[i]); f[i] = lmaxf; } LL ans = LLONG_MAX; for(int i=0; i<n; i++) { LL ci = max(e[i],f[i]) - e[i]; ans = min(ans,c[i]+d[i+1]+ci); } cout<<ans<<endl; return 0; }
相关文章推荐
- Codeforces 847 C. Sum of Nestings (技巧)
- Codeforces 653A: Bear and Three Balls(技巧,水)
- E - Toda 2 CodeForces - 730A(简单思维和处理技巧)
- CodeForces - 659D Bicycle Race (数学几何&技巧转换)
- C - Load Testing CodeForces - 847H
- CodeForces 552C:Vanya and Scales【技巧】
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter DP+技巧优化
- codeforces 348 C. Subset Sums (暴力+技巧)
- codeforces 704A Thor [技巧吧.]
- CodeForces 589B -- B. Layer Cake(多重集合+技巧枚举)
- CodeForces - 366D Dima and Trap Graph (并查集&技巧)好题
- F - Load Testing CodeForces - 847H 前缀后缀和
- Codeforces 110B-Lucky String(技巧)
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- CodeForces - 552C Vanya and Scales (进制转换&技巧)好题
- codeForces 198B.Jumping on Walls(DFS+技巧)
- Codeforces 599B: Spongebob and Joke(技巧,规律)
- codeforces-869B(模拟&技巧)
- codeforces 279C Ladder(数据技巧)
- CodeForces 597A(公式+技巧)