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

时间:2023-03-10 02:28:08
Codeforces Round #525 (Div. 2) C. Ehab and a 2-operation task

传送门

https://www.cnblogs.com/violet-acmer/p/10068786.html

题意:

  给定一个长度为 n 的数组a[ ],并且有两种操作:

  ①将前 i 个数全都加上 x;

  ②将前 i 个数全都 mod x

  要求用不超过 n+1 次操作,使得数组 a[ ] 严格单调递增。

题解:

  预备知识补充:

  假设 a > b,在什么条件下可以使式子 a%(a-b) == b 成立 ?

  只有当 a > 2*b 时才成立。

  证明如下:

  用反证法,假设 a < 2*b,那么 b > a/2。

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

  如果 a > 2*b 呢?

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

  根据题干要求,x 最大可取 1e6 ,而 i 最大才 2000 ,所以可以通过上述公式使数组 a[ i ]=i;

  步骤:

    (1):数组a[ ] 全部加上一个较大的数 maxNum(maxNum+a[i] > 2*i , 最极端的情况就是 i = 2000,a[i]=0,那么只需让 maxNum = 40001就行了);

    (2):对于操作(1)后的每个数a[ i ],实施操作 a[ i ]%( a[ i ]-i ),使a[ i ] = i,最终序列便是 1~n 的排列,当然严格单调递增啦。

AC代码:

 #include<iostream>
#include<cstdio>
using namespace std;
const int maxn=+; int n;
int a[maxn];
int maxNum=; void Solve()
{
printf("%d\n",n+);//共进行了 n+1 次操作
printf("%d %d %d\n",,n,maxNum);
for(int i=;i <= n;++i)
printf("%d %d %d\n",,i,maxNum+a[i]-i);
}
int main()
{
scanf("%d",&n);
for(int i=;i <= n;++i)
scanf("%d",a+i);
Solve();
}