This commit is contained in:
Zengtudor 2024-10-01 11:52:01 +08:00
parent f4031e39e6
commit ad3358f50a
2 changed files with 72 additions and 1 deletions

View File

@ -16,3 +16,4 @@ endif()
project(alogrithm_2024)
add_executable(P1031 ${CMAKE_CURRENT_LIST_DIR}/P1031/main.cpp)
add_executable(P1031_pro ${CMAKE_CURRENT_LIST_DIR}/P1031/pro.cpp)

70
P1031/pro.cpp Normal file
View File

@ -0,0 +1,70 @@
#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";
}