Kattis - fairdivision 【贪心】
题意
有一堆人 要给他们的朋友 买一个生日礼物,然后 每个人 给出自己的最大负担额度 并且给出礼物总价 然后要给出一种解决方案 尽量让 所有人的支出都接近平均,如果实在无法平均,那就让 先来的人 多处
思路
用贪心的思路,我们先将负担额度排序,第一个关键字是负担额度,第二个关键字是进来的先后次序,按双关键字排序,先按负担额度从小到大,若负担额度相等,再按进来的先后次序从大到小
然后 比较每一个人的负担额度 与 平均值的关系,如果小于等于平均值 那么它就出这个负担额度的钱,如果大于平均值,就出平均值的钱,然后这个平均值也是在变的 每次更新之后 sum - 这个人出的钱 并且 人数 - 1
然后 每次更新平均值 ,这样就能保证 在不平均的情况下,后面的人出的钱多
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
const int MOD = 1e9 + 7;
struct Node
{
int num, id;
}q[maxn];
bool comp(Node x, Node y)
{
if (x.num == y.num)
return x.id > y.id;
return x.num < y.num;
}
bool cmp(Node x, Node y)
{
return x.id < y.id;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int p, n;
scanf("%d%d", &p, &n);
int sum = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &q[i].num);
q[i].id = i;
sum += q[i].num;
}
if (sum < p)
printf("IMPOSSIBLE");
else if(sum == p)
{
for (int i = 0; i < n; i++)
{
if (i)
printf(" ");
printf("%d", q[i].num);
}
}
else
{
sort(q, q + n, comp);
sum = p;
int ave;
for (int i = 0; i < n; i++)
{
ave = sum / (n - i);
q[i].num = min(ave, q[i].num);
sum -= q[i].num;
}
sort(q, q + n, cmp);
for (int i = 0; i < n; i++)
{
if (i)
printf(" ");
printf("%d", q[i].num);
}
}
printf("\n");
}
}
Kattis - fairdivision 【贪心】的更多相关文章
- Installing Apps Kattis - installingapps (贪心 + 背包)
Installing Apps Kattis - installingapps Sandra recently bought her first smart phone. One of her fri ...
- Kattis - entertainmentbox 【贪心】
思路 先将 N 个 电视节目 排序 根据 结束时间 ,结束的早的 排在前面 然后 弄 K个标记 记录 结束时间 然后 遍历一下 每次 如果能插入的话 插入到 结束时间最小的那个 队列里面去然后 每次插 ...
- Kattis - horrorfilmnight 【贪心】
题意 有两个人想去一起看电影,然后分别给出两个人 分别喜欢看的电影都在哪些天 然后 同一个人 不能连续看两天他不喜欢的电影 求他们最多可以看多少次电影 思路 先将两人喜欢看的电影进行排序, ① 选择两 ...
- 2016 acm香港网络赛 C题. Classrooms(贪心)
原题网址:https://open.kattis.com/problems/classrooms Classrooms The new semester is about to begin, and ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
随机推荐
- Mac 下Versions的 svn无法上传 .a 文件的问题
Xcode自带的svn和Versions以及一些其它工具都默认ignore".a"文件. 解决办法有两个: 方法一:使用命令行添加文件([转]原文在这) 1.打开终端,输入cd,空 ...
- CSS遮罩层,全兼容
<script type="text/javascript"> $(function(){ $('#divLocker').css({ "position&q ...
- C#:异步编程和线程的使用(.NET 4.5 ),异步方法改为同步执行
摘自:http://www.codeproject.com/Articles/996857/Asynchronous-programming-and-Threading-in-Csharp-N(葡萄城 ...
- list集合转换成json类型
public String gettext(HttpServletRequest request,HttpServletResponse response){ List<xuanhuan_> ...
- Atitit.rust语言特性 attilax 总结
Atitit.rust语言特性 attilax 总结 1. 创建这个新语言的目的是为了解决一个顽疾:软件的演进速度大大低于硬件的演进,软件在语言级别上无法真正利用多核计算带来的性能提升.1 2. 不会 ...
- MQTT服务器搭建--Apollo
尊重原创,我是伸手党:https://blog.csdn.net/u012377333/article/details/68943416 1.Apollo下载 下载地址:http://activemq ...
- Arcgis:什么是栅格数据类型
栅格数据单元大小 栅格所表示的内容的详细程度(要素现象)通常取决于栅格的单元(像素)大小或空间分辨率. 单元必须足够小,这样才可以捕获到所需的详细信息: 而单元又必须足够大,这样才可以提高计算机存储和 ...
- 13 Memcached 永久数据被踢现象
一:Memcached 永久数据被踢现象(1)网上有人反映"memcached"数据丢失,明明设为永久不失效,却莫名其妙的丢失了. 其实这要从2个方面来找原因. 即使前面介绍的惰性 ...
- mysql windows安装
http://blog.csdn.net/tossgoon/article/details/44412491 1.从该地址http://dev.mysql.com/downloads/mysql/中选 ...
- git add -A使用说明
git help add -A, --all Like -u, but match <filepattern> against files in the workin ...