[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分
裸贪心显然是错的,证明略。
对这道题而言就是略微调整w的范围。
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的更多相关文章
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...
- 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp
这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...
- 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 ...
- [bzoj2621] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta] ...
- [luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)
传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper (状态压缩DP)
不打算把题目放着,给个空间传送门,读者们自己去看,传送门(点我) . 这题是自己做的第一道状态压缩的动态规划. 思路: 在这题中,我们设f[i]为i在二进制下表示的那些牛所用的最小电梯数. 设g ...
随机推荐
- 浅谈JAVA中如何利用socket进行网络编程(二)
转自:http://developer.51cto.com/art/201106/268386.htm Socket是网络上运行的两个程序间双向通讯的一端,它既可以接受请求,也可以发送请求,利用它可以 ...
- Linux - 常用命令索引
文件和目录操作 pwd - 显示当前所在的位置 cd - 切换目录 文件过滤及内容编辑 ... 文本处理三剑客 ... 系统信息查询与搜索文件 ... 文件备份与压缩 ... 用户管理与信息查询 .. ...
- Linux 入门学习教材
我大约从两年前开始接触Linux,在那之前工作中用的都是MCU,arm-cortex M系列的. 从单片机转向Linux学习,经历了很多的困难,刚开始都不知道怎么去编译, 网上也没有找到基础的教程,后 ...
- 8. VIM 系列 - 利用 VIM 8.1 版本编译项目和GDB调试
目录 term 模式 termdebug 模式 VIM版本安装请参考: 0. VIM 系列 - 源码升级最新版本vim term 模式 输入:term 打开此模式,效果如下 这个模式有编辑文本窗口和s ...
- python开发基础教程
第一:python基础 第二:python异常处理类 第三:python装饰器 python常用的装饰器 第四:python发送邮件
- [POI2011]Temperature
Description The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The mea ...
- 16-2 基于localStorage或sessionStorage的计数器
localStorage 方法 localStorage 方法存储的数据没有时间限制.第二天.第二周或下一年之后,数据依然可用. <!doctype html> <html> ...
- Hibernate配置(通过注解配置)
本文主要讲通过注解配置来替换Hibernate的映射文件 1.多对一配置 package com.jazz7.entity; import java.util.Date; import javax.p ...
- spring常用注解笔记
spring常用注解解释: 1. Mybatis的映射文件xxxMapper.xml中resultMap标签的作用 resultMap标签是为了映射select查询出来结果的集合,其主要 作用是将实体 ...
- 老潘 - ListView分析 - 学以致用篇(一)
ListView分析学以致用篇(1) 在我们查看别人的博客的时候,一个人是一个风格的.先说下我的风格,我喜欢思想类比,然后介绍知识,不太喜欢填鸭式的灌输.如果只是想单纯的从我的博客中直接看到代码,我个 ...