题目链接:http://codeforces.com/problemset/problem/1154/F

题目大意:

  商店有n把铲子,欲购k把,现有m种优惠,每种优惠可使用多次,每种优惠(x, y)表示一次买满x把可使其中最便宜的y把免费。就正好购买k把的最小花费。

分析:

  由于要正好购买k把铲子,我们只需要关注n把铲子中价格偏小的k把即可,同时满减优惠如果要买的数量大于k的优惠也不用管。

  设dp[i]为购买i把铲子的最大优惠额度。

  显然有dp[0] = 0。

  为了便于计算某个区间内的优惠,我们可以建立关于铲子售价的前缀和数组。

  设买满j把的最大优惠铲子数为offer[j]

  对于dp[i],需要遍历offer[1]~offer[i]每个优惠,对于第j个优惠,无非用或不用,两种取最大即可:dp[i] = max(dp[i], dp[i - j] + getSum(i - j + 1, i - j + offers[j])),问题来了,为什么这个优惠一定作用在这i把铲子中的后j把呢?有没有可能作用在中间j把,后面比如说k(k < j)把用优惠offer[k]的时候优惠更大呢?确实有可能。不过如果这个优惠存在,他必然在遍历到第k个优惠的时候已经算过了,第j个优惠此时被作用在前i - k把的最后j把上(如果能使得优惠更大的话,这是在算dp[i - k]时就算好的了),并且会保留到第j个优惠。

  这道题很像完全背包但并不是完全背包,因为优惠的钱数是依赖于铲子的价值而非固定不变的。

代码如下:

 #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef set< int > SI;
typedef vector< int > VI;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ;
const LL ONE = ; int n, m, k, ans;
int a[maxN], sumA[];
int offers[]; int dp[]; inline int getSum(int x, int y) {
if(x > y) return ;
return sumA[y] - sumA[x - ];
} int main(){
INIT();
cin >> n >> m >> k;
For(i, , n) cin >> a[i];
For(i, , m) {
int x, y;
cin >> x >> y;
if(x > k) continue;
offers[x] = max(offers[x], y);
} sort(a + , a + n + ); For(i, , k) sumA[i] = sumA[i - ] + a[i]; For(i, , k) {
For(j, , i){
int x = dp[i - j] + getSum(i - j + , i - j + offers[j]);
dp[i] = max(dp[i], x);
}
} cout << sumA[k] - dp[k] << endl;
return ;
}

Codeforces 1154F Shovels Shop的更多相关文章

  1. codeforces#1154F. Shovels Shop (dp)

    题目链接: http://codeforces.com/contest/1154/problem/F 题意: 有$n$个物品,$m$条优惠 每个优惠的格式是,买$x_i$个物品,最便宜的$y_i$个物 ...

  2. Codeforces 1154F - Shovels Shop - [DP]

    题目链接:https://codeforces.com/contest/1154/problem/F 题解: 首先,可以确定的是: 1.$(x,y)$ 里 $x>k$ 的都不可能用: 2.肯定买 ...

  3. CF F. Shovels Shop(前缀和预处理+贪心+dp)

    F. Shovels Shop time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)

    题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...

  5. Codeforces Round #552 (Div. 3) F. Shovels Shop(dp)

    题目链接 大意:给你n个物品和m种优惠方式,让你买k种,问最少多少钱. 思路:考虑dpdpdp,dp[x]dp[x]dp[x]表示买xxx种物品的最少花费,然后遍历mmm种优惠方式就行转移就好了. # ...

  6. CodeForces 286E Ladies' Shop 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8781889.html 题目传送门 - CodeForces 286E 题意 首先,给你$n$个数(并告诉你$m$ ...

  7. cf F. Shovels Shop

    https://codeforces.com/contest/1154/problem/F 给定m个折扣 每个折扣的{x,y}的意思是每次购买如果买到确切的x只铲子就其中的最便宜的y只铲子免付: 先贪 ...

  8. Codeforces 286E - Ladies' Shop(FFT)

    Codeforces 题面传送门 & 洛谷题面传送门 好久没刷过 FFT/NTT 的题了,写篇题解罢( 首先考虑什么样的集合 \(T\) 符合条件.我们考察一个 \(x\in S\),根据题意 ...

  9. codeforces 286E Ladies' Shop

    题目大意:n个小于等于m的数,现在你需要在[1,m]中选择若干个数,使得选出的数能组成的所有数正好与n个数相同,给出最少要选多少个数. 题目分析: 结论一:选择的若干个数一定在n个数中. 证明:否则的 ...

随机推荐

  1. Centos7下用户登录失败N次后锁定用户禁止登陆的方法

    前言 针对linux上的用户,如果用户连续3次登录失败,就锁定该用户,几分钟后该用户再自动解锁.Linux有一个pam_tally2.so的PAM模块,来限定用户的登录失败次数,如果次数达到设置的阈值 ...

  2. Spring Security(十六):5.7 Multiple HttpSecurity

    We can configure multiple HttpSecurity instances just as we can have multiple <http> blocks. T ...

  3. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  4. 初学Python——介绍一些内置方法

    1.abs()求绝对值 a=abs(-10) print(a) # 输出:10 2.all() 用来检测列表元素是否全部为空.0.False print(all([0,5,4])) #当列表所有元素都 ...

  5. P3372 【模板】线段树 1

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...

  6. Ubuntu14.04下如何安装TensorFlow

    一.安装Anaconda Anaconda官网(www.continuum.io/downloads) 也可以在(https://repo.continuum.io/archive/)上根据自己的操作 ...

  7. dubbo源码阅读

    http://seekheap.com/posts/dubbo/dubbo-src-01-overview-and-debug-environment.html 先占坑

  8. Elicpse使用技巧-打开选中文件文件夹或者包的当前目录

    很多时候,我们需要在eclipse那里打开选中文件(文件夹,包)的当前目录,在资源管理器那里显示这个目录,这个时候,我们又不想采用“选中文件/文件夹/包名--右击--Properties--Locat ...

  9. 小L记单词

    题目描述 小L最近在努力学习英语,但是对一些词组总是记不住,小L小把这些词组中每一个单词的首字母都记一下,这样形成词组的缩写,通过这种方式小L的学习效率明显提高. 输入 输入有多行,每组测试数据占一行 ...

  10. scrapy框架原理学习

    Scrapy框架原理: 参考出处:https://cuiqingcai.com/3472.html 整个Scrapy的架构图: Scrapy Engine: 这是引擎,负责Spiders.ItemPi ...