[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)

题目解析

模拟退火

话说啊,贪心是错的,虽然一眼看上去是没有问题的。 
贪心:75分 
裸贪心显然是错的,证明略。
 
但是我们发现了一个有趣的事情,把奶牛的重量从大到小排个序贪心,在一般强度的数据下是正确的,有点像给罐子里先放石头再放沙子再放水比先放水再放沙子石头要更好一样。外加这题数据很小,n^2的贪心是可以执行1e5级别次的。 
综上,退火。 

但答案如果经过特殊构造,将使得退火效率降低,难以得到正确解,这时可以采取一个小技巧: 对题目询问的区间进行小幅晃动
对这道题而言就是略微调整w的范围。 
 
鉴于贪心的错误性,我把e设成了0.9。 在错误性较大的算法下,降温稍快可以让得到正解的可能增大 
不多说了,上代码。
 
用小号调了半天参WA来WA去WAWA大哭

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<ctime>
using namespace std;

const int MAXN = 25;

int n,w,ans,tim;
int a[MAXN],s[MAXN];
double T,e;

bool cmp(int x,int y) {
    return x > y;
}

inline bool getposs() {
    T *= e;
    if(rand() % 10000 < T) return false;
    else return true;
}

inline void clean() {
    T = 9000, e = 0.9;
    memset(s,0,sizeof(s));
    tim = 0;
    return;
}

int main() {
    srand(time(NULL));
    scanf("%d%d",&n,&w);
    w *= 1.005;
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
    }
    sort(a+1,a+1+n,cmp);
    bool flag = false;
    int cnt = 200000;
    ans = 0x3f3f3f3f;
    while(cnt--) {
        clean();
        for(int i = 1;i <= n;i++) {
            flag = false;
            for(int j = 1;j <= tim;j++) {
                if(w - s[j] >= a[i] && getposs()) {
                    s[j] += a[i];
                    flag = true;
                    break;
                }
            }
            if(!flag) s[++tim] += a[i];
        }
        ans = min(ans,tim);
    }
    printf("%d\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/floatiy/p/9750703.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值