C. Ehab and a 2-operation tasktime
C. Ehab and a 2-operation tasktime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou’re given an array aa of length nn. You can perform the following operations on it:choose an index ii (1≤i≤n)(1≤i≤n), an integer xx (0≤x≤106)(0≤x≤106), and replace ajaj with aj+xaj+x for all (1≤j≤i)(1≤j≤i), which means add xx to all the elements in the prefix ending at ii.choose an index ii (1≤i≤n)(1≤i≤n), an integer xx (1≤x≤106)(1≤x≤106), and replace ajaj with aj%xaj%x for all (1≤j≤i)(1≤j≤i), which means replace every element in the prefix ending at ii with the remainder after dividing it by xx.Can you make the array strictly increasing in no more than n+1n+1 operations?InputThe first line contains an integer nn (1≤n≤2000)(1≤n≤2000), the number of elements in the array aa.The second line contains nn space-separated integers a1a1, a2a2, ……, anan (0≤ai≤105)(0≤ai≤105), the elements of the array aa.OutputOn the first line, print the number of operations you wish to perform. On the next lines, you should print the
1 2 3
outputCopy0inputCopy3
7 6 3
outputCopy2
1 1 1
2 2 4
NoteIn the first sample, the array is already increasing so we don’t need any
#include<bits/stdc++.h> using namespace std; int num=1e6,i; int main() { int a; cin>>a; cout<<a+1<<endl; //cout<<1<<" "<<a<<" "<<1<<endl; cout<<2<<" "<<a<<" "<<1<<endl; cout<<1<<" "<<a<<" "<<num<<endl; while(--a) { printf("2 %d %d\n",++i,--num); } return 0; }
這道題,我沒想到1e6會用到,還有n+1,因為是找出存在的解,所以找到一般需要技巧,驗證解是否存在,我的常規想法往往是找出最優解,所以會陷入誤區,所以存在性問題要會找答案。由於%1所有數都會為0,很特殊,所以直接轉化,1e6很大的數也是有 特徵的,直接各個加上,模上不超過2000的數,沒有影響,又是不超過n+1步,所以直接搞成遞增就行了
哇,一看程式碼,瞬間覺得自己傻叉了,還硬算,如此簡潔的程式碼,還是要充分利用條件的