ACdream 1224 Robbers (贪心)
2015-06-01 09:32
288 查看
Robbers
Special JudgeTime Limit: 2000/1000MS (Java/Others)
Memory Limit: 128000/64000KB (Java/Others)
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 turnedout 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
The first line of the input file contains numbers N, M and Y (1 ≤ N ≤ 1000, 1 ≤ M, Y ≤ 10000). N integer numbers follow - Xi (1 ≤ Xi ≤ 10000, sum of all Xi is Y).Output
Output N integer numbers - Ki (sum of all Ki must be M), so that the total unfairness is minimal.Sample Input
3 10 4 1 1 2
Sample Output
2 3 5
Source
Andrew Stankevich Contest 2Manager
mathlover解析:先对每个人都按比例向下取整得到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; }
相关文章推荐
- eval、json.parse()的介绍和使用注意点
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
- Esper专栏介绍
- LeetCode String to Integer (atoi)
- (HDU-5246)2015Astar-初赛-1001-超级赛亚ACMer
- [转载] 运维平台规划体系全介绍
- oracle判断一个字符串中是否包含另外一个字符串
- notepad++什么快捷键可以选择一行?
- css :before :after
- php用于URL的base64
- 一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
- 栈与队列
- Maven Java EE Configuration Problem问题
- 百度之星初赛2 棋盘占领
- C语言前五章总结
- dialog
- 已收录的帝国cms文章被误删除了怎么办?
- 白板编程浅谈——Why, What, How
- 代理模式
- SVN实用经验贴(转)