uva - 10670 - Work Reduction(贪心)
来源:互联网 发布:mac如何设置搜狗输入法 编辑:程序博客网 时间:2024/05/18 02:20
题意:N个单元的文件,Boss要求剩下M个,其余的要你完成。但是在deadline之前你完不成所以你要雇佣机构来帮你。L个机构,每个机构都可以分A、B两种方案,A是帮你完成一个单元,B是帮你完成剩下的一半。输出每个机构的最小花费。注意rounding down是四舍五入的意思。
方法:贪心计算就行,循环结束的条件是(1) temp_N - (temp_N+1)/2 >= M,(2) agen[i].B < (temp_N+1)/2 * agen[i].A。注意这两个就行,下边的WA代码也是这个思路但是wa.. 不知道原因。
AC代码:
#include <iostream> #include <iomanip> #include <string> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <algorithm> #include <cmath> #include <ctime> using namespace std;const int maxn_f = 100000+10;const int maxn_a = 100+10;struct Agency{char name[20];int A;int B;int cost;};Agency agen[maxn_a];void Handle_Input(char temp[], int i){int j = 0, k = 0;char temp1[50];memset(temp1, '\0', sizeof(temp1));for (j = 0; temp[j] != ':'; j++)agen[i].name[k++] = temp[j];for (k = 0, j++; temp[j] != ','; j++)temp1[k++] = temp[j];agen[i].A = atoi(temp1);memset(temp1, '\0', sizeof(temp1));for (k = 0, j++; j < strlen(temp); j++)temp1[k++] = temp[j];agen[i].B = atoi(temp1);}int cmp(const void *a, const void *b){struct Agency *c = (Agency *)a;struct Agency *d = (Agency *)b;if ((*c).cost != (*d).cost)return (*c).cost - (*d).cost;elsereturn strcmp((*c).name, (*d).name);}int main(){#ifdef Localfreopen("a.txt", "r", stdin);#endifint t = 0;cin >> t;for (int kases = 1; kases <= t; kases++){memset(agen, 0, sizeof(agen));int N = 0, M = 0, L = 0;int i = 0;cin >> N >> M >> L;for (i = 0; i < L; i++){char temp[50];cin >> temp;Handle_Input(temp, i);int temp_N = N;while (temp_N - (temp_N+1)/2 >= M && agen[i].B < (temp_N+1)/2 * agen[i].A){agen[i].cost += agen[i].B;temp_N -= (temp_N+1) / 2;}agen[i].cost += (temp_N-M) * agen[i].A;}qsort(agen, L, sizeof(agen[0]), cmp);cout << "Case " << kases << endl;for (i = 0; i < L; i++)cout << agen[i].name << " " << agen[i].cost << endl;}return 0;}
WA代码:
#include <iostream> #include <iomanip> #include <string> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <algorithm> #include <cmath> #include <ctime> using namespace std;const int maxn_f = 100000+10;const int maxn_a = 100+10;struct Agency{char name[20];int A;int B;int cost;};Agency agen[maxn_a];void Handle_Input(char temp[], int i){int j = 0, k = 0;char temp1[50];memset(temp1, '\0', sizeof(temp1));for (j = 0; temp[j] != ':'; j++)agen[i].name[k++] = temp[j];for (k = 0, j++; temp[j] != ','; j++)temp1[k++] = temp[j];agen[i].A = atoi(temp1);memset(temp1, '\0', sizeof(temp1));for (k = 0, j++; j < strlen(temp); j++)temp1[k++] = temp[j];agen[i].B = atoi(temp1);}int cmp(const void *a, const void *b){struct Agency *c = (Agency *)a;struct Agency *d = (Agency *)b;if ((*c).cost != (*d).cost)return (*c).cost - (*d).cost;elsereturn strcmp((*c).name, (*d).name);}int main(){#ifdef Localfreopen("a.txt", "r", stdin);#endifint t = 0;cin >> t;for (int kases = 1; kases <= t; kases++){memset(agen, 0, sizeof(agen));double N = 0, M = 0, L = 0;int i = 0;cin >> N >> M >> L;for (i = 0; i < L; i++){char temp[50];cin >> temp;Handle_Input(temp, i);double temp_N = N;while (true){if (temp_N / 2 < M){agen[i].cost += (temp_N-M) * agen[i].A;break;}else if ((agen[i].B / (int)(temp_N/2+0.5)) < agen[i].A){agen[i].cost += agen[i].B;temp_N = temp_N - (int)(temp_N/2+0.5);}else{agen[i].cost += (temp_N-M) * agen[i].A;break;}}}qsort(agen, L, sizeof(agen[0]), cmp);cout << "Case " << kases << endl;for (i = 0; i < L; i++)cout << agen[i].name << " " << agen[i].cost << endl;}return 0;}
1 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
- UVA 10670 - Work Reduction
- uva 10670 - Work Reduction
- uva 10670 Work Reduction
- UVa:10670 Work Reduction
- uva 10670 - Work Reduction
- 算法思考--------生命游戏
- 将一个链式链表L分成2个链表,一个存储L的奇数位置的元素,一个存储L偶数位置的元素
- Java常见面试题
- poj3660
- 开始读英文原著Thinking in java
- uva - 10670 - Work Reduction(贪心)
- linux中文件压缩解压缩-----详解
- 总结一些 android 平常用到的自定义控件
- 【英语学习】四月份
- hdu1856 More is better
- ERROR mapreduce.TableInputFormatBase
- 什么是动态网页
- 字节对齐
- 渐行渐远~~ 只为了自我提高