Codeforces Round #783 (Div. 2) C题
You are given an array aa consisting of nn positive integers, and an array bb, with length nn. Initially bi=0bi=0 for each 1≤i≤n1≤i≤n.
In one move you can choose an integer ii (1≤i≤n1≤i≤n), and add aiai to bibi or subtract aiai from bibi. What is the minimum number of moves needed to make bb increasing (that is, every element is strictly greater than every element before it)?
Input
The first line contains a single integer nn (2≤n≤50002≤n≤5000).
The second line contains nn integers, a1a1, a2a2, ..., anan (1≤ai≤1091≤ai≤109) — the elements of the array aa.
Output
Print a single integer, the minimum number of moves to make bb increasing.
Examples
input
Copy
5 1 2 3 4 5
output
Copy
4
input
Copy
7 1 2 1 2 1 2 1
output
Copy
10
input
Copy
8 1 8 2 7 3 6 4 5
output
Copy
16
Note
Example 11: you can subtract a1a1 from b1b1, and add a3a3, a4a4, and a5a5 to b3b3, b4b4, and b5b5 respectively. The final array will be [−1−1, 00, 33, 44, 55] after 44 moves.
Example 22: you can reach [−3−3, −2−2, −1−1, 00, 11, 22, 33] in 1010 moves.
思路:我们通过观察可以得知,数组b存在0时可以使操作数最少,减少一次操作,也能将其他数的值减少。这时我们只要暴力寻找0点位置,然后再由0点向外扩展即可,这样就能满足单调行了,让离0点的变化次数最小,此时的时间复杂度是n^2,对于5000的数据足够了。
结论:暴力枚举0点,由0点向外扩展(向两端扩展)。
完整代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
const int N=5010;
int a[N];
void solve()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int ans=1e18;
for(int i=1;i<=n;i++)
{
int res=0;
int f=0,x=0;
for(int j=i-1;j>=1;j--)
{
x=f/a[j]+1;
res+=x;
f=x*a[j];
}
x=0,f=0;
for(int j=i+1;j<=n;j++)
{
x=f/a[j]+1;
res+=x;
f=x*a[j];
}
ans=min(ans,res);
}
cout<<ans<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}