[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

洛谷题目链接:[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.

一句话题意: \(n\)个物品,每个物品有一个大小\(C_i\),现在有若干个袋子来装这些物品,且这些物品的大小和不能超过袋子的大小,问要怎么样分组才能用尽量少的袋子装完这些物品.

题解: 首先看数据范围,发现\(n\)是非常小的,可以直接用一些比较暴力的方法过掉.这里我用的是状压DP.

我们用\(f[i]\)表示\(i\)状态下所需的袋子数(\(i\)为一个二进制数表示以选的物品),再用\(used[i]\)表示\(i\)状态袋子内的使用空间,用\(j\)枚举选用的物品.那么显然有状态转移方程:\(f[i|(1<<j)]=min(f[i]+1, f[i|(1<<j)])\)(在需要多用一个袋子的时候).

看代码理解一下吧.

#include<bits/stdc++.h>
using namespace std;
const int N=18+5;

int n, m, a[N];
int f[(1<<20)], used[(1<<20)];

int main(){
    //freopen("data.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i=0;i<n;i++) cin >> a[i];
    int U = (1<<n)-1;//U表示全集
    memset(f, 127, sizeof(f)); f[0] = 0;
    for(int i=0;i<=U;i++){
		for(int j=0;j<n;j++){
		    if(i&(1<<j)) continue;
		    if(!used[i]){//袋子内没有物品时放物品需要多用一个袋子
				if(f[i|(1<<j)] > f[i]+1) f[i|(1<<j)] = f[i]+1, used[i|(1<<j)] = a[j];
				else if(f[i|(1<<j)] == f[i]+1) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
		    } else {
				if(used[i]+a[j] <= m){
				    if(f[i|(1<<j)] > f[i]) f[i|(1<<j)] = f[i], used[i|(1<<j)] = used[i]+a[j];
				    else if(f[i|(1<<j)] == f[i]) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
				}
				else {
				    if(f[i|(1<<j)] > f[i]+1) f[i|(1<<j)] = f[i]+1, used[i|(1<<j)] = a[j];
				    else if(f[i|(1<<j)] == f[i]+1) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
				}
		    }
		}
    }
    cout << f[U] << endl;
    return 0;
}
posted @ 2018-07-22 20:03  Brave_Cattle  阅读(312)  评论(0编辑  收藏  举报