D. Make Them Equal (Thinking + Structure)

https://codeforces.com/contest/1417/problem/D


Idea: Observe the sample to know that what can be constructed will eventually become an average value.

Thinking about the topic 3*n, you can consider sweeping the sequence three times. The last time should be to pass a number to turn all the numbers into an average. Then the first two times should be collected on a number.

The structure is convenient to use i==1 to construct, but it is found that the ai of the title can only be subtracted, and it is not easy to reduce ai to a negative number. So try to add all the following numbers to ai. But you can't add directly. If you want to add a1, it can only be subtracted. It is found that the following number can only be a multiple of i. So for the first time, all i=2~n must be changed to multiples of i. This is theoretically possible. (Although I don't know how to prove it). After the first time, put all the following numbers into a1. Finally, a1 is evenly distributed.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
LL a[maxn];
void oper(LL i,LL j,LL x)
{
	a[i]-=i*x;
	a[j]+=i*x;
	cout<<i<<" "<<j<<" "<<x<<endl;
}
void solve()
{
	LL n;cin>>n;LL sum=0;
	for(LL i=1;i<=n;i++) cin>>a[i],sum+=a[i];
	if(sum%n) {
		cout<<"-1"<<endl;return;
	}
	cout<<3*(n-1)<<endl;
	for(LL i=2;i<=n;i++)
	{
		oper(1,i,(i-a[i]%i)%i);
		oper(i,1,a[i]/i);	
	}
	for(LL i=2;i<=n;i++)
	{
		oper(1,i,sum/n);
	}
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--)
  {
  	solve();
  }
return 0;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/108855687