九度 1399 名侦探柯南

http://ac.jobdu.com/problem.php?id=1399

显然是贪心,不过忘了jewellery[i].w,jewellery[i].v中全是整数,做除法要乘以1.0,WA了N次。。看来这东西一旦放下手就不热了。。

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <algorithm>
4 using namespace std;
5
6 struct Jewe{
7 int v,w;
8 }jewellery[100010];
9 bool cmp(struct Jewe a,struct Jewe b)
10 {
11 return a.v*b.w>a.w*b.v;
12 }
13 int main()
14 {
15 int N,C;
16 while(scanf("%d%d",&N,&C)!=EOF){
17 int i;
18 for(i=0;i<N;i++){
19 scanf("%d%d",&jewellery[i].w,&jewellery[i].v);
20 }
21 sort(jewellery,jewellery+N,cmp);
22 long long cur_val=0;
23 for(i=0;i<N;i++){
24 if(C>jewellery[i].w){
25 cur_val+=jewellery[i].v;
26 C-=jewellery[i].w;
27 }else{
28 cur_val+=(int)((double)(1.0*C)*(jewellery[i].v*1.0/jewellery[i].w)+0.5);
29 break;
30 }
31 }
32 printf("%lld\n",cur_val);
33 }
34 }



原文地址:https://www.cnblogs.com/yangce/p/2385785.html