悼念512汶川大地震遇难同胞――老人是真饿了
#include<iostream>
#include<stdio.h>
using namespace std;
struct mi{
int price, weight;
}a[1000], b;
void quick_sort(mi s[], int l, int r){
if(l < r){
int i=l, j=r, x=s[l].price;
b = s[l];
while(i < j){
while(i < j && s[j].price >= x) j--;
if(i < j) s[i++] = s[j];
while(i < j && s[i].price < x) i++;
if(i < j) s[j--] = s[i];
}
s[i] = b;
quick_sort(s, l, i-1);
quick_sort(s, i+1, r);
}
}
int main(){
int c;
cin>>c;
while(c--){
int n, m, i;
cin>>n>>m;
for(i=0; i<m; i++)
cin>>a[i].price>>a[i].weight;
quick_sort(a, 0, m-1);
double sum = 0;
i=0;
while(n>0){
if(n-a[i].price * a[i].weight> 0){
sum += a[i].weight;
n -= a[i].price * a[i].weight;
i++;
}
else {
sum += n*1.0 / a[i].price;
break;
}
}
printf("%.2lf\n", sum);
}
return 0;
}
浙公网安备 33010602011771号