Codeforces Round #415 (Div. 2) B. Summer sell-off
1 second
256 megabytes
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 kiproducts 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.
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.
Print a single integer denoting the maximal number of products that shop can sell.
4 2
2 1
3 5
2 3
1 5
10
4 1
0 2
0 3
3 5
0 6
5
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 total1 + 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天每天的存货量和需求量,其中可以选f天使得当天的存货量翻倍,问这n天的最大销售量可以是多少?
解题思路:
这题可以分两种情况:
1 当天的存货量大于等于需求量的时候,这一天的存货量是不需要翻倍的
2 当天存货量小于需求量的时候,先让这一天的存货量翻倍,求出当天 ”可增加“ 的销售量
然后根据可增加的销售量从大到小排序,选出前f天。
AC代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> using namespace std; struct P{
__int64 x; // 存货量
__int64 y; // 需求量
__int64 z; // “可增加”的销售量
}p[];
bool cmp(P a,P b)
{
return a.z > b.z;
}
int main ()
{
int n,f,i,j;
__int64 a,b;
while (~scanf("%d %d",&n,&f))
{
int k = ;
__int64 counts = ; // 销售量
for (i = ; i < n; i ++)
{
cin>>a>>b;
if (a >= b) // 当天的存货量大于等于需求量
counts += b; // 直接加上当天的最大销售额即可
else // 当天存货量小于需求量
{
p[k].x = a;
p[k].y = b;
if (a* <= b)
p[k ++].z = a;
else
p[k ++].z = b-a;
}
}
sort(p,p+k,cmp);
for (i = ; i < k; i ++)
{
if (i < f) // 选出前f个
counts += (p[i].x+p[i].z);
else
counts += p[i].x;
}
cout<<counts<<endl;
}
return ;
}
Codeforces Round #415 (Div. 2) B. Summer sell-off的更多相关文章
- Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- Codeforces Round#415 Div.2
A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...
- Codeforces Round #415 (Div. 2) A B C 暴力 sort 规律
A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #415 (Div. 2) 翻车啦
A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #415(Div. 2)-810A.。。。 810B.。。。 810C.。。。不会
CodeForces - 810A A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes i ...
- Codeforces Round #415 Div. 1
A:考虑每对最大值最小值的贡献即可. #include<iostream> #include<cstdio> #include<cmath> #include< ...
- Codeforces Round #415 (Div. 2)C
反正又是一个半小时没做出来... 先排序,然后求和,第i个和第j个,f(a)=a[j]-a[i]=a[i]*(2^(j-i-1))因为从j到i之间有j-i-1个数(存在或者不存在有两种情况) 又有a[ ...
- Codeforces Round #415 (Div. 2) C. Do you want a date?
C. Do you want a date? 2 seconds 256 megabytes Leha decided to move to a quiet town Vičkopolis, ...
- Codeforces Round #415 (Div. 1) (CDE)
1. CF 809C Find a car 大意: 给定一个$1e9\times 1e9$的矩阵$a$, $a_{i,j}$为它正上方和正左方未出现过的最小数, 每个询问求一个矩形内的和. 可以发现$ ...
随机推荐
- 进阶篇:4)面向装配的设计DFA总章
本章目的:理解装配的重要性,明确结构工程师也要对装配进行设计. 1.基础阅读 ①进阶篇:1)DFMA方法的运用: ②需要一台FDM3d打印机:请查看 基础篇:8)结构设计装备必备: 2.为什么要学习D ...
- MVC返回数据流,ajax接受并保存文件
js代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- 基于python实现GET和POST请求及token相关调用
GET请求实例: #coding:utf- import requests parm={"}#封装登录参数 r=requests.get("http://space.test.co ...
- APP和小程序哪个更合适企业
很多企业都在纠结做APP好呢,还是做小程序好,那么我们来说说APP和小程序之间的区别,希望通过对比让您了解APP和小程序的功能,让您的企业在制作APP和小程序之间有一个选择. 一. 面向用户群 App ...
- centos 7安装phpMyadmin
1.phpmyadmin官网下载多语言包 2.找到libraries目录下的config.default.php1)查找$cfg['PmaAbsoluteUri'] 设置默认访问网址 2)查找$cfg ...
- LeetCode 100.相同的树(C++)
给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1 ...
- 移动端手势事件 hammer.JS插件
一.引入hammer.JS 1.下载地址:http://download.csdn.net/detail/webxiaoma/9872249 2.官网地址:http ...
- 深入理解JavaScript系列(46):代码复用模式(推荐篇)
介绍 本文介绍的四种代码复用模式都是最佳实践,推荐大家在编程的过程中使用. 模式1:原型继承 原型继承是让父对象作为子对象的原型,从而达到继承的目的: function object(o) { fun ...
- php操作Excel
php操作Excel 1.new PHPExcel对象$objPHPExcel = new PHPExcel(); 2表的初始化设置$objPHPExcel->getProperties()-& ...
- Why Isn't curr_items Decreasing When Items Expire?
Why Isn't curr_items Decreasing When Items Expire?