UVa 10670 - Work Reduction
来源:互联网 发布:linux ant 配置 编辑:程序博客网 时间:2024/05/21 09:18
贪心题,只要每一步都保证B的费用小于等于A的费用,则一直用B方案,否则用A方案结束,唯一值得注意的是在B方案减半时不能使N的值小于M的值(题目中有要求)。
代码如下:
#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>using namespace std;struct agency{ int A, B, V; char name[20];} gen[102];int cmp(const void *a, const void *b){ agency *aa = (agency*)a; agency *bb = (agency*)b; if(aa->V != bb->V) return aa->V - bb->V; return strcmp(aa->name, bb->name);}int main(){#ifdef test freopen("in.txt", "r", stdin);#endif int t; char str[50]; scanf("%d", &t); for(int k = 1; k <= t; k++) { int N, M, num; scanf("%d%d%d", &N, &M, &num); for(int i = 0; i < num; i++) { scanf("%s", str); sscanf(str, "%[^:]:%d,%d",gen[i].name, &gen[i].A, &gen[i].B); } for(int i = 0; i < num; i++) { int tem_a = 0, tem_b = 0, n = N; while(n / 2 >= M && gen[i].A * (n / 2) > gen[i].B) // 实际为 gen[i].A * (n-(n/2)),保证每一步A的费用都大于B的费用 { n /= 2; ++tem_b; } tem_a = n - M; gen[i].V = tem_b * gen[i].B + tem_a * gen[i].A; } qsort(gen, num, sizeof(gen[0]), cmp); printf("Case %d\n", k); for(int i = 0; i < num; i++) printf("%s %d\n", gen[i].name, gen[i].V); } 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--10670Work Reduction +模拟
- uva 10670 Work Reduction(贪心)
- UVa 10670 - Work Reduction(水)
- uva 10670 Work Reduction(贪心)
- uva - 10670 - Work Reduction(贪心)
- uva:10670 - Work Reduction(贪心)
- Office 2007 中 Word 出现 Setup Error 的解决方法
- HDU 4027 单点更新+“剪枝”
- hdu 1003 Max Sum
- 初级程序员必知--第一个程序是这样写出来的!
- viewDidUnload释疑
- UVa 10670 - Work Reduction
- HDU-1711(KMP)
- ContentType ,charset和pageEncoding的区别
- JSON
- 快速删除超大文件夹
- 微软笔试题 跳台阶问题
- 堆和栈的区别(转载)
- C# HTML生成PDF
- 逆序