zoukankan      html  css  js  c++  java
  • P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    用pair写dp, 有种新鲜的感觉.
    就是状压dp, pair第一维记录用了几个电梯, 第二维记录还剩多少空间, 取min转移即可.
    用pair主要是为了使代码简洁.

    #include <cstdio>
    #include <cstring>
    #include <cassert>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef pair<int, int> P;
    const int MAXN = 18 + 1;
    inline int read(){
        char ch = getchar(); int x = 0;
        while(!isdigit(ch)) ch = getchar();
        while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
        return x;
    }
    
    int N, W;
    int c[MAXN];
    P f[(1 << 18)];
    
    int main(){
        cin>>N>>W;
        for(int i = 0; i < N; i++) c[i] = read();
        memset(f, 0x3f, sizeof(f)), f[0] = P(0, 0);
        for(int i = 0; i < (1 << N); i++) {
            for(int j = 0; j < N; j++) if(!(i & (1 << j)))
                f[i | (1 << j)] = min(f[i | (1 << j)], 
                f[i].second + c[j] <= W ? P(f[i].first, f[i].second + c[j]) : P(f[i].first + 1, c[j]));
        }
        printf("%d
    ", f[(1 << N) - 1].first + (f[(1 << N) - 1].second != 0));
        return 0;
    }
    
    
  • 相关阅读:
    zyUpload+struct2完成文件上传
    js表单动态添加数据并提交
    Hibernate注解
    ueditor的配置和使用
    设计模式
    静态Include和动态Include测试并总结
    java笔试题
    perf使用示例1
    perf 简介
    ss简单使用
  • 原文地址:https://www.cnblogs.com/wsmrxc/p/9818557.html
Copyright © 2011-2022 走看看