https://cn.vjudge.net/contest/158742#problem/G
来源:互联网 发布:淘宝哪里看信誉评价 编辑:程序博客网 时间:2024/09/21 08:55
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.
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 N integer numbers - Ki (sum of all Ki must be M), so that the total unfairness is minimal.
1
3 10 4
1 1 2
2 3 5
分析:这里的贪心策略是,先向下取整着分给每个人。然后,多余的金币,每次分给不公平度最大的人。
代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1111;
int x[maxn];
int flag[maxn];
int t,n,m,y;
int main()
{
scanf("%d",&t);
while(t--)
{
printf("\n");
scanf("%d%d%d",&n,&m,&y);
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&x[i]);
k[i]=m*x[i]/y; //取整;
sum+=k[i];
}
int ans=m-sum;
while(ans--)
{
double tmp=0.0;
int j;
for(int i=0;i<n;i++)
{
double p=x[i]*1.0/y-k[i]*1.0/m;
if(p>tmp)tmp=p,j=i;
}
k[j]++;
}
for(int i=0;i<n;i++)
printf("%d%c",k[i],i==n-1?'\n':' ');
}
}
- https://cn.vjudge.net/contest/158742#problem/G
- 搜索练习9 https://cn.vjudge.net/contest/81658#problem/J Fire! bfs
- https://vjudge.net/contest/161167#problem/F——dp
- Oil Deposits 油田,深搜 http://vjudge.net/contest/view.action?cid=49515#problem/G
- Kids and Prizes 来源: <http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85996#problem/B>
- 还是畅通工程 http://vjudge.net/contest/view.action?cid=49515#problem/D
- Calendar 日历 http://vjudge.net/contest/view.action?cid=49515#problem/I
- 例题1.15 网络 https://vjudge.net/problem/UVALive-3902 树上的搜索
- Codeforces contest 802 problem G
- Vjudge Prime Ring Problem (dfs)
- 2013 Multi-University Training Contest 3 (g) The Unsolvable Problem
- Vjudge
- https://projecteuler.net/problem=26
- https://projecteuler.net/problem=1
- https://projecteuler.net/problem=2
- https://projecteuler.net/problem=3
- https://projecteuler.net/problem=4
- https://projecteuler.net/problem=5
- 找出字符串的最长不重复子串,输出长度
- C++友元函数和友元类
- python-集合
- images2张图片合并
- Java的反射机制
- https://cn.vjudge.net/contest/158742#problem/G
- CTFgif图片
- 最小生成树 最短路 并查集
- 树莓派3入手环境搭建(QT+OpenCV)
- Java并发编程实战 之 线程的取消与关闭
- JAVA 解析Xml字符串
- 我的第一个html
- Vue.js Webpack warning
- java切蛋糕