USACO2007 The Bale Tower /// DFS oj21160
题目大意:
给出N个捆包,每个捆包有相应的长度和宽度,要求堆叠捆包,使下方的捆包长宽永远大于上方的捆包的长宽。
Multiple test case. For each case:
* Line 1: A single integer, N
* Lines 2..N+1: Each line describes a bale with two space-separated integers, respectively the width and breadth
For each case, output one line: The height of the tallest possible tower that can legally be built from the bales.
6
6 9
10 12
9 11
8 10
7 8
5 3
5
方法一
先结构体sort()对长排序 长相等时对宽排序, 再枚举各个宽为底,算出所有可能结果,再求出最大结果
#include <bits/stdc++.h>
using namespace std;
int n,ans;
struct BALE
{
int x,y;
}bale[];
bool cmp(struct BALE q,struct BALE p)
{
if(q.x==p.x) return q.y<p.x;
return q.x>p.x;
}
void cnt(int i,int sum)
{
ans=max(ans,sum);
if(sum==n) return;
for(int j=i+;j<n;j++)
if(bale[i].y>=bale[j].y)
{
cnt(j,sum+);
if(sum==n) return;
}
}
int main()
{
while(~scanf("%d",&n))
{
ans=;
for(int i=;i<n;i++)
scanf("%d%d",&bale[i].x,&bale[i].y);
sort(bale,bale+n,cmp);
for(int i=;i<n;i++)
{
cnt(i,);
//printf("%d\n",cnt(i));
}
printf("%d\n",ans);
} return ;
}
—— 01.28更 ——
OJ测试数据更新了之后 这份代码狗带了 因为相同的情况是不能考虑的
如:
3
9 3
8 4
8 4
答案应为 1
按方法二补
#include <bits/stdc++.h>
using namespace std;
int n,ans,flag[];
struct BALE
{
int x,y;
}bale[];
void DFS(int i,int sum)
{
ans=max(sum,ans);
if(sum==n) return;
for(int j=;j<=n;j++)
{
if(bale[i].x>bale[j].x&&bale[i].y>bale[j].y&&!flag[j])
{
flag[j]=;
DFS(j,sum+);
flag[j]=;
}
}
}
int main()
{
while(~scanf("%d",&n))
{
ans=;
for(int i=;i<=n;i++)
scanf("%d%d",&bale[i].x,&bale[i].y);
for(int i=;i<=n;i++)
{
memset(flag,,sizeof(flag));
flag[i]=;
DFS(i,);
}
printf("%d\n",ans);
} return ;
}
————————
方法二
DFS深搜 (偷下LXH的代码)
还是需要添加标记

USACO2007 The Bale Tower /// DFS oj21160的更多相关文章
- CF 327D - Block Tower 数学题 DFS 初看很难,想通了就感觉很简单
D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs
D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...
- codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)
题目链接: D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 【BZOJ】1638: [Usaco2007 Mar]Cow Traffic 奶牛交通(dfs+dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1638 一条边(u, v)经过的数量=度0到u的数量×v到n的数量 两次记忆化dfs算出他们即可 #i ...
- bzoj 1647: [Usaco2007 Open]Fliptile 翻格子游戏【dfs】
这个可以用异或高斯消元,但是我不会呀我用的暴搜 2的m次方枚举第一行的翻转情况,然后后面的就定了,因为对于一个j位置,如果i-1的j位置需要翻,那么一定要翻i的j,因为这是i-1的j最后翻的机会 按字 ...
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
- BZOJ 1711: [Usaco2007 Open]Dining吃饭
1711: [Usaco2007 Open]Dining吃饭 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 902 Solved: 476[Submit ...
- BZOJ1711: [Usaco2007 Open]Dingin吃饭
1711: [Usaco2007 Open]Dingin吃饭 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 508 Solved: 259[Submit ...
- BZOJ1690: [Usaco2007 Dec]奶牛的旅行
1690: [Usaco2007 Dec]奶牛的旅行 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 552 Solved: 286[Submit][St ...
随机推荐
- 哈希表(hash)详解
哈希表结构讲解: 哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度. ...
- Django中的缓存机制
概述 对于中等流量网站来说,尽可能的减少开销是必要的.缓存数据就是为了保存那些需要很多计算资源大的结果,这样的的话就不必在下次重复消耗计算资源. Django自带了一个健壮的缓存系 ...
- Linux安装配置Nginx服务器
如有需要可以加我Q群[308742428]大家一起讨论技术,有偿服务. 后面会不定时为大家更新文章,敬请期待. 喜欢的朋友可以关注下. 前言 今天搭建nginx服务器,来访问静态资源文件. Nginx ...
- MyBatis笔记一:GettingStart
MyBatis笔记一:GettingStart 1.MyBatis优点 我们的工具和各种框架的作用就是为了我们操作数据库简洁,对于一些数据库的工具能帮我们少写一些处理异常等等的代码,但是他们并不是自动 ...
- JAVA基础学习-多态 对象转型 final
一.多态的产生条件 1:继承 存在继承的类之间 2:方法重装 3:父类继承子类重装的方法 子类的对象 也是属于父类的 二:对象的转型 1:向上转型:当子类转型成父类时 例如:Animal a = n ...
- JMeter 并发压力测试
一,下载JMeter http://jmeter.apache.org/download_jmeter.cgi 二,创建默认配置 可以不用配置相同参数. 测试计划:右键添加线程组 线程组:右键添加 ...
- Qt:代码里存在中文时带来的问题
一.报错: 常量中有换行符 方法1: 把文本文件转化为unicode或者utf-8, 同是还要带上QString::fromLocal8Bit() 还有其他方法,感觉不靠谱 二.显示异常:乱码 QSt ...
- 关于_getattr_方法的一些理解
在学习rest framework的过程中,rest framework的request是经过重构的,但是如果调用重构对象request中的属性,如果属性不存在会调用原request对象中的属性,它使 ...
- nodejs fs.readFile
fs.readFile(path[, options], callback) path <string> | <Buffer> | <URL> | <inte ...
- 记录我个人对Telegram的了解
对Telegram(电报) 开始的了解是以为提供了Telegram API,就可以基于它进行开发自己的即时通讯(Instant Messaging)程序. 大概使用过: webogram 和 tele ...