https://www.hackerrank.com/contests/101hack41/challenges/washing-plates

给定n个物品,选这个物品,贡献 + p, 不选的话,贡献 - d

问最大贡献。

考虑贪心。优先选最好的k件。什么是最好呢。

把每个物品的p + d作为权值排序,最大的就是最好的。

因为要使得p + d最大,必然是p或者d是很大的,或者都大。这两个值对答案都有相同意义的贡献。就是你能选的话就能加很多,不选的话,就会减很多。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e5 + ;
struct node {
LL val;
LL p, d;
int id;
}tosort[maxn];
bool cmp(node a, node b) {
return a.val > b.val;
}
void work() {
int n, k;
cin >> n >> k;
for (int i = ; i <=n; ++i) {
LL p, d;
cin >> p >> d;
tosort[i].val = p + d;
tosort[i].id = i;
tosort[i].p = p;
tosort[i].d = d;
}
sort(tosort + , tosort + + n, cmp);
LL sum = ;
for (int i = ; i <= k; ++i) {
sum += tosort[i].p;
}
for (int i = k + ; i <= n; ++i) {
sum -= tosort[i].d;
}
sum = max(sum, 0LL);
cout << sum << endl;
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

Washing Plates 贪心的更多相关文章

  1. 第十五周 Leetcode 517. Super Washing Machines(HARD) 贪心

    Leetcode517 很有趣的一道题 由于每一步可以任选某些数字对它们进行转移,所以实际上是在求最优解中的最复杂转移数. 那么我们考虑,到底哪一个位置要经过的流量最大呢? 枚举每个位置,考虑它左边的 ...

  2. 2016 CCPC-Final-Wash(优先队列+贪心)

                  Wash Mr.Panda is about to engage in his favourite activity doing laundry! He’s brought ...

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

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

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

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

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

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

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

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

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

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

  8. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  9. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

随机推荐

  1. 从生成文件对比两种创建虚拟机的方式:boot from image和boot from bootable-volume

    1. 创建bootable-volume(参考:http://docs.openstack.org/grizzly/openstack-compute/admin/content/instance-c ...

  2. Python-Redis的Hash操作

    Redis的hash是一个string类型的field和value的映射表,特别适合用于存储对象,每个hash可以存储40多亿键值对 hset(name, key, value):创建一个name的类 ...

  3. QTP使用outlook发送邮件

    '发邮件 Dim objOutlook  Dim objOutlookMsg Dim olMailItem  ' Create the Outlook object and the new mail ...

  4. popup的简单应用举例

    一.首先说一下自执行函数 1. 立即执行函数是什么?也就是匿名函数 立即执行函数就是 声明一个匿名函数 马上调用这个匿名函数 2.popup的举例 点击,弹出一个新的窗口.保存完事,页面不刷新数据就返 ...

  5. HTML input 标签不可编辑的 readonly 属性

    1 <form action="form_action.asp" method="get"> Name:<input type="t ...

  6. linux命令-vim一般模式下光标移动

    vim 有一般模式,编辑模式,命令模式 ///////一般模式可以光标移动,复制,剪切,粘贴     编辑模式可以输入想输入的字符       命令模式刚才用到了set nu //////////// ...

  7. Spring IOC 巨多 非常 有用

    关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 <Spring入门经典>这本书无论对于初学者或者有 ...

  8. php学习笔记-变量的作用域

    这个东西很难理解,但很重要,我觉得非常容易出错. PHP中的变量按照作用域分为有两种,一种是global,一种是local. 函数内部声明的变量就叫local型变量,只能在函数内部被访问到.一句话,l ...

  9. 20、BLAST比对及结果介绍

    1.formatdb -i /share/nas1/huangt/project/IsoSeq/BMK170104-E545-03-a/Analysis_T01/MoveRebundant/T01/c ...

  10. p4171&bzoj1823 满汉全席

    传送门(洛谷) 传送门(bzoj) 题目 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能 ...