跳到正文

题解

题解:CF2143B Discounts

Jason_xuan约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年10月9日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 2 分钟
题目
CF2143B

版本

  1. v1当前

思路

注意到如果要使总价最小,要尽量用小面额的折扣券优惠贵的商品,那么就把两个数组排序,从小到大使用还能使用的折扣券即可。

代码

C++
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

#define TRACE 1
#define tcout TRACE && cout

#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define int long long

const int P = 998244353;
const int Base = 3221225477;
const int INF = 0x3f3f3f3f3f3f3f3f;

const int N = 1e2 + 10, M = 2e6 + 10;

int n, k;
int a[N], b[N];

bool cmp(int u, int v)
{
	return u > v;
}
int ans;

void solve()
{
	cin >> n >> k;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for(int i = 1; i <= k; i++)
	{
		cin >> b[i];
	}
	sort(a + 1, a + n + 1, cmp);
	sort(b + 1, b + k + 1);
	int now = 0;
	for(int i = 1; i <= k; i++)
	{
		if(now + b[i] > n)
		{
			break;
		}
		a[now + b[i]] = 0;
		now += b[i];
	}
	ans = 0;
	for(int i = 1; i <= n; i++)
	{
		ans += a[i];
	}
	cout << ans << endl;
}

signed main()
{
    int t;
    cin >> t;
    while(t--)
    {
    	solve();
	}
    return 0;
}
原文