• [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
    ",ans);
        return 0;
    }
  • 相关阅读:
    深入浅出理解基于 Kafka 和 ZooKeeper 的分布式消息队列
    消息队列使用的四种场景介绍
    《深入理解Java函数式编程》系列文章
    搭建微服务框架(Spring Boot + Dubbo + Docker + Jenkins)
    spring boot 整合dubbo
    IDEA中使用springBoot+gradle构建多模块项目
    Mac上zookeeper的安装与启动
    redis常用客户端命令
    mac下安装、配置redies
    轻松看懂机器学习常用算法
  • 原文地址:https://www.cnblogs.com/floatiy/p/9750703.html
Copyright © 2020-2023  润新知