Pagini recente » Cod sursa (job #2054827) | Cod sursa (job #2127167) | Cod sursa (job #2491584) | Cod sursa (job #2735350) | Cod sursa (job #437158)
Cod sursa(job #437158)
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
class Gutuie{
public:
unsigned int greutate, nivel;
};
bool comp1(Gutuie a, Gutuie b){
if( a.nivel < b.nivel )
return false;
else
if( a.nivel == b.nivel ){
if( a.greutate > b.greutate )
return true;
else
return false;
}
else
return true;
}
bool comp2(unsigned int a, unsigned int b){
return a > b;
}
inline unsigned int nivel( unsigned int a, unsigned int u, unsigned int hmax ){
if( a % u <= hmax % u )
return a / u;
else
return a / u + 1;
}
int main(){
unsigned int n, u, hmax;
ifstream fin("gutui.in");
ofstream fout("gutui.out");
fin >> n >> hmax >> u;
unsigned int i, x, y;
vector<Gutuie> v(n);
for(i = 0; i < n; i++){
fin >> x >> y;
v[i].greutate = y;
v[i].nivel = nivel(x, u, hmax);
}
sort( v.begin(), v.end(), comp1 );
unsigned int nivmax = nivel(hmax, u, hmax), niv = v[0].nivel, nr = 0;
unsigned int gmax = 0;
vector<unsigned int> greutate;
for(i = 0; i < n; i++){
if( v[i].nivel == niv ){
if( nr < nivmax - niv + 1 ){
nr ++;
if( greutate.size() < nivmax - niv + 1 ){
gmax += v[i].greutate;
greutate.push_back( v[i].greutate );
push_heap( greutate.begin(), greutate.end(), comp2 );
}
else
if( greutate.front() < v[i].greutate ){
gmax -= greutate.front();
gmax += v[i].greutate;
pop_heap( greutate.begin(), greutate.end(), comp2 );
greutate.pop_back();
greutate.push_back( v[i].greutate );
push_heap( greutate.begin(), greutate.end(), comp2 );
}
}
}
else{
niv = v[i].nivel;
nr = 0;
i --;
}
}
fout << gmax;
fin.close();
fout.close();
return 0;
}