L2-009 Grab Red Packets (Simulation)

topic link

https://pintia.cn/problem-sets/994805046380707840/problems/994805066890854400

ideas

We open a structure Nodeto store the number id, the number of red envelopes received, and numthe total amount of red envelopes obtained money, and then we will deal with the red envelopes received by each line of people, moneyreduce the amount of the red envelopes, and then use the red envelopes to receive red envelopes. The number of people moneyincreases, and the number of red envelopes for those who receive red envelopes will increase, and the final sorting and output will be fine.

code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define endl "\n"
#define PII pair<int,int>
#define INF 0x3f3f3f3f

const int N = 1e4+10;

int n;

struct Node{
    
    
	int id,num;
	double money;
}people[N];

bool cmp(Node a,Node b) {
    
    
	if(fabs(a.money-b.money) <= 0.0001)
		if(a.num == b.num) 
			return a.id < b.id;
	else
		return a.num > b.num;
	return a.money > b.money;
}

map<int,bool> vis;

int main()
{
    
    
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	int k,a;
	double b;
	for(int i = 1;i <= n; ++i){
    
    
		cin>>k;
		people[i].id = i;
		double sum = 0;
		vis.clear();
		for(int j = 1;j <= k; ++j) {
    
    
			cin>>a>>b;
			if(vis[a]) continue;
			vis[a] = true;
			people[i].money -= b;
			people[a].num++;
			people[a].money += b;
		}
	}
	
	sort(people+1,people+1+n,cmp);
	cout<<fixed<<setprecision(2);
	for(int i = 1;i <= n; ++i) {
    
    
		cout<<people[i].id<<" "<<people[i].money/100.0<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_46201544/article/details/123804795