Topic description
No one has not snatched red envelopes... Here are the records of N people sending red envelopes to each other and robbing red envelopes. Please count their gains from robbing red envelopes.
Input format:
The first line of input gives a positive integer N ( ≤ 1 0 4 ) N (≤ 10^4 )N(≤104 ), that is, the total number of people participating in the red envelope distribution and grabbing red envelopes, then these people are numbered from 1 to N. Next N lines, the i-th line gives the record of the red envelope issued by the person number i, the format is as follows:whereK ( 0 ≤ K ≤ 20 ) K (0≤K≤20)
K(0≤K≤2 0 ) is the number of red envelopes sent out,N i NiNi
is
the number of the person who grabbed the red envelope, Pi (>0) Pi (>0)Pi(>0 ) is the amount of red envelopes ( init grabs. Note: For the red envelopes issued by the same person, each person can only grab a red envelope at most once, and cannot be repeatedly grabbed.
Output format:
Output each person's serial number and income amount in descending order of income amount (in yuan, output 2 decimal places). Each person's information occupies one line, and there is a space between the two numbers. If there is a tie in the income amount, the output will be decreased according to the number of red envelopes grabbed; if there is a tie, the output will be increased according to the personal number.
Input sample:
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
Sample output:
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
solution
It's not too difficult, pay attention to what the title says is points
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e4 + 10;
int n;
struct User{
int no, num;
double sum;
// 这里貌似只能重载小于,但符号里的含义符合题意
bool operator<(const User& w) const {
if (sum == w.sum) {
if (num == w.num) {
return w.no > no;
}
return num > w.num;
}
return sum > w.sum;
}
}user[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
user[i].no = i;
int k;
scanf("%d", &k);
for (int j = 0; j < k; j++) {
int n, p;
scanf("%d%d", &n, &p);
user[i].sum -= p, user[n].sum += p, user[n].num++;
}
}
for (int i = 1; i <= n; i++) user[i].sum /= 100;
sort(user + 1, user + n + 1);
for (int i = 1; i <= n; i++) {
printf("%d %.2lf\n", user[i].no, user[i].sum);
}
return 0;
}