一、题目
L2-009 抢红包
二、题解
1.基本思路:
- 主要时要理解题意,每个人发完红包,自己的金额就要减去红包的总金额数
- 最后在按照题意排个序即可
2.代码:
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl "\n"
#define int long long
#define fi first
#define se second
const int N = 1e4+10, INF = 0x3f3f3f3f;
struct node{
int id,num,p; //编号,抢到的红包个数,金额(分)
double w; //元
bool operator < (const node &A){ //重载<运算符
if(w==A.w&&num!=A.num) return num>A.num; //红包的个数递减输出
else if(w==A.w&&num==A.num) return id<A.id; //按个人编号递增输出
return w>A.w; //收入金额递减顺序
}
}arr[N];
void solve() {
int n; cin>>n;
for(int i=1;i<=n;i++){
int k,sum=0; //发的红包个数k,总金额sum分
cin>>k;
set<int> s; //每人最多只能抢1次
for(int j=1;j<=k;j++){
int a,b; cin>>a>>b;
if(s.count(a)) continue;
s.insert(a); //编号a已抢过
sum+=b; //记录总金额
arr[a].num++; arr[a].p+=b;
}
arr[i].p-=sum; arr[i].id=i; //记录i的信息
}
for(int i=1;i<=n;i++) arr[i].w=arr[i].p*1.0/100; //分转变为元
sort(arr+1,arr+n+1);
for(int i=1;i<=n;i++)
printf("%d %.2f\n",arr[i].id,arr[i].w);
}
signed main() {
// IOS;
int T=1;
// cin>>T;
while(T--) {
solve();
}
return 0;
}
/*
10
3 2 22 10 58 8 125
5 1 345 3 211 5 233 7 13 8 101
1 7 8800
2 1 1000 2 1000
2 4 250 10 320
6 5 11 9 22 8 33 7 44 10 55 4 2
1 3 8800
2 1 23 2 123
1 8 250
4 2 121 4 516 7 112 9 10
1 11.63
2 3.63
8 3.63
3 2.11
7 1.69
6 -1.67
9 -2.18
10 -3.26
5 -3.26
4 -12.32
*/