题目描述

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 = ; 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() % < T) return false;
else return true;
} inline void clean() {
T = , e = 0.9;
memset(s,,sizeof(s));
tim = ;
return;
} int main() {
srand(time(NULL));
scanf("%d%d",&n,&w);
w *= 1.005;
for(int i = ;i <= n;i++) {
scanf("%d",&a[i]);
}
sort(a+,a++n,cmp);
bool flag = false;
int cnt = ;
ans = 0x3f3f3f3f;
while(cnt--) {
clean();
for(int i = ;i <= n;i++) {
flag = false;
for(int j = ;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 ;
}

[USACO12MAR] 摩天大楼里的奶牛 Cows in a Skyscraper的更多相关文章

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

    洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  2. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

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

    题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...

  4. 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  5. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp

    这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...

  6. LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

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

    题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta] ...

  8. [luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)

    传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f ...

  9. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper (状态压缩DP)

    不打算把题目放着,给个空间传送门,读者们自己去看,传送门(点我)    . 这题是自己做的第一道状态压缩的动态规划. 思路: 在这题中,我们设f[i]为i在二进制下表示的那些牛所用的最小电梯数. 设g ...

随机推荐

  1. 浅谈JAVA中如何利用socket进行网络编程(二)

    转自:http://developer.51cto.com/art/201106/268386.htm Socket是网络上运行的两个程序间双向通讯的一端,它既可以接受请求,也可以发送请求,利用它可以 ...

  2. Linux - 常用命令索引

    文件和目录操作 pwd - 显示当前所在的位置 cd - 切换目录 文件过滤及内容编辑 ... 文本处理三剑客 ... 系统信息查询与搜索文件 ... 文件备份与压缩 ... 用户管理与信息查询 .. ...

  3. Linux 入门学习教材

    我大约从两年前开始接触Linux,在那之前工作中用的都是MCU,arm-cortex M系列的. 从单片机转向Linux学习,经历了很多的困难,刚开始都不知道怎么去编译, 网上也没有找到基础的教程,后 ...

  4. 8. VIM 系列 - 利用 VIM 8.1 版本编译项目和GDB调试

    目录 term 模式 termdebug 模式 VIM版本安装请参考: 0. VIM 系列 - 源码升级最新版本vim term 模式 输入:term 打开此模式,效果如下 这个模式有编辑文本窗口和s ...

  5. python开发基础教程

    第一:python基础 第二:python异常处理类 第三:python装饰器  python常用的装饰器 第四:python发送邮件

  6. [POI2011]Temperature

    Description The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The mea ...

  7. 16-2 基于localStorage或sessionStorage的计数器

    localStorage 方法 localStorage 方法存储的数据没有时间限制.第二天.第二周或下一年之后,数据依然可用. <!doctype html> <html> ...

  8. Hibernate配置(通过注解配置)

    本文主要讲通过注解配置来替换Hibernate的映射文件 1.多对一配置 package com.jazz7.entity; import java.util.Date; import javax.p ...

  9. spring常用注解笔记

    spring常用注解解释: 1. Mybatis的映射文件xxxMapper.xml中resultMap标签的作用 resultMap标签是为了映射select查询出来结果的集合,其主要 作用是将实体 ...

  10. 老潘 - ListView分析 - 学以致用篇(一)

    ListView分析学以致用篇(1) 在我们查看别人的博客的时候,一个人是一个风格的.先说下我的风格,我喜欢思想类比,然后介绍知识,不太喜欢填鸭式的灌输.如果只是想单纯的从我的博客中直接看到代码,我个 ...