HDU 2111 Saving HDU

明显的贪心

  
    
#include < stdio.h >
#include
< iostream >
#include
< vector >
#include
< algorithm >
using namespace std;
struct BaoBei{
int p, m;
double val;
};

bool cmp( const BaoBei & a, const BaoBei & b){
return a.p > b.p;    // >号 降序排序
}
int main(){
int v, n;
vector
< BaoBei > vec;
while ( cin >> v, v ){
cin >> n;

vec.clear();
BaoBei tmp;
for ( int i = 0 ; i < n; ++ i){
cin
>> tmp.p >> tmp.m;
vec.push_back(tmp);
}
sort(vec.begin(), vec.end(), cmp);
int ans = 0 ;
for ( int i = 0 ; i < n; ++ i){
if (v <= 0 ){
break ;
}
else {
if ( vec[i].m <= v ){
ans
+= vec[i].p * vec[i].m;
v
-= vec[i].m;
}
else {
ans
+= v * vec[i].p;
v
-= v;
}
}
}
cout
<< ans << endl;
}
return 0 ;
}

你可能感兴趣的:(HDU)