题意

有一堆人 要给他们的朋友 买一个生日礼物,然后 每个人 给出自己的最大负担额度 并且给出礼物总价 然后要给出一种解决方案 尽量让 所有人的支出都接近平均,如果实在无法平均,那就让 先来的人 多处

思路

用贪心的思路,我们先将负担额度排序,第一个关键字是负担额度,第二个关键字是进来的先后次序,按双关键字排序,先按负担额度从小到大,若负担额度相等,再按进来的先后次序从大到小

然后 比较每一个人的负担额度 与 平均值的关系,如果小于等于平均值 那么它就出这个负担额度的钱,如果大于平均值,就出平均值的钱,然后这个平均值也是在变的 每次更新之后 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 【贪心】的更多相关文章

  1. Installing Apps Kattis - installingapps (贪心 + 背包)

    Installing Apps Kattis - installingapps Sandra recently bought her first smart phone. One of her fri ...

  2. Kattis - entertainmentbox 【贪心】

    思路 先将 N 个 电视节目 排序 根据 结束时间 ,结束的早的 排在前面 然后 弄 K个标记 记录 结束时间 然后 遍历一下 每次 如果能插入的话 插入到 结束时间最小的那个 队列里面去然后 每次插 ...

  3. Kattis - horrorfilmnight 【贪心】

    题意 有两个人想去一起看电影,然后分别给出两个人 分别喜欢看的电影都在哪些天 然后 同一个人 不能连续看两天他不喜欢的电影 求他们最多可以看多少次电影 思路 先将两人喜欢看的电影进行排序, ① 选择两 ...

  4. 2016 acm香港网络赛 C题. Classrooms(贪心)

    原题网址:https://open.kattis.com/problems/classrooms Classrooms The new semester is about to begin, and ...

  5. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  6. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. 【Excle】科学计数法快速还原

    在Excle的单元格中,如果输入大于11位的数字,结果就会以E+形式显示 如果是单个输入的话,只需要把Excle中的单元格格式设置为文本即可,然后输入就不会出现科学计数法,但是有时候是从外部导入的序号 ...

  2. http://m2eclipse.sonatype.org/sites/m2e地址更换了

    http://m2eclipse.sonatype.org/sites/m2e 更换为 https://repository.sonatype.org/content/sites/forge-site ...

  3. 无法调试存储过程,无法启动T-SQL调试

    用本机管理员或者具有SYSADMIN角色的帐号登录,不要用.,用实例名来连接

  4. MVC组件分析

    MVC组件分析   2 System.Web.Mvc V 4.0.0.0 组件分析 2.1 Routing组件(路由选择) Routing的作用就是负责分析Url Action的要求• 必须是一个公有 ...

  5. .NET CORE 2.0小白笔记(四):asp.net core输出中文乱码的问题

    问题描述:在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题. 分析解决:控制台乱码的原因是因为中文windows命令行默认编码页是gb2312,想输出中文只要把 ...

  6. Theme.AppCompat.Light无法找到问题

    使用adt开发新建一个Android app.选择支持的SDK版本号假设小于11(Android3.0)就会报例如以下错误. error: Error retrieving parent for it ...

  7. 第九章 用多线程来读取epoll模型下的client数据

    #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include &l ...

  8. spring boot 使用拦截器 实现 用户登录拦截

    登录拦截和和权限拦截实现类似   首先自定义一个[DefineAdapter]类,这个类我是用来放自定义的配置(比如 自定义请求参数,自定义拦截器等),集成WebMvcConfigurerAdapte ...

  9. 数据库操作事物的四大特性以及MySQL数据库的四种隔离级别

    1 .事物操作数据库的四大特性(ACID) 1.原子性 (Atomicity) 原子性:就是事物的所包含的所有操作,要么全部成功,要么全部失败回滚. 2.一致性 (Consistency) 一致性:简 ...

  10. Unity UGUI——提供可视功能的UI组件(Text)

    基本属性介绍 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTXJfQUhhbw==/font/5a6L5L2T/fontsize/400/fill/I0J ...