zoukankan      html  css  js  c++  java
  • 【洛谷 3052】摩天大楼里的奶牛 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,g[i]表示状态为 i 时还能放下的空间,f[i]表示状态为 i 时用得最少“包”的数量。

    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    using namespace std;
    const int N=20;
    const int M=1<<18+5;
    const int oo=0x3f3f3f3f;
    int n,m,a[N],f[M],g[M];
    int main(){
        scanf("%d %d",&n,&m);
        int op=(1<<n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(f,0x3f,sizeof(f));
        f[0]=1; g[0]=m;
        for(int i=0;i<op;i++){
            for(int j=1;j<=n;j++){
                if((1<<(j-1)) & i) continue;//物体已经被选了 
                if(g[i]>=a[j]  && f[i|(1<<(j-1))]>=f[i]){ //物体可以放进  
                   f[i|(1<<(j-1))]=f[i];
                   g[i|(1<<(j-1))]=max(g[i|(1<<(j-1))],g[i]-a[j]);
                }
                else if(g[i]<a[j] && f[i|(1<<(j-1))]>=f[i]+1){ //包放不下了 
                   f[i|(1<<(j-1))]=f[i]+1;
                   g[i|(1<<(j-1))]=max(g[i|(1<<(j-1))],m-a[j]);            
                }
            }
        }    
        printf("%d",f[(1<<n)-1]); 
        return 0;
    }
  • 相关阅读:
    Url中文传值乱码、编码转码问题总结
    简单介绍下我使用了一年多还不知道的Sql server 2005 组件知识
    一次管理项目小组网络的经历(使用RouterOS与Wireshark破解adsl上网密码,使用isa与Bandwidth Splitter来协调带宽)
    asp.net异步处理机制研究
    注释的力量
    AutoMapper简明教程(学习笔记)
    seajs学习笔记
    Hifi入门
    fxcop使用DataFlowRules规则集
    Visual Studio编译选项简介
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11325406.html
Copyright © 2011-2022 走看看