CF.1416B. Make Them Equal

You are given an array aa consisting of nn positive integers, numbered from 11 to nn. You can perform the following operation no more than 3n3n times:

1. choose three integers i, j and x (1≤i,j≤n; 0≤x≤10^9);
2. assign ai:=ai−x⋅i,aj:=aj+x⋅i.

After each operation, all elements of the array should be non-negative.

Can you find a sequence of no more than 3n3n operations after which all elements of the array are equal?

#include<bits/stdc++.h>
#define N 10010
#define ll long long
using namespace std;
int n,a[N];
struct nd{
int l,r,v;
};
vector<nd>ans;
int main(){
int qt;
scanf("%d",&qt);
while(qt--){
scanf("%d",&n);
int tot=0;
ans.clear();
for(int i=1;i<=n;i++)scanf("%d",&a[i]),tot+=a[i];
if(tot%n!=0){
printf("-1\n");continue;
}
int x;
for(int i=2;i<=n;i++){
int t=a[i]/i;
if(a[i]%i==0){
a[i]-=t*i;a[1]+=t*i;
//cout
ans.push_back((nd){i,1,t});
}else{
x=i-a[i]%i;a[i]+=x;a[1]-=x;
///			cout<<a[1]<<"***"<<endl;
ans.push_back((nd){1,i,x});
x=a[i]/i;a[1]+=a[i];a[i]=0;
ans.push_back((nd){i,1,x});
}
}
int mid=tot/n;
for(int i=2;i<=n;i++){
a[i]=mid;a[1]-=mid;
ans.push_back((nd){1,i,mid});
}
int len=ans.size();
printf("%d\n",len);
for(int i=0;i<len;i++)printf("%d %d %d\n",ans[i].l,ans[i].r,ans[i].v);
}
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

从冬

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-25
12-27 1万+

11-22 366
12-21 5212
09-04 21万+
07-02 7744
07-21 2万+
08-24 4万+