Guest User

Untitled

a guest
Jun 19th, 2018
76
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 <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. struct info {
  8.     string nom;
  9.     int valor;
  10. };
  11.  
  12. bool comp1(const info& a, const info& b) {
  13.     if (a.nom != b.nom) return a.nom < b.nom;
  14.     return a.valor < b.valor;
  15. }
  16.  
  17. bool comp2(const info& a, const info& b) {
  18.     if (a.valor != b.valor) return a.valor < b.valor;
  19.     return a.nom < b.nom;
  20. }
  21.  
  22. int main() {
  23.     int n;
  24.     cin >> n;
  25.     while (n > 0) {
  26.         --n;
  27.         int m;
  28.         cin >> m;
  29.         vector<info> v(2*m);
  30.         for (int i = 0; i < 2*m; i += 2) {
  31.             string s, t;
  32.             int valor;
  33.             cin >> s >> t >> valor;
  34.             v[i].nom = s;
  35.             v[i].valor = -valor;
  36.             v[i + 1].nom = t;
  37.             v[i + 1].valor = valor;
  38.         }
  39.         sort(v.begin(), v.end(), comp1);
  40.         vector<info> res(m);
  41.         for (int i = 0; i < m; ++i) {
  42.             res[i].nom = v[2*i].nom;
  43.             res[i].valor = v[2*i].valor + v[2*i + 1].valor;
  44.         }
  45.         sort(res.begin(), res.end(), comp2);
  46.         for (int i = 0; i < m; ++i) {
  47.             cout << res[i].nom << " " << res[i].valor << endl;
  48.         }
  49.         cout << endl;
  50.     }
  51. }
Add Comment
Please, Sign In to add comment