Codeforces Global Round 13 C题(差分)
https://codeforces.com/contest/1491/problem/C
贪心,从左往右每一轮只需要每个长度跳第一次,可以用差分数组维护每个点被踩的次数。
1 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 2 #define bug(x) cout<<#x<<" is "<<x<<endl 3 #include <bits/stdc++.h> 4 #define iter ::iterator 5 using namespace std; 6 typedef long long ll; 7 typedef pair<int,ll>P; 8 #define pb push_back 9 #define mk make_pair 10 #define se second 11 #define fi first 12 #define rs o*2+1 13 #define ls o*2 14 const ll inf=1e18; 15 const int N=2e5+5; 16 int T; 17 18 int n,cnt; 19 20 ll a[N],b[N],c[N]; 21 22 23 int main(){ 24 IO; 25 cin>>T; 26 while(T--){ 27 int f=0; 28 cin>>n; 29 for(int i=1;i<=n;i++){ 30 cin>>a[i]; 31 b[i]=0; 32 } 33 ll ans=0; 34 35 for(int i=1;i<=n;i++){ 36 b[i]+=b[i-1]; 37 b[i+2]++; 38 int r=i+a[i]-1; 39 if(r<=n)b[i+a[i]+1]--; 40 ans+=max(0ll,a[i]-b[i]-1); 41 if(b[i]-a[i]+1>0){ 42 b[i+1]+=b[i]-a[i]+1; 43 b[i+2]-=b[i]-a[i]+1; 44 } 45 } 46 47 48 cout<<ans<<endl; 49 } 50 51 } 52 /* 53 18 54 6 6 5 4 3 3 3 2 3 5 2 10 7 6 1 1 10 2 55 56 57 58 1 4 2 2 2 2 2 59 60 1 3 2 2 2 1 2 61 62 1 2 2 2 1 1 1 63 64 1 1 2 1 1 1 1 65 66 67 */