Description
在商场中,有m种的折扣,当你买的东西到qi个的时候,可以使用该种类型的折扣,对要买的东西进行免费,东西可以免费最至多2个。
免费的条件是,你所选择进行免费的东西,不能超过已经在购物车的中最便宜的商品。
问买所有的物品,使用这些打折规则,使得付的钱最少。
Solution
读题可发现:
- 每种优惠类型可以使用无数次 => 明示贪心(每次都用要求最少的那种)
- 所选择进行免费的东西,不能超过已经在购物车的中最便宜的商品(
狡猾精明的商人)=> 所以最好就是从大到小依次买,能优惠时直接跳两个
(我不知道怎么脑子抽风了,想到从大到小的顺序以后还在顺序做…浪费了半个多小时)
代码如下:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int n, q;
int discount[maxn], a[maxn];
ll ans = 0;
il bool cmp(int x, int y) {
return x > y;
}
int main() {
scanf("%d", &q);
for(re int i = 1; i <= q; ++i)
scanf("%d", &discount[i]);
sort(discount+1, discount+q+1);
scanf("%d", &n);
for(re int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
sort(a+1, a+n+1, cmp);
int cnt = 0, tmp = discount[1];
for(re int i = 1; i <= n; ++i) {
if(cnt == tmp) {
cnt = 0;
i += 2;
}
cnt++;
ans += a[i];
}
printf("%lld\n", ans);
return 0;
}