zoukankan      html  css  js  c++  java
  • Codeforces 1154F Shovels Shop

    题目链接: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个优惠。

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

    代码如下:

     1 #pragma GCC optimize("Ofast")
     2 #include <bits/stdc++.h>
     3 using namespace std;
     4  
     5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
     6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
     7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
     8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
     9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
    10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
    11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
    12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
    13  
    14 #define pr(x) cout << #x << " = " << x << "  "
    15 #define prln(x) cout << #x << " = " << x << endl
    16  
    17 #define LOWBIT(x) ((x)&(-x))
    18  
    19 #define ALL(x) x.begin(),x.end()
    20 #define INS(x) inserter(x,x.begin())
    21  
    22 #define ms0(a) memset(a,0,sizeof(a))
    23 #define msI(a) memset(a,inf,sizeof(a))
    24 #define msM(a) memset(a,-1,sizeof(a))
    25 
    26 #define MP make_pair
    27 #define PB push_back
    28 #define ft first
    29 #define sd second
    30  
    31 template<typename T1, typename T2>
    32 istream &operator>>(istream &in, pair<T1, T2> &p) {
    33     in >> p.first >> p.second;
    34     return in;
    35 }
    36  
    37 template<typename T>
    38 istream &operator>>(istream &in, vector<T> &v) {
    39     for (auto &x: v)
    40         in >> x;
    41     return in;
    42 }
    43  
    44 template<typename T1, typename T2>
    45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
    46     out << "[" << p.first << ", " << p.second << "]" << "
    ";
    47     return out;
    48 }
    49  
    50 typedef long long LL;
    51 typedef unsigned long long uLL;
    52 typedef pair< double, double > PDD;
    53 typedef set< int > SI;
    54 typedef vector< int > VI;
    55 const double EPS = 1e-10;
    56 const int inf = 1e9 + 9;
    57 const LL mod = 1e9 + 7;
    58 const int maxN = 2e5 + 7;
    59 const LL ONE = 1;
    60 
    61 int n, m, k, ans;
    62 int a[maxN], sumA[2007];
    63 int offers[2007];
    64 
    65 int dp[2007];
    66 
    67 inline int getSum(int x, int y) {
    68     if(x > y) return 0;
    69     return sumA[y] - sumA[x - 1];
    70 }
    71 
    72 int main(){
    73     INIT();
    74     cin >> n >> m >> k;
    75     For(i, 1, n) cin >> a[i];
    76     For(i, 1, m) {
    77         int x, y;
    78         cin >> x >> y;
    79         if(x > k) continue;
    80         offers[x] = max(offers[x], y);
    81     }
    82     
    83     sort(a + 1, a + n + 1);
    84     
    85     For(i, 1, k) sumA[i] = sumA[i - 1] + a[i];
    86     
    87     For(i, 1, k) {
    88         For(j, 1, i){
    89             int x = dp[i - j] + getSum(i - j + 1, i - j + offers[j]);
    90             dp[i] = max(dp[i], x);
    91         }
    92     }
    93     
    94     cout << sumA[k] - dp[k] << endl;    
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    To select the file to upload we can use the standard HTML input control of type
    Cascading Menu Script using Javascript Explained
    网站首页head区代码规范
    轻松掌握 Java 泛型
    JDK 5.0 中的泛型类型学习
    如何在firefox下获取下列框选中option的text
    是同步方法还是 synchronized 代码? 详解多线程同步规则
    javascript select option对象总结
    Select的动态取值(Text,value),添加,删除。兼容IE,FireFox
    javascript在ie和firefox下的一些差异
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/10732037.html
Copyright © 2011-2022 走看看