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. Asp.net工作流workflow实战(一)

    最近开发一个项目用到了工作流引擎,之前研究过微软的workflow所有就用它了,距离上次用有一段时间了,好多东西有点模糊了,就在此处一遍写代码一遍回忆. 首先,在我的项目中新建了.netframwor ...

  2. virtual judge(专题一 简单搜索 E)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  3. Python:正则表达式(二)

    则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re 模块使 Python 语 ...

  4. 【转】 Pro Android学习笔记(二一):用户界面和控制(9):Gallery和SimpleAdapter

    Gallery画廊式控件,如图所示,但是在API level 16,也即Android 4.1,被deprecated,可以使用HorizontableScroolView和ViewPager.但是后 ...

  5. 人物-IT-史玉柱:史玉柱

    ylbtech-人物-IT-史玉柱:史玉柱 史玉柱,1962年9月15日生于安徽省蚌埠市怀远县,商人.企业家. 1984年从浙江大学数学系本科毕业,分配至安徽省统计局工作.1989年深圳大学软件科学系 ...

  6. (转)64位系统安装Delphi7提示Can’t load package:dclite70.bpl 以及 提示地址错误

    第一个问题: 今天在64的Win7上安装Delphi7,在启动时候出现如下提示: Can't load package:dclite70.bpl 告诉大家一个解决办法,就是给Delphi32.exe去 ...

  7. Learning Python 003 缩进

    Python 缩进 Python的代码中不使用{}大括号来来表示一个代码块,而是使用缩进方式.像下面这段代码: # print absolute value of an integer: a = 10 ...

  8. Multi-task Pose-Invariant Face Recognition 论文笔记

    摘要: 在不受限制的环境中拍摄的人脸图像通常包含显著的姿态变化,这会显著降低设计用于识别正面的算法的性能.本文提出了一种新颖的面部识别框架,能够处理±90°偏航范围内的全方位姿势变化.所提出的框架首先 ...

  9. jquery ajax + struts2用例

    ajax var url = '/itsm/contract/contract!deleteShopItemById.action'; var shopItemId= selectRows[x].da ...

  10. 支持Mono的盘古分词(PanGu)

    不废话,直接上下载地址http://files.cnblogs.com/files/RainbowInTheSky/PanGu.rar 群友在做项目移植到Mono+jexus上时遇到了问题,盘古分词无 ...