hdu1009贪心
/*头文件:
#include <algorithm>
using namespace std;
1.默认的sort函数是按升序排。对应于1)
sort(a,a+n); //两个参数分别为待排序数组的首地址和尾地址
2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)
例如:
int cmp( const int &a, const int &b ){
if( a > b )
return 1;
else
return 0;
}
sort(a,a+n,cmp);
是对数组a降序排序
又如:
int cmp( const POINT &a, const POINT &b ){
if( a.x < b.x )
return 1;
else
if( a.x == b.x ){
if( a.y < b.y )
return 1;
else
return 0;
}
else
return 0;
}
sort(a,a+n,cmp);
是先按x升序排序,若x值相等则按y升序排
*/
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; struct date { int j,f; double res; }a [3001]; bool cmp(date a,date b)//判断真值 真为1 假为0 不用bool也行 可以改为int { return a.res>b.res; } int main() { int M,N,i; while(scanf("%d%d",&M,&N)!=EOF) { if(M==-1&&N==-1) break; for(i=0;i<N;i++) { scanf("%d%d",&a[i].j,&a[i].f); a[i].res=(double)a[i].j/a[i].f; } sort(a,a+N,cmp); double sum=0; for(i=0;i<N;i++) { if(M<=a[i].f) { sum=sum+M*a[i].res;M=0;break; } else { M=M-a[i].f; sum=sum+a[i].j; } } printf("%.3lf\n",sum); } }