Codeforces Round #525 (Div. 2)C. Ehab and a 2-operation task

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;
}

猜你喜欢

转载自www.cnblogs.com/heyuhhh/p/10074086.html