ACdream 1224 Robbers (贪心)
来源:互联网 发布:mac adobecc2015破解 编辑:程序博客网 时间:2024/06/05 22:38
Robbers
Problem Description
N robbers have robbed the bank. As the result of their crime they chanced to get M golden coins. Before the robbery the band has made an agreement that after the robbery i-th gangster would get Xi/Y of all money gained. However, it turned out that M may be not divisible by Y.
The problem which now should be solved by robbers is what to do with the coins. They would like to share them fairly. Let us suppose that i-th robber would get Ki coins. In this case unfairness of this fact is |Xi/Y - Ki/M|. The total unfairness is the sum of all particular unfairnesses. Your task as the leader of the gang is to spread money among robbers in such a way that the total unfairness is minimized.
Input
Output
Sample Input
3 10 41 1 2
Sample Output
2 3 5
Source
Manager
解析:先对每个人都按比例向下取整得到k[i],之后再将剩下的金币每次都分给当前“不公平值”最大的那个人,这样就会让他的“不公平值”减小,直到所有的金币分配完为止。
AC代码:
#include <bits/stdc++.h>using namespace std;int k[1002], x[1002];bool flag[1002];int main(){ #ifdef sxk freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif // sxk int n, m, y, sum; while(scanf("%d%d%d", &n, &m, &y)!=EOF){ sum = 0; for(int i=0; i<n; i++){ scanf("%d", &x[i]); k[i] = m * x[i] / y; if(m * x[i] % y == 0) flag[i] = true; sum += k[i]; } int tx = m - sum; while(tx--){ double t = 0; int j = 0; for(int i=0; i<n; i++){ if(flag[i]) continue; double foo = (double)x[i]/y - (double)k[i]/m; if(t < foo){ t = foo; j = i; } } k[j] ++; } for(int i=0; i<n-1; i++) printf("%d ", k[i]); printf("%d\n", k[n-1]); } return 0;}
- ACdream 1224 Robbers (贪心)
- ACDREAM 03C Robbers(贪心专场)
- ACdream 1224 Robbers
- Robbers(贪心)
- ACdream 1224(贪心处理)
- acdream 1224(贪心)
- ACdream发电站(贪心)
- sgu207:Robbers(贪心)
- ZOJ2343 Robbers(贪心)
- zoj 2343 Robbers 【贪心】
- 1224 Robbers
- ACDREAM 03D ACdream王国的花店(贪心专场)
- ACDREAM 03E ACdream王国的士兵(贪心专场)
- ACDREAM 03G ACdream的高速公路(贪心专场)
- Robbers
- UVA 1616 Caravan Robbers(二分+贪心)
- acdream 1073(贪心)
- acdream 1714(贪心)
- LeetCode Reverse Integer
- tcp四种定时器
- AngularJS 从入门到精通(一 过滤器 Filter)
- h264编译通不过问题
- runtime 运行时机制 完全解读
- ACdream 1224 Robbers (贪心)
- QuerySet API
- 自制Xcode插件——XcodeCareer:统计真实输入代码行数和时长
- JAVA中接口存在的意义
- Oracle使用递归获得首级和末级的内容
- DHCP分配释放过程
- 【算法】判断点在多边形内部
- 代理模式
- 百度之星初赛2 棋盘占领