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 ...
随机推荐
- JPA 唯一索引
@Table(name = "adam_module", uniqueConstraints = @UniqueConstraint(columnNames = {"in ...
- Oracle查询最近执行过的SQL语句
oracle 查询最近执行过的 SQL语句 select sql_text,last_load_time from v$sql order by last_load_time desc; SELECT ...
- >>> print "hello" SyntaxError: Missing parentheses in call to 'print'
错误原因说你的函数print缺省圆括号,可以知道你用的python是3.x版本3.x版本的python,print中的参数要用圆括号括起来,改成:print("hello")
- React-native 关于键盘遮挡界面问题
//引入 KeyboardAvoidingView import { KeyboardAvoidingView } from 'react-native'; //使用 KeyboardAvoiding ...
- Python第十二节 异常
Python 第十三节 异常 python的错误一般包含两类: 语法错误 : 语法错误一般就是, 当你写完程序提交的时候, 发现无法提交, 提示存在错误.例如: 英文逗号 错写成了 中文逗号 异常 : ...
- js函数方法
/** * call()和apply * 这两个方法都是函数对象的方法,需要通过函数对象来调用 * 当对函数调用call()和apply()都会调用函数执行 * 在调用call()和apply()可以 ...
- 解决code first Migration 增加外键时出现错误的问题
先上模型 Comment public class Comment { [Key] public int CommentId { get; set; } [Required] public int S ...
- Red5文件结构简介
Red5文件结构简介 Red5 是 支持Windows,Linux等多平台的RTMP流媒体服务器,最早属于谷歌下的开源项目,先已移植到Github,地址为https://github.com/Red5 ...
- 记录ajax前后交互
前台请求 $.ajax({ url : '/turn', type : "post", data : { "userName":userName, " ...
- 使用openntpd替换ntpd
系统自带的ntp服务太难用,systemd启动几次没启动起来,懒得折腾,换了openntpd一次成功.