Pagini recente » Rating Neamt Mihai (NGMihai) | Profil Ovidiu94 | Istoria paginii utilizator/3t3r4n | Istoria paginii runda/oni_10_5 | Cod sursa (job #442034)
Cod sursa(job #442034)
#include<fstream>
#include<cstdlib>
#include<climits>
#include<vector>
#include<algorithm>
#define IN "gutui.in"
#define OUT "gutui.out"
using namespace std;
typedef struct
{
unsigned long h;
unsigned long w;
unsigned long l;
} GUTUIE;
bool cmph ( GUTUIE i, GUTUIE j)
{
return i.l > j.l;
}
bool cmpw ( GUTUIE i, GUTUIE j)
{
return i.w < j.w;
}
int main (int argc, char **argv)
{
FILE *fin = fopen (IN, "r");
FILE *fout = fopen (OUT, "w");
int i, aux;
int N;
unsigned long H, U;
unsigned long G = 0;
fscanf (fin, "%d%lu%lu", &N, &H, &U);
vector <GUTUIE> g, Heap;
make_heap (Heap.begin(), Heap.end(), cmpw);
for (i = 0; i < N; ++i)
{
GUTUIE a;
fscanf(fin, "%lu%lu", &a.h, &a.w);
if (a.h > H)
{
aux = 1;
continue;
}
a.l = (H - a.h)/U + 1;
g.push_back(a);
}
sort (g.begin(), g.end(), cmph);
if ( aux == 1)
N = (int)g.size();
unsigned long current = g[0].l;
Heap.push_back (g[0]);
push_heap (Heap.begin(), Heap.end(), cmpw);
for (i = 1; i < N; ++i)
{
if (g[i].l != g[i-1].l)
{
while ( current > g[i].l)
{
if (Heap.size())
{
G += Heap.front().w;
pop_heap (Heap.begin(), Heap.end(), cmpw);
Heap.pop_back();
current--;
}
else break;
}
}
Heap.push_back (g[i]);
push_heap (Heap.begin(), Heap.end(), cmpw);
}
while ( current > 0 && Heap.size())
{
G += Heap.front().w;
pop_heap (Heap.begin(), Heap.end(), cmpw);
Heap.pop_back();
current--;
}
fprintf (fout, "%lu\n", G);
fclose (fin);
fclose (fout);
return 0;
}