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

    题目描述

    A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.

    The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.

    给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)

    输入输出格式

    输入格式:

    * Line 1: N and W separated by a space.

    * Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.

    输出格式:

    * A single integer, R, indicating the minimum number of elevator rides needed.

    one of the R trips down the elevator.

    输入输出样例

    输入样例#1: 
    4 10 
    5 
    6 
    3 
    7 
    
    输出样例#1: 
    3 

    说明

    There are four cows weighing 5, 6, 3, and 7 pounds. The elevator has a maximum weight capacity of 10 pounds.

    We can put the cow weighing 3 on the same elevator as any other cow but the other three cows are too heavy to be combined. For the solution above, elevator ride 1 involves cow #1 and #3, elevator ride 2 involves cow #2, and elevator ride 3 involves cow #4. Several other solutions are possible for this input.


    状态压缩DP;

    设f[i] 为状态为i的牛的集合被运送到对岸的最小代价, g[i]为f[i]最优时最后一个电梯的最大剩余容量;

    于是我们对于一个不在集合S中的牛i, 如果w[i] <= g[s], 那么我们可以直接把这头牛放进最后一个电梯, 否则我们只能另外开一个电梯;

    然后我们拿新的值与要更新的值做对比, 如果比f[i|1<<(j-1)]小,那么直接替换;

    否则不替换, 如果f值一样,我们就让g[i|1<<(j-1)] 与求出的值取min;

    然后...没了...

    对了, 这道题一不小心暴力过了...什么暴力?明明是乱搞...不过我喜欢;


    乱搞:

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    inline int read(){
        int res = 0;char ch=getchar();
        while(!isdigit(ch))ch=getchar();
        while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch^48);ch=getchar();}
        return res;
    }
    int n, sum, m;
    int main()
    {
        n = read(), m = read();
        for (register int i = 1 ; i <= n ; i ++) sum += read();
        printf("%d
    ", (int)ceil(1.0*((double)sum/(double)m)));
        return 0;
    }

    正解:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    inline int read(){
        int res = 0;char ch=getchar();
        while(!isdigit(ch))ch=getchar();
        while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch^48);ch=getchar();}
        return res;
    }
    int n, w[19], m;
    int g[1<<19], f[1<<19];
    
    int main()
    {
        n = read(), m = read();
        for (register int i = 1 ; i <= n ; i++) w[i] = read();
        memset(f, 0x3f, sizeof f);
        f[0] = 0;
        for (register int i = 0 ; i <= (1<<n)-1 ; i ++) g[i] = m;
        for (register int i = 0 ; i <= (1<<n)-1 ; i ++)
        {
            for (register int j = 1 ; j <= n ; j ++)
            {
                int la = 0, ha = 0;
                if (i & (1<<(j-1))) continue;
                if (g[i] >= w[j]) ha = g[i] - w[j], la = f[i];
                else ha = m - w[j], la = f[i] + 1;
                if (f[i|(1<<(j-1))] > la) 
                    f[i|(1<<(j-1))] = la, g[i|(1<<(j-1))] = ha;
                else if (f[i|(1<<(j-1))] == la)
                    g[i|1<<(j-1)] = max(g[i|1<<(j-1)], ha);
            }
        }
        if (g[(1<<n)-1] < m) f[(1<<n)-1]++;
        cout<<f[(1<<n)-1];
        return 0;
    }
  • 相关阅读:
    阶段一-01.万丈高楼,地基首要-第2章 单体架构设计与准备工作-2-1 单体架构阶段概述与项目演示
    阶段一-01.万丈高楼,地基首要-第1章 学习指南-1-4 架构师所需要具备的技术栈与能力
    阶段一-01.万丈高楼,地基首要-第1章 学习指南-1-3 大型网站架构演变历程
    Spring cloud微服务安全实战-8-1课程总结
    Spring cloud微服务安全实战-7-13章节总结
    Spring cloud微服务安全实战-7-12整合链路追踪和日志监控
    Spring cloud微服务安全实战-7-11PinPoint+SpringBoot环境搭建
    Spring cloud微服务安全实战-7-10ELK日志采集架构优化
    Spring cloud微服务安全实战-7-9自定义日志采集的格式和内容
    Spring cloud微服务安全实战-7-8ELK+SpringBoot环境搭建
  • 原文地址:https://www.cnblogs.com/BriMon/p/9326074.html
Copyright © 2011-2022 走看看