题目:

Robbers Time Limit: 5 Seconds Memory Limit: 32768 KB Special
Judge 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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line
followed by N input blocks. Each input block is in the format
indicated in the problem description. There is a blank line between
input blocks.

The output format consists of N output blocks. There is a blank line
between output blocks.

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

1

3 10 4 1 1 2

Sample Output

2 3 5

Author: Andrew Stankevich Source: Andrew Stankevich’s Contest #2
Submit Status

思路

题目给了Y的值和M的值,然后有n个数据,分别代表x1 x _ 1 ,x2 x _ 2 …xn x _ n
然后题目问的意思是要使

|x1Y \frac{x _ 1 }{Y} -k1M\frac{k_1}{M}|+|x2Y \frac{x _ 2 }{Y} -k2M\frac{k_2}{M}|+…+|xnY \frac{x _ n}{Y} -knM\frac{k_n}{M}|的值最小

让你输出k1,k2,...,knk_1,k_2,...,k_n的值

这里我们要使他们的绝对值最小,那么我们令|xiY \frac{x _ i}{Y} -kiM\frac{k_i}{M}|=0,可以得到ki=M∗xiYk_i=\frac{M*xi}{Y},我们知道MY\frac{M}{Y}的值是不变的,所以我们就先求出它。

然后我们把xi∗MYx_i*\frac{M}{Y}的值求出来,因为是一个浮点数,所以我们把整数部分和小数部分分别存储,看整数部分的和和M的差距是多少,这时我们用到贪心思路,我们按照小数部粉从大到小排序,然后在那个差距范围里,给xi∗MYx_i*\frac{M}{Y}的每一位都+1,这样就可以得出我们要的kik_i的值,最后按照题目给出的顺序输出就行

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define debug() puts("what the fuck!!!")
#define N 100100
#define M 1000000+10
#define ll long long
using namespace std;
int n,m,y;
int a[N];
struct node
{int x,pos;double y;
}q[N];
bool cmp1(node a,node b)
{if(a.y==b.y)return a.pos<b.pos;return a.y>b.y;
}
bool cmp2(node a,node b)
{return a.pos<b.pos;
}
int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&y);for(int i=1;i<=n;i++){scanf("%d",&a[i]);}double cc=m*1.0/y;double temp;int sum=0;for(int i=1;i<=n;i++){q[i].pos=i;temp=a[i]*cc;//另他们详见的绝对值=0,然后推出ki=xi*(m/y)q[i].x=(int)temp;//取整数部分q[i].y=temp-q[i].x;//取小数部分sum+=q[i].x;//把整数部分的值加起来}sum=m-sum;//整数部分和m的值相差的位数sort(q+1,q+n+1,cmp1);//小数部分从大到小排序for(int i=1;i<=n;i++){if(sum){q[i].x++;sum--;}}sort(q+1,q+1+n,cmp2);for(int i=1;i<=n;i++){if(i>1)printf(" ");printf("%d",q[i].x);}puts("");}return 0;
}

ZOJ2343 Robbers(贪心)相关推荐

  1. ACdream 1224 Robbers (贪心)

    一道贪心题,很久前做的,代码是我以前写的. 题意:有n个抢劫者抢劫了m块金子,然后第i个人平分xi/y块金子,但是会有除不尽的情况而金子不可再分,那么每个人都有一个不满意度fabs(xi / y - ...

  2. sgu207:Robbers(贪心)

    大致翻译:nn个强盗去抢劫银行得到mm个金币,抢劫前他们先确定好了分 配方案,每个人按比例Xi/YXi/Y分配,X1+X2+..Xn=YX_1+X_2+..X_n = Y,mm可能不能 被YY整除,所 ...

  3. Zoj2343 Robbers java

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2343 题目大意:有N劫匪去抢银行,总计抢了M个金币,Y是每个劫匪 ...

  4. AC_Dream 1224 Robbers(贪心)

    题意:n个抢劫犯分别抢到的金钱是k1, k2, k3,...,一共得到的金钱是m, 但是在分钱的时候是按照x1/y, x2/y, x3/y,....的比例进行分配的!这样的话 一些抢劫犯就会觉得不公平 ...

  5. zoj 2343 Robbers 【贪心】

    题意: 给你n个金币,然后m个人,每个人有理论的分配份额.但是金币只能整Robbers个整个分. 问你怎么分才能让金币分配的最合理: 题解: 先按比例,把能分的先都分了,最后剩余的,往余数最大的上面补 ...

  6. UVA 1616 Caravan Robbers 【二分+贪心+枚举分母】

    题目链接 题意 给n个互不相包含的区间,求出一个长度的最大值,使得可以在每个区间中选出这样一个长度的子区间,这些子区间互不相交.结果用分数表示 分析 先考虑如果给定了区间长度能不能选出这样的区间.因为 ...

  7. zoj 2709 Lottery 组合数,概率,贪心 (8-F)

    题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2709 题解:  1   组合数的计算,用DP,速度又快又简洁. ...

  8. Grakn Forces 2020 D. Searchlights——贪心

    D. Searchlights Description There are n robbers at coordinates (a1,b1), (a2,b2), -, (an,bn) and m se ...

  9. 算法设计与分析第3章 贪心算法

    第4章 贪心算法 贪心算法总是作出在当前看来最好的选择.也就是说贪心算法并不从整体最优考虑,它所作出的选择只是在某种意义上的局部最优选择. 贪心算法的基本要素 1.贪心选择性质 所谓贪心选择性质是指所 ...

最新文章

  1. Uva10285 Longest Run on a Snowboard
  2. 企业即时通讯最可见的价值是效率和成本
  3. python2 http请求post、get
  4. rabbitmq异步_在Node.js中使用RabbitMQ和Tortoise进行异步消息传递
  5. PCL学习(4)——octree
  6. Mean Shift算法(2)在OpenCV上的实现目标跟踪——直方图反向投影
  7. python 实现字典树_python字典树(Trie)的实现
  8. 雷蛇雷云3无法连接服务器,雷蛇驱动安装无法访问服务器怎么办
  9. 共享单车数据集_共享单车数据的数据可视化
  10. 华为软件精英挑战赛参赛感悟
  11. 微星MS16j9鼠标面板可以移动指针,无法通过面板点击
  12. 在keil中使用bdata型可位寻址全局变量
  13. 哪款蓝牙耳机适合吃鸡?盘点2022适合苹果吃鸡的蓝牙耳机
  14. php跟踪系统调用,使用strace命令跟踪系统调用
  15. 本机配置nginx后css样式失效
  16. upc Buy an Integer#二分
  17. 苹果x微信语音十秒就断_原来苹果手机信号差是这个原因!教你4个方法,信号马上满格...
  18. 职称申报神器-职称小百科告诉你专业对应的评委会
  19. Google浏览器搜索页自定义图片
  20. 读取灰点相机图像C++

热门文章

  1. android 5.1 root权限,最新的安卓5.1.1 ROOT教程(不需要刷第三方内核)
  2. 看《墨攻》理解IoC
  3. KubeEdge环境搭建(支持网络插件flannel)
  4. py-fater-rcnn中config.py參數的調整
  5. python 将列表中的英文或者拼音转换为中文
  6. 设计字体打包_设计师都在用的艺术字体素材
  7. 手机芯片的AP、BP和CP
  8. 深度学习之迁移学习介绍与使用
  9. Google Authenticator 原理及Java实现
  10. JS中的Storage