Summer sell-off CodeForces - 810B (排序后贪心)
Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying to get a part-time job. This summer Noora decided that she wants to earn some money, and took a job in a shop as an assistant.
Shop, where Noora is working, has a plan on the following n days. For each day sales manager knows exactly, that in i-th day ki products will be put up for sale and exactly li clients will come to the shop that day. Also, the manager is sure, that everyone, who comes to the shop, buys exactly one product or, if there aren't any left, leaves the shop without buying anything. Moreover, due to the short shelf-life of the products, manager established the following rule: if some part of the products left on the shelves at the end of the day, that products aren't kept on the next day and are sent to the dump.
For advertising purposes manager offered to start a sell-out in the shop. He asked Noora to choose any f days from n next for sell-outs. On each of f chosen days the number of products were put up for sale would be doubled. Thus, if on i-th day shop planned to put up for sale ki products and Noora has chosen this day for sell-out, shelves of the shop would keep 2·ki products. Consequently, there is an opportunity to sell two times more products on days of sell-out.
Noora's task is to choose f days to maximize total number of sold products. She asks you to help her with such a difficult problem.
Input
The first line contains two integers n and f (1 ≤ n ≤ 105, 0 ≤ f ≤ n) denoting the number of days in shop's plan and the number of days that Noora has to choose for sell-out.
Each line of the following n subsequent lines contains two integers ki, li (0 ≤ ki, li ≤ 109) denoting the number of products on the shelves of the shop on the i-th day and the number of clients that will come to the shop on i-th day.
Output
Print a single integer denoting the maximal number of products that shop can sell.
Examples
4 2
2 1
3 5
2 3
1 5
10
4 1
0 2
0 3
3 5
0 6
5
Note
In the first example we can choose days with numbers 2 and 4 for sell-out. In this case new numbers of products for sale would be equal to [2, 6, 2, 2] respectively. So on the first day shop will sell 1 product, on the second — 5, on the third — 2, on the fourth — 2. In total 1 + 5 + 2 + 2 = 10 product units.
In the second example it is possible to sell 5 products, if you choose third day for sell-out.
夏天到了,商店要进行促销活动。
商店在夏天中营业 n 天。每天出售的商品数已经确定,第 i 天出售 ki 件商品,有 li 个客户回来购买。已知每个客户恰好购买一件商品。如果没有商品可以购买,那客户就会选择空手离开。
由于要进行促销,你可以选择 f 天将出售的商品数翻倍,但是客户的数量不会翻倍。现在让你求出最多能卖出多少件商品。
思路:先把每一天的不翻倍能卖出多少个商品加入到ans中,然后再开一个数组(其实用大根堆最方便)来记录每天如果这一天翻倍的话,能多卖出多少商品。
然后把这个新数组排序,然后取前f个加入到ans中。
细节见我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== "<<x<<" =="<<endl;
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,f;
int k[maxn];
int l[maxn];
int a[maxn];
// priori
int main()
{
gbtb;
cin>>n>>f;
ll ans=0ll;
int cnt=;
repd(i,,n)
{
cin>>k[i]>>l[i];
if(k[i]==)
{ }else if(k[i]>=l[i])
{
ans+=l[i];
}else
{
ans+=k[i];
a[cnt++]=min(k[i]*,l[i])-k[i];
}
}
// cout<<ans<<endl;
// db(ans);
sort(a,a+cnt);
for(int i=cnt-;i>=;i--)
{
if(f==)
{
break;
}
ans+=a[i];
// cout<<a[i]<<" == "<<endl;
f--; }
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Summer sell-off CodeForces - 810B (排序后贪心)的更多相关文章
- Uva11292--------------(The Dragon of Loowater)勇者斗恶龙 (排序后贪心)
---恢复内容开始--- 题目: Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major ...
- Problem #3263 丽娃河的狼人传说 区间满足灯数,r排序后贪心。
丽娃河的狼人传说 Time limit per test: 1.0 seconds Time limit all tests: 1.0 seconds Memory limit: megabytes ...
- uva11292 Dragon of Loowater(排序后贪心)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- Codeforces C. Elections(贪心枚举三分)
题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 830A. Office Keys (贪心二分 or DP)
原题链接:http://codeforces.com/contest/830/problem/A 题意:在一条数轴上分别有n个人和k把钥匙(n<=k),以及一个目的地,每个人要各自拿到一个钥匙后 ...
- codeforces 480A A. Exams(贪心)
题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 1100F(线性基+贪心)
题目链接 题意 给定序列,$q(1\leq q \leq 100000) $次询问,每次查询给定区间内的最大异或子集. 思路 涉及到最大异或子集肯定从线性基角度入手.将询问按右端点排序后离线处理询问, ...
- CodeForces - 1253C(思维+贪心)
题意 https://vjudge.net/problem/CodeForces-1253C n个糖果,一天最多吃m个糖果,每个糖果有个值a[i],第d天会变成d*a[i],问吃k(k=1~n)个糖果 ...
- 将1~n个整数按字典顺序进行排序,返回排序后第m个元素
给定一个整数n,给定一个整数m,将1~n个整数按字典顺序进行排序,返回排序后第m个元素.n最大可为5000000.字典排序的含义为:从最高位开始比较.1开头的数字排在最前面,然后是2开头的数字,然后是 ...
随机推荐
- python之bytes和string
转自:https://www.cnblogs.com/skiler/p/6687337.html 1.bytes主要是给在计算机看的,string主要是给人看的 2.中间有个桥梁就是编码规则,现在大趋 ...
- python中的猴子补丁Monkey Patch
python中的猴子补丁Monkey Patch 什么是猴子补丁 the term monkey patch only refers to dynamic modifications of a cla ...
- python 实现网页 自动登录
完整代码: 1 from apscheduler.schedulers.blocking import BlockingScheduler 2 from selenium import webdriv ...
- Windows单机最大TCP连接数的问题
本文和大家分享一下Windows下单机最大TCP连接数,因为在做Socket 编程时,我们经常会要问,单机最多可以建立多少个 TCP 连接,本文将介绍如何调整系统参数来调整单机的最大TCP连接数. W ...
- March 02nd, 2018 Week 9th Friday
Make hay while the sun shines. 勿失良机. Last night the toothache woke me up and it was very difficult f ...
- C语言的main函数到底该怎么写
公众号[编程珠玑]:专注但不限于分享计算机编程基础,Linux,C语言,C++,Python,数据库等编程相关[原创]技术文章,号内包含大量经典电子书和视频学习资源.欢迎一起交流学习,一起修炼计算机“ ...
- 一台电脑安装两个JDK
起因:由于嫌自己电脑东西太乱,在上个学期重新格式化整理了一下.下载的jdk也为当时最新的10版本,上次在买jsp的虚拟主机时候也遇到了这个问题,对方提供的jdk只有7版本的,我是10版本的,所以当时打 ...
- Mysql 关键字的优先级 分组 多表联查
查看模式 select @@global.sql_mode; 关键字的优先级 from 来自 where 条件 group by 分组 having 筛选 select 查询 distinct 去重 ...
- 学习任务,阅读一下Redis分布式锁的官方文档
地址: https://redis.io/topics/distlock 这是一篇质疑RedLock的论文:https://martin.kleppmann.com/2016/02/08/how-to ...
- P2802 回家 (DFS+剪枝)
这里详细讲一下剪枝的点: 因为,可以重复在同一个点上走动.所以,这个步数是无穷的. 剪枝一:步数< n*m; (因为起点不算所以不取等号) 剪枝二:步数当大于已有的答案时,直接退出DFS, ...