Advertisement
JosepRivaille

P96432: L'amic invisible

May 28th, 2015
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Participant {
  7.     string nom;
  8.     int saldo;
  9. };
  10.  
  11. typedef vector<Participant> Gent;
  12.  
  13. bool ordre_saldo(Participant a, Participant b) {
  14.     if (a.saldo == b.saldo) return a.nom <= b.nom;
  15.     return a.saldo < b.saldo;
  16. }
  17.  
  18. bool ordre_nom(Participant a, Participant b) {
  19.     return a.nom >= b.nom;
  20. }
  21.  
  22. int main() {
  23.     int n;
  24.     cin >> n;
  25.     for (int i = 0; i < n; ++i) {
  26.         int m;
  27.         cin >> m;
  28.         Gent g(2*m);
  29.         for (int j = 0; j < 2*m; j += 2) {
  30.             Participant p, q;
  31.             cin >> p.nom >> q.nom;
  32.             int saldo;
  33.             cin >> saldo;
  34.             p.saldo = -1*saldo;
  35.             q.saldo = saldo;
  36.             g[j] = p;
  37.             g[j + 1] = q;
  38.         }
  39.         sort(g.begin(), g.end(), ordre_nom);
  40.         Gent g2(m);
  41.         for (int j = 0; j < m; ++j) {
  42.             Participant p;
  43.             p.nom = g[2*j].nom;
  44.             p.saldo = g[2*j].saldo + g[2*j + 1].saldo;
  45.             g2[j] = p;
  46.         }
  47.         sort(g2.begin(), g2.end(), ordre_saldo);
  48.         for (int j = 0; j < m; ++j) cout << g2[j].nom << ' ' << g2[j].saldo << endl;
  49.         cout << endl;
  50.     }
  51. }
  52.  
  53. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement