UVA - 10670 Work Reduction 贪心
2014-11-05 22:42
369 查看
题目大意:有N个单元的文件,boss你只能剩下M个单元的文件,有L家抄写机构,每个机构都能提供两种服务,一种是抄1个单元,另一种是抄一半,两个服务各有价格,要求列出一张表单,表单上是每个机构的名称和聘请该机构完成任务所需的最小钱数,要求按钱数的升序排列,如果钱数相同,就按机构名称的升序排列,注意,机构名称是一个字符串
解题思路:贪心,如果帮忙完成一半的服务比完成一个单元*要完成的单元的价格还要少的话,肯定先选要完成的单元。
解题思路:贪心,如果帮忙完成一半的服务比完成一个单元*要完成的单元的价格还要少的话,肯定先选要完成的单元。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 100 + 5; int N,M,L; char name[maxn][20]; int cost[maxn],a[maxn],b[maxn],temp[maxn]; char str[maxn]; int cmp(const void *p1,const void *p2) { int *p = (int *)p1; int *q = (int *)p2; if(cost[*p] == cost[*q]) return strcmp(name[*p],name[*q]); else return cost[*p] > cost[*q] ? 1:-1; } int main() { int test; int mark = 1; scanf("%d",&test); while(test--) { scanf("%d%d%d\n", &M,&N,&L); for(int i = 0; i < L; i++) { scanf("%s",str); for(int j = 0; str[j];j++) if(str[j] == ':' || str[j] == ',') str[j] = ' '; sscanf(str,"%s%d%d",name[i],&a[i],&b[i]); } printf("Case %d\n", mark++); memset(cost,0,sizeof(cost)); int s; for(int i = 0; i < L; i++) { s = M; while( s-(s + 1)/2 >= N && b[i] < (s+1) / 2 * a[i]) { s = s - (s + 1) / 2; cost[i] = cost[i] + b[i]; } cost[i] = cost[i] + a[i] * (s - N); } for(int i = 0; i < L; i++) temp[i] = i; qsort(temp,L,sizeof(temp[0]),cmp); for(int i = 0; i < L; i++) printf("%s %d\n",name[temp[i]],cost[temp[i]]); } return 0; }
相关文章推荐
- Uva-10670 Work Reduction(贪心)
- UVA - 10670 Work Reduction (贪心)
- uva 10670 Work Reduction(贪心)
- uva - 10670 - Work Reduction(贪心)
- uva 10670 Work Reduction (贪心)
- uva 10670 Work Reduction(贪心)
- UVa 10670 Work Reduction 解题报告(贪心)
- uva 10670 Work Reduction(贪心)
- UVA 10670 Work Reduction (贪心 + 被题意坑了- -)
- uva:10670 - Work Reduction(贪心)
- uva 10670 Work Reduction
- UVa 10670 - Work Reduction
- UVA 10670 Work Reduction
- uva10670 - Work Reduction
- uva 10670 - Work Reduction
- UVa:10670 Work Reduction
- UVa 10670 - Work Reduction
- uva 10670 Work Reduction
- UVa 10670 - Work Reduction
- uva10670 Work Reduction