algorithm_2024/P1031/pro.cpp

70 lines
1.3 KiB
C++
Raw Normal View History

2024-10-01 03:52:01 +00:00
#include <ios>
#include<iostream>
#include <queue>
#include <vector>
const int MAX_N = (int)1e2+5;
int n;
int a[MAX_N];
int sum;
int avg;
int ans;
struct Gift{
int next,num;
};
std::vector<Gift> to[MAX_N];
int inDegree[MAX_N];
std::queue<int> q;
int main(){
std::ios::sync_with_stdio(false),std::cin.tie(nullptr),std::cout.tie(nullptr);
std::cin>>n;
for(int i=1;i<=n;i++){
std::cin>>a[i];
sum+=a[i];
}
avg = sum/n;
ans = 0;
for(int i=1;i<n;i++){
ans++;
if(a[i]>avg){
int num = a[i]-avg;
to[i].push_back({.next=i+1,.num=num});
inDegree[i+1]++;
a[i+1]+=num;
}else if(a[i]<avg){
int num = avg-a[i];
to[i+1].push_back({.next=i,.num=num});
inDegree[i]++;
a[i+1]-=num;
}else{
ans--;
}
}
for(int i=1;i<=n;i++){
if(inDegree[i]==0){
q.push(i);
}
}
while(q.empty()==false){
int top = q.front();
q.pop();
for(Gift i:to[top]){
inDegree[i.next]--;
std::cout<<top<<" give "<<i.next<<" "<<i.num<<" points\n";
if(inDegree[i.next]==0){
q.push(i.next);
}
}
}
// std::cout<<ans<<"\n";
}