CF779C(round 402 div.2 C) Dishonest Sellers
题意:
Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.
Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.
Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.
In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — total number of items to buy and minimal number of items Igor wants to by right now.
The second line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 104) — prices of items during discounts (i.e. right now).
The third line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104) — prices of items after discounts (i.e. after a week).
Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.
3 1
5 4 6
3 1 5
10
5 3
3 4 7 10 3
4 5 5 12 5
25
In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10.
In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25.
思路:
贪心,排序。
实现:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; struct node
{
int a, b;
};
node t[];
int n, k;
bool cmp(const node & x, const node & y)
{
if (x.a >= x.b && y.a >= y.b)
{
return x.a - x.b < y.a - y.b;
}
else if (x.a < x.b && y.a >= y.b)
{
return true;
}
else if (x.a >= x.b && y.a < y.b)
{
return false;
}
else
{
return x.b - x.a > y.b - y.a;
} }
int main()
{
cin >> n >> k;
for (int i = ; i < n; i++)
{
scanf("%d", &t[i].a);
}
for (int i = ; i < n; i++)
{
scanf("%d", &t[i].b);
}
sort(t, t + n, cmp);
int sum = ;
for (int i = ; i < n; i++)
{
if (i < k)
{
sum += t[i].a;
}
else
{
sum += min(t[i].a, t[i].b);
}
}
cout << sum << endl;
return ;
}
CF779C(round 402 div.2 C) Dishonest Sellers的更多相关文章
- 【贪心】Codeforces Round #402 (Div. 2) C. Dishonest Sellers
按照b[i]-a[i],对物品从大到小排序,如果这个值大于零,肯定要立刻购买,倘若小于0了,但是没买够K个的话,也得立刻购买. #include<cstdio> #include<a ...
- Codeforces Round #402 (Div. 2) A+B+C+D
Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...
- Codeforces Round #402 (Div. 2)
Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...
- Codeforces Round #402 (Div. 2) A,B,C,D,E
A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces Round #402 (Div. 2) A B C sort D二分 (水)
A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces Round #402 (Div.2) A-E
2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...
- 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding
暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=1 ...
- Codeforces Round #402 (Div. 2) 题解
Problem A: 题目大意: 给定两个数列\(a,b\),一次操作可以交换分别\(a,b\)数列中的任意一对数.求最少的交换次数使得任意一个数都在两个序列中出现相同的次数. (\(1 \leq a ...
- Codeforces Round #402 (Div. 2) C
Description Igor found out discounts in a shop and decided to buy n items. Discounts at the store wi ...
随机推荐
- linux内存操作--ioremap和mmap
最近在做视频输出相关的东西,对于预留给framebuffer的内存使用不是很清楚,现在找到一些资料整理一下,以备使用.if (想看使用方法) goto 使用方法: 对于一个系统来讲,会有很多的外 ...
- react源码分析
ReactMount.render -> ReactMount._renderSubtreeIntoContainer -> ReactMount._renderNewRootCompon ...
- Oracle:exp导出exp-00091问题
今天导出一数据库数据,发现EXP-00091问题: 连接到: Oracle Database 10g Enterprise Edition Release - Production With the ...
- 安装程序工具 (Installutil.exe)22
网址:https://msdn.microsoft.com/zh-cn/library/50614e95(VS.80).aspx 安装程序工具 (Installutil.exe) .NET Fram ...
- uuid.js
// On creation of a UUID object, set it's initial valuefunction UUID(){ this.id = this.createUUID ...
- OpenCV——PS滤镜算法之 Ellipsoid (凹陷)
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- 跨平台实现zip压缩加密功能
使用zlib将文件夹压缩成zip文件时,需要自己读取文件然后写入zip文件.利用官方下载的zlib包中包含的contrib/minizip/zip.h和zip.c代码提供的函数,可以很容易实现这个功能 ...
- SPOJ:Ada and Graft (set合并&优化)
As you might already know, Ada the Ladybug is a farmer. She grows a big fruit tree (with root in 0). ...
- 洛谷P3195 [HNOI2008]玩具装箱TOY——斜率优化DP
题目:https://www.luogu.org/problemnew/show/P3195 第一次用斜率优化...其实还是有点云里雾里的: 网上的题解都很详细,我的理解就是通过把式子变形,假定一个最 ...
- python-day9-进程、线程、协程篇
python threading模块 线程有两种调用方式: 直接调用 import threading import time def sayhi(num): #定义每个线程要运行的函数 print( ...