Cod sursa(job #2832110)

Utilizator eazachiar nu cont eaza Data 12 ianuarie 2022 20:27:35
Problema Taramul Nicaieri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = 1e2 + 2;

#define cin fin
#define cout fout
ifstream cin("harta.in");
ofstream cout("harta.out");

struct cell {
  int x, y, i;
} v[nmax];
auto byx = [](cell a, cell b){ return pair<int,int>{a.x,a.y} < pair<int,int>{b.x,b.y};};
auto byy = [](cell a, cell b){ return pair<int,int>{a.y,a.x} > pair<int,int>{b.y,b.x};};

int rnd(int x) {
  return rand()*rand()%x;
}

int occ[nmax];

int main() {
  srand(time(0));
  int n, m = 0;
  cin >> n;
  for(int i = 0, x, y; i < n; i++) {
    cin >> x >> y; 
    v[i] = {x,y};
    m += x + y;
    v[i].i = i + 1;
  }
  m /= 2;
  cout << m << '\n';
  for(int t = 0; t < n; t++) {
    int i = -1;
    sort(v,v+n,byy);
    for(int h = 0; h < n; h++)
      if(occ[v[h].i] == 0)
        if(i == -1 || v[h].x > v[i].x)
          i = h;
    //cout << v[i].i << '\n';
    occ[v[i].i] = 1;
    for(int j = 0; v[i].x > 0 && j < n; j++) {
      if(j == i)
        continue;
      if(v[j].y > 0)
        v[j].y --, v[i].x--, cout << v[i].i << ' '<< v[j].i << '\n';
      //else
        //cout << "___" << v[i].i << ' '<< v[j].i << ' '<< v[j].x << ' '<< v[i].y << '\n';
    }
  }
}
/*
 * 0 0
 * 2 0
 * 0 2
 * 0 0
 * 
 */