zoukankan      html  css  js  c++  java
  • BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper

    首先比较容易想到是状态压缩DP

    令$f[S]$表示选取了集合$S$以后,已经送了最少次数$cnt$且当前电梯剩下的体积$rest$最大(即$f[S]$是一个二元组$(cnt, rest)$)

    于是$f[S] = min_{i in S} f[S - {i}] + v[i]$

    重载的$<$和$+$运算详情就请看程序好了,反正就是一个贪心思想,总复杂度$O(n * 2 ^ {n - 1})$

     1 /**************************************************************
     2     Problem: 2621
     3     User: rausen
     4     Language: C++
     5     Result: Accepted
     6     Time:532 ms
     7     Memory:13092 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <algorithm>
    12  
    13 using namespace std;
    14 const int N = 20;
    15 const int S = 1 << N;
    16 const int inf = 1e9;
    17  
    18 int n, mx, mxs;
    19 int a[S];
    20  
    21 struct data {
    22     int cnt, rest;
    23     data(int _c = 0, int _r = mx) : cnt(_c), rest(_r) {}
    24      
    25     inline data operator + (int t) const {
    26         static data res;
    27         res = *this;
    28         res.rest -= t;
    29         if (res.rest < 0) ++res.cnt, res.rest += mx;
    30         return res;
    31     }
    32      
    33     inline bool operator < (const data &d) const {
    34         return cnt == d.cnt ? rest < d.rest : cnt < d.cnt;
    35     }   
    36 } f[S];
    37  
    38 int main() {
    39     int i, s, t, now;
    40     scanf("%d%d", &n, &mx);
    41     mxs = (1 << n) - 1;
    42     for (i = 1; i <= n; ++i) scanf("%d", &a[1 << i - 1]);
    43     f[0] = data(0, mx);
    44     for (s = 1; s <= mxs; ++s) {
    45         t = s, f[s] = data(inf, mx);
    46         while (t) {
    47             now = t & (-t);
    48             f[s] = min(f[s], f[s ^ now] + a[now]);
    49             t -= now;
    50         }
    51     }
    52     printf("%d
    ", f[mxs].cnt + (f[mxs].rest != mx));
    53     return 0;
    54 }
    55 
    View Code
    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)
    hdu 1181 以b开头m结尾的咒语 (DFS)
    hdu 1258 从n个数中找和为t的组合 (DFS)
    hdu 4707 仓鼠 记录深度 (BFS)
    LightOJ 1140 How Many Zeroes? (数位DP)
    HDU 3709 Balanced Number (数位DP)
    HDU 3652 B-number (数位DP)
    HDU 5900 QSC and Master (区间DP)
    HDU 5901 Count primes (模板题)
    CodeForces 712C Memory and De-Evolution (贪心+暴力)
  • 原文地址:https://www.cnblogs.com/rausen/p/4471198.html
Copyright © 2011-2022 走看看