CF - 779C. Dishonest Sellers 排序+贪心

1.题目描述:

C. Dishonest Sellers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·1050 ≤ 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).

Output

Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.

Examples
input
3 1
5 4 6
3 1 5
output
10
input
5 3
3 4 7 10 3
4 5 5 12 5
output
25
Note

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.


2.题意概述:

一个人打算这两周去商店总共买n件衣服,今日至少要买k件,剩下的可以下周买,正逢打折活动,持续一周,但是老板很黑,有些衣服本周打折的价格反而比下周不打折的价格还高,要你帮他算一下,他买这n件衣服的最少花费。

3.解题思路:

今日至少要买k件,那么肯定先买“真”打折的衣服,如果“真”打折衣服买完还不够k件,则贪心地买差价最小的“假”打折衣服。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 200100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7
using namespace std;
typedef long long ll;
struct node
{
	int a, b, vis;
}p[maxn];
bool cmpa(node x, node y)
{
	if (x.a == y.a)
		return x.b < y.b;
	return x.a < y.a;
}
bool cmp(node x, node y)
{
	return x.a - x.b < y.a - y.b;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 0; i < n; i++)
			scanf("%d", &p[i].a);
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &p[i].b);
			p[i].vis = 0;
		}
		sort(p, p + n, cmpa);
		int cnt = 0, ans = 0;
		for (int i = 0; i < n; i++)	//先买诚实的产品
		{
			if (p[i].a < p[i].b)
			{
				ans += p[i].a;
				cnt++;
				p[i].vis = 1;
			}
		}
		sort(p, p + n, cmp);
		if (cnt < k)	//只能买说谎的产品了
		{
			for (int i = 0; i < n; i++)
			{
				if (!p[i].vis)
				{
					if (cnt < k)
					{
						ans += p[i].a;
						p[i].vis = 1;
						cnt++;
					}
					else
						break;
				}
			}
		}
		// 买剩下的产品
		for (int i = 0; i < n; i++)
			if (!p[i].vis)
				ans += p[i].b;
		printf("%d\n", ans);
	}

#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值