Pagini recente » Cod sursa (job #2287374) | Cod sursa (job #2712928) | Cod sursa (job #539982) | Cod sursa (job #2476459) | Cod sursa (job #3190988)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("harta.in");
ofstream fout("harta.out");
int n, x, y, s, mx, poz;
struct Nod {
int in;
int nod;
int out;
Nod(int a, int b, int c) : in(a), nod(b), out(c) {}
bool operator < (const Nod& other) const
{
if (in > other.in)
return true;
if (in < other.in)
return false;
else
return out > other.out;
}
};
vector<Nod> drum;
int main()
{
fin >> n;
drum.push_back({0, 0, 0});
for(int i = 1; i <= n; i++)
{
fin >> x >> y;
Nod nod(y, i, x);
drum.push_back(nod);
s += y;
}
fout << s << endl;
while(n)
{
sort(drum.begin() + 1, drum.end());
n--;
mx = 0; poz = 0;
for (int i = 1; i < drum.size(); i++)
{
if (drum[i].out > mx)
{
mx = drum[i].out;
poz = i;
}
}
for (int i = 1; i <= mx + 1; i++)
{
if (i != poz && drum[poz].out > 0)
{
drum[i].in--;
drum[poz].out--;
fout << drum[poz].nod << " " << drum[i].nod << endl;
}
}
}
return 0;
}