[HDU] [2187] Mourning the Victims of the 512 Wenchuan Earthquake—The Old Man Is Really Hungry

Problem Description

Loading...

Problem Analysis

Not a hard problem.
Roughly, sort first and then simulate the purchases.
Is this the legendary Greedy?
In memory of the compatriots lost in the great earthquake~

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<stdio.h>
int main() {
	int C,n,m,p[1010],h[1010],t;
	int i,k,j;
	double r;
	scanf("%d",&C);
	while(C--) {
		scanf("%d %d",&n,&m);
		for(i=0; i<m; i++) {
			scanf("%d %d",&p[i],&h[i]);
		}
		for(k=m-1; k>=0; k--) {
			for(i=1,t=p[0]; i<m; i++) {
				if(p[i]<p[i-1]) {
					t=p[i];
					p[i]=p[i-1];
					p[i-1]=t;
					t=h[i];
					h[i]=h[i-1];
					h[i-1]=t;
				}
			}
		}
		for(r=0,j=0; j<m; j++) {
			if((n-p[j]*h[j])>=0) {
				r=r+h[j];
				n=n-p[j]*h[j];
			} else {
				r=r+double(n)/double(p[j]);
				break;
			}
		}
		printf("%.2lf\n",r);
	}
	return 0;
}

Loading...