题意

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

思路

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

然后 比较每一个人的负担额度 与 平均值的关系,如果小于等于平均值 那么它就出这个负担额度的钱,如果大于平均值,就出平均值的钱,然后这个平均值也是在变的 每次更新之后 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. Ubuntu14.04下MySQL的安装与卸载

    转载自:https://www.2cto.com/os/201408/329502.html 安装MysQL 执行以下命令:sudo apt-get install mysql-server 2. 继 ...

  2. asp.net core mvc视频A:笔记2-3.高级数据绑定

    默认的绑定顺序,如果需要取指定数据源里的数据,需要通过属性控制,比如[FromQuery] 前端 控制器方法 前端 此时并不能得到head中的数据 改造控制器方法,添加[FromHeader]属性 再 ...

  3. different between method and function

    A method is on an object. A function is independent of an object. For Java, there are only methods. ...

  4. java 安装后 不能 java javac 说找不到命令 -bash: javac: command not found

    java 安装后 不能 java javac  说找不到命令 -bash: javac: command not found 不是环境变量的问题, 直接cd到java的目录 也不能执行命令 后来发现是 ...

  5. surface4 笔盖失灵的解决方案

    http://tieba.baidu.com/p/3670357234 先找到设备管理器,找到蓝牙,删除里面所有的设备.然后重启. 之后再次找到蓝牙,匹配pen.就可以用了. 解决的前提是:我确定笔帽 ...

  6. java游戏开发基础Swing之JCheckBox

    © 版权声明:本文为博主原创文章,转载请注明出处 1.复选框(JCheckBox) 使用复选框可以完成多项选择.Swing中的复选框与AWT中的复选框相比,优点是Swing复选框中可以添加图片 JCh ...

  7. Unity动态字体在手机上出现字体丢失问题解决

    在我们游戏的开发过程中,在部分手机上运行游戏的时候,出现了字体丢失的问题,出问题的手机似乎用的都是高通芯片. 使用的unity是4.2.0版本,ngui是3.4.9版本. 在unity的论坛及unit ...

  8. APICloud打包Vue单页应用

    APICloud新建项目后,会生成以下目录结构 其中index.html是入口文件,而vue-cli打包生成的文件是在dist目录下 ├─dist│ └─static│ ├─css│ └─js │ └ ...

  9. oracle中can not set解决方法

    原因:set autotrace on和set trimspool on在pl\sql中使用不了 解决方法:在window环境中,使用cmd命令,sqlplus user_name/password@ ...

  10. Foundation框架 - NSDictionary类、NSMutableDictionary类

    NSArray.NSSet.NSDictionary /* 集合 1.NSArray\NSMutableArray * 有序 * 高速创建(不可变):@[obj1, obj2, obj3] * 高速訪 ...