Codeforces Round #525 (Div. 2)C. Ehab and a 2-operation task
阿新 • • 發佈:2018-12-06
一個數 n-1 i++ 我們 text ++ target sin const
C. Ehab and a 2-operation task
題目鏈接:https://codeforc.es/contest/1088/problem/C
題意:
給出n個數,然後最多可以進行n+1次操作,可以選擇前i個數都加上一個非負數或者模上一個數,使最後這n個數嚴格遞增。
題解:
我說下我的解法:
從末尾往前考慮,我們假設模上的數為n,那麽最後的數列一定是0,1......n-1。
從末往前的話,可以對當前最後的一個數進行加法操作,那麽通過n次我們可以對n個數進行加法操作,最後使得這些數模上n之後都為0,1.....n-1就好了,嚴格操作上界是n+1次。
這題也可以先模後加~
給出我的先加再模的代碼:
#include <bits/stdc++.h> using namespace std; const int N = 1e5+5 ; int n; int a[N]; vector <pair<pair<int,int>,int> > ans; int main(){ scanf("%d",&n); a[0]=-1; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } int sum=0; for(int i=n;i>=1;i--){ a[i]+=sum; if(a[i]%n!=i-1){ int tmp = i-1-a[i]%n; while(tmp<n) tmp+=n; ans.push_back({{1,i},tmp}); sum+=tmp; }else ans.push_back({{1,i},0}); } ans.push_back({{2,n},n}); cout<<ans.size()<<endl;for(auto v : ans){ cout<<v.first.first<<" "<<v.first.second<<" "<<v.second<<endl; } return 0; }
Codeforces Round #525 (Div. 2)C. Ehab and a 2-operation task