CF1523B Lord of the Values(构造+思维)
题意:
给定一个长度为\(n(2<=n<=10^{3}且为偶数)\)的序列,每次都可以选择两个下标\(i,j\)并且\(i<j\),进行下面两种操作之一:
\(1.\) \(a_{i}=a_{i}+a_{j}\)
\(2.a_{j}=a_{j}-a_{i}\)
最多执行\(5000\)次操作,并且操作后的的值不能超过\(10^{18}\)。
输出方案使得序列全部变为相反数。
思路:
对于两个数来说,按照 \(1 2 1 1 2 1\)的顺序执行能够将这两个数都变为自身的相反数。所以,两两的进行操作,最多操作次数为\(6*n/2=3*n\)次。
代码:
// Problem: B. Lord of the Values
// Contest: Codeforces - Deltix Round, Spring 2021 (open for everyone, rated, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/1523/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
const int maxn=1100;
int n,a[maxn];
int main(){
int T;cin>>T;
while(T--){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
cout<<3*n<<endl;
for(int i=1;i<=n;i+=2){
cout<<"1 "<<i<<" "<<i+1<<endl;
cout<<"2 "<<i<<" "<<i+1<<endl;
cout<<"1 "<<i<<" "<<i+1<<endl;
cout<<"1 "<<i<<" "<<i+1<<endl;
cout<<"2 "<<i<<" "<<i+1<<endl;
cout<<"1 "<<i<<" "<<i+1<<endl;
}
}
return 0;
}