CF.1416B. Make Them Equal

题目链接:http://codeforces.com/contest/1416/problem/B

描述:

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?

分析:当x=1,i=1时的两种情况比较特殊,从此入手,先把所有可以加的值加到a[1]上,当a[i]%i==0时,直接加到a[1]即可,若a[i]%i!=0,先与a[1]操作,使a[i]加到为i的倍数,然后再全加到a[1],因为a[i]>=1,所以一定可以完成以上操作

然后令i=1,去使其他数变为平均数。最终操作次数最多是3n-3

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值