题意

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

思路

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

然后 比较每一个人的负担额度 与 平均值的关系,如果小于等于平均值 那么它就出这个负担额度的钱,如果大于平均值,就出平均值的钱,然后这个平均值也是在变的 每次更新之后 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. c#调用WinRAR软件压缩和解压文件

    using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...

  2. Iocomp控件教程之LinearGauge--线性刻度尺控件

    线性刻度尺-线性刻度尺控件(LinearGauge)是一个具有线性表达式刻度的图像控件.支持多达5种颜色断面和4种指示器样式,相同功能,查看线性对数刻度尺(Linear Log Gauge)控件内容 ...

  3. excel批量取消隐藏工作表

    按下"Alt+F11"键,在打开的"Microsoft Bisual Basic"窗口中,选择"插入——模块".,复制下面的代码,按F5键运 ...

  4. Tautology - poj 3295

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10437   Accepted: 3963 Description WF ...

  5. Java -- 数字

    @.运用BigDecimal处理Double类型的算术运算的精度问题 原文:https://blog.csdn.net/j754379117/article/details/52238396 可使用 ...

  6. json性能测试

    http://www.open-open.com/lib/view/open1434377191317.html

  7. php != 和 !== 的区别

    == and != do not take into account the data type of the variables you compare. So these would all re ...

  8. KiB、MiB与KB、MB的区别

    原来没太注意MB与MiB的区别,甚至没太关注还有MiB这等单位,今天认真了一下,发现两者还是有区别的,具体的差别是MB等单位以10为底数的指数,MiB是以2为底数的指数,如:1KB=10^3=1000 ...

  9. Yii2实用基础学习笔记(二):Html助手和Request组件 [ 2.0 版本 ]

    Html助手 1 .在@app\views\test的index.php中: <?php //引入命名空间 use yii\helpers\Html; ?> <?php //[一]表 ...

  10. html5小趣味知识点系列(一)contentEditable

    在这里纠正一下某些书籍说 这个修改后的文字内容是无法保存的 的错误必须发送到服务器进行保存才可以(因为我可以保存到内容)看代码吧  也许我理解的不对 <!DOCTYPE html> < ...