Washing Plates 贪心
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 贪心的更多相关文章
- 第十五周 Leetcode 517. Super Washing Machines(HARD) 贪心
Leetcode517 很有趣的一道题 由于每一步可以任选某些数字对它们进行转移,所以实际上是在求最优解中的最复杂转移数. 那么我们考虑,到底哪一个位置要经过的流量最大呢? 枚举每个位置,考虑它左边的 ...
- 2016 CCPC-Final-Wash(优先队列+贪心)
Wash Mr.Panda is about to engage in his favourite activity doing laundry! He’s brought ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
随机推荐
- 关于Snoop的用法
snoop是开发wpf应用程序的利器.用它可以观察WPF的可视树,监听事件,更改元素属性等. 下面我介绍下snoop一些用法. 1.获取指定应用程序的UI 打开snoop,选择"Drag ...
- Poj 2017 Speed Limit(水题)
一.Description Bill and Ted are taking a road trip. But the odometer in their car is broken, so they ...
- openstackM版本安装
部署期间常见问题:http://www.cnblogs.com/bfmq/p/6001233.html,问题跟对架构的理解永远比部署重要!你玩技术是绝对是要基于理论的 一.基本情况:物理设备:4台惠普 ...
- vs2015类中方法前的引用链接不显示的解决方案
在工具→选项,打开如下界面,寻找“文本编辑器→所有语言”中设置显示:<img data-rawheight="761" data-rawwidth="130 ...
- USB插拔检测程序
一.手动添加ON_WM_DEVICECHANGE()消息 二.添加头文件#include <Dbt.h> 三.定义设备的GUID static const GUID GUID_DEVINT ...
- shell script-条件语句、循环语句
条件语句 #!/bin/bash read -p "input your name:" name #第一种判断 if [ "$name" == "Mi ...
- JavaScript正则表达式应用---replace()
replace()方法使用一个替换值(replacement)替换掉一个匹配模式(pattern)在原字符串中某些或所有的匹配项,并返回替换后的字符串.这个替换模式可以是字符串或者RegExp(正则表 ...
- Struts2学习第七课 动态方法调用
动态方法调用:通过url动态调用Action中的方法. action声明: <package name="struts-app2" namespace="/&quo ...
- Boost Python学习笔记(一)
开发环境搭建 下载源码 boost_1_66_0.tar.gz 生成编译工具 # tar axf boost_1_66_0.tar.gz # cd boost_1_66_0 # yum install ...
- Mysql索引优化1
Btree索引和哈希索引(索引是一种数据结构,提高查询,排序,分组速度) Btree索引的数据结构是平衡二叉树,时间复杂度为lgN 哈希索引的数据结构是一个Hash函数,时间复杂度为O(1),数据输入 ...