Robbers
2015-06-07 09:52
253 查看
Robbers
Time Limit: 2000/1000MS(Java/Others) Memory
Limit: 128000/64000KB
(Java/Others)
Special Judge
Submit Status
Problem Description
N robbershave 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 firstline 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 Ninteger 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
强盗分钱,要求是不公平度最小,那么先按原来分配取整,对每个强盗最优解肯定是a[i]*m/y或是加一,然后再把他们加一后不公平度增加进行排序,选最小的分别加一,把剩下的硬币分配完。自己写的时候比较2比的先进行排序了,wa了好久。还是写的c语言,用sort会简单许多。
#include
#include
#include
struct robber{
int id;
int a;
double dis;
}dp[1010];
int ans[1010];
int main()
{
int m, n, i, j, y, sum1, sum2, leap, pre,
temp;
double temp1;
while (scanf("%d %d %d", &n, &m, &y)
!= EOF){
sum1 = 0;
for (i = 1; i <= n;
i++)
{
scanf("%d",
&dp[i].a);
dp[i].id =
i;
}
//没有必要排序
for (i = 1; i <= n;
i++)
{
ans[i] =
m*dp[i].a / y;
sum1 +=
ans[i];
}
for (i = 1; i <= n;
i++)
{
dp[i].dis =
-fabs(dp[i].a*1.0 / y - ans[dp[i].id] * 1.0 / m) + fabs(dp[i].a*1.0
/ y - (ans[dp[i].id] + 1) *1.0 / m);
}
for (i = 1; i <=
n;i++)
for (j = 1; j <= n -
i;j++)
if (dp[j].dis > dp[j +
1].dis)
{
temp =
dp[j].a; dp[j].a = dp[j + 1].a; dp[j + 1].a = temp;
temp =
dp[j].id; dp[j].id = dp[j + 1].id; dp[j + 1].id = temp;
temp1 =
dp[j].dis; dp[j].dis = dp[j + 1].dis; dp[j + 1].dis = temp1;
}
for (i = 1; i <= m - sum1;
i++)
ans[dp[i].id]++;
for (i = 1; i <= n;
i++)
printf("%d ",
ans[i]);
printf("\n");
}
return 0;
}
相关文章推荐
- hiho一下 第四十九周
- css中的relative(相对位置)和absolute(绝对位置)
- 全面SSH 远程操作与端口转发,远程copy
- dfsd
- 二进制暴力
- NYOJ 47 过河问题
- wamp 中安装cakephp Fatal error: You must enable the intl extension to use CakePHP. in XXX
- ASP.NET中集成百度编辑器UEditor
- C++模板中的静态
- 如何直接访问WEB-INF下列文件
- NGS项目四:高通量测序在植物生物胁迫研究中的应用
- Compiling... ,Error spawning cl.exe 解决方案——VC6环境变量配置
- 设计一个中间件的访问日志组件
- 折半查找
- python 2 给我自己看
- 设计一个中间件的访问日志组件
- Class文件内容及常量池(转)
- java获得平台相关的行分隔符和java路径分隔符的方法
- FastDFS概要
- 一起talk C栗子吧(第十一回:C语言实例--文件组织结构)