题目
输入格式:
输入第一行给出一个正整数N(≤104),即参与发红包和抢红包的总人数,则这些人从1到N编号。随后N行,第i行给出编号为i的人发红包的记录,格式如下:
K N1 P1⋯NK PK
其中K(0≤K≤20)是发出去的红包个数,Ni是抢到红包的人的编号,Pi(>0)是其抢到的红包金额(以分为单位)。注意:对于同一个人发出的红包,每人最多只能抢1次,不能重复抢。
输出格式:
按照收入金额从高到低的递减顺序输出每个人的编号和收入金额(以元为单位,输出小数点后2位)。每个人的信息占一行,两数字间有1个空格。如果收入金额有并列,则按抢到红包的个数递减输出;如果还有并列,则按个人编号递增输出。
输入样例:
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
AC代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <iomanip>
using namespace std;
bool cmp(pair<int,pair<int,int>> a,pair<int,pair<int,int>> b){ //对值进行排序以符合输出要求
if(a.second.second!=b.second.second){
return a.second.second>b.second.second;
}else if(a.second.first!=b.second.first){
return a.second.first>b.second.first;
}else{
return a.first<b.first;
}
}
int main() {
ios::sync_with_stdio(false);
map<int,pair<int,int>>x; //创建map以快速存储和查找数据
int n;
cin>>n;
for (int i = 0; i < n; ++i) {
int num;
cin>>num;
for (int j = 0; j < num; ++j) {
int a,b;
cin>>a>>b;
x[i+1].second-=b; //插入数据或修改数据
x[a].second+=b; //记录谁拿了钱
x[a].first+=1; //让拿红包的人红包数加一
}
}
vector<pair<int,pair<int,int>>>ansSheet;
for (int k = 1; k <=n ; ++k) {
ansSheet.push_back(make_pair(k,x[k])); //将答案连同序号一起塞进答案向量中
}
sort(ansSheet.begin(),ansSheet.end(),cmp);
for (int l = 0; l <n; ++l) { //排序后计算出结果
float b=100;
float a=ansSheet[l].second.second/b;
cout<<ansSheet[l].first<<' '<<fixed<<setprecision(2)<<a<<endl;
}
return 0;
}
题解
也是一道大水题
根据给出的数据修改Map中的值,最后对修改好的数据排序、计算、输出答案即可。