ACDREAM 03C Robbers(贪心专场)
来源:互联网 发布:mac上如何装office 编辑:程序博客网 时间:2024/06/06 00:44
ACDREAM 03C 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
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
题目大意:每组数据包含两个部分,第一行N, M, Y,第二行是N个整数X1 ~ XN。题目给出了一个公式:|Xi/Y−Ki/M| , 要求求出使这个式子的值的和最小的N个k的值(k的和要为M)。
解题思路:将|Xi/Y−Ki/M| 转换成s[i]=(M∗Xi)/Y , 最后将s[i]相加得出sum,比较sum与M的大小,作出相应操作(具体见代码)。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#define N 1005using namespace std;typedef long long ll;int K[N];double S[N];struct rec{ int id; double num;}r[N];int cmp1(rec a, rec b) { return a.num < b.num;}int cmp2(rec a, rec b) { return a.num > b.num;};int main() { int n; while (scanf("%d", &n) != EOF) { int M, Y; scanf("%d %d", &M, &Y); int X, sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &X); S[i] = (M * X) / Y; K[i] = (int)S[i]; if (1 - (S[i] - K[i]) < S[i] - K[i]) { K[i]++; } r[i].num = abs((S[i] + 1.0) / M - X * 1.0 / Y) - abs(S[i] * 1.0 / M - X * 1.0 / Y); r[i].id = i; sum += K[i]; } if (sum < M) { int temp = M - sum; sort(r, r + n, cmp1); for (int i = 0; i < temp; i++) { K[r[i].id]++; } } else if (sum > M) { int temp = sum - M; sort(r, r + n, cmp2); for (int i = 0; i < temp; i++) { K[r[i].id]--; } } printf("%d", K[0]); for (int i = 1; i < n; i++) { printf(" %d", K[i]); } printf("\n"); } return 0;}
- ACDREAM 03C Robbers(贪心专场)
- ACdream 1224 Robbers (贪心)
- ACDREAM 03D ACdream王国的花店(贪心专场)
- ACDREAM 03E ACdream王国的士兵(贪心专场)
- ACDREAM 03G ACdream的高速公路(贪心专场)
- ACDREAM 03A 雷霆战机(贪心专场)
- ACDREAM 03B New Year Bonus Grant(贪心专场)
- Robbers(贪心)
- XMUT acdream 数据结构专场 C题(单调栈)
- ACdream 1224 Robbers
- ACDREAM 02C 小晴天老师系列——竖式乘法(暴力专场)
- ACDREAM 05C 哗啦啦族的24点游戏(DFS专场)
- ACdream发电站(贪心)
- ACdream 暴力专场B
- ACdream 暴力专场F
- Acdream的暴力专场
- XMUT acdream DP专场
- sgu207:Robbers(贪心)
- day17/CollectionsDemo2.java
- 剑指offer 面试题16—反转链表
- vb.net环境下将DataTable转成泛型集合方法
- day17/CollectionsDemo3.java
- Java基础——多线程+卖票程序+同步函数-单例设计模式+死锁
- ACDREAM 03C Robbers(贪心专场)
- 黑马程序员---------------JAVA基础--------正则表达式
- day17/CollectionsDemo4.java
- 面对象
- JSP标准动作、EL表达式和JSTL
- OC--内存管理(1)
- 字符串的填充、复制与加密和解密
- day17/CollectionToArray.java
- 命令行的简单使用