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 ...
随机推荐
- adb 常用命令大全
adb 常用命令大全 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd ...
- MVC,MVP 和 MVVM 的图示(转载)
MVC,MVP 和 MVVM 的图示 作者: 阮一峰 日期: 2015年2月 1日 复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常 ...
- libvirt虚拟机管理常用指令
创建虚拟机 virt-install virt-install --connect qemu:///system -n $NAME -r $MEM -f $DISK -s $DISK_SIZE --v ...
- HashSet、LinkedHashSet、TreeSet的区别
HashSet:哈希表是通过使用称为散列法的机制来存储信息的,元素并没有以某种特定顺序来存放: LinkedHashSet:以元素插入的顺序来维护集合的链接表,允许以插入的顺序在集合中迭代: Tree ...
- CSV模块的使用
1.csv简介 CSV (Comma Separated Values),即逗号分隔值(也称字符分隔值,因为分隔符可以不是逗号),是一种常用的文本 格式,用以存储表格数据,包括数字或者字符.很多程序在 ...
- 利用Process类创建多个子进程对象执行任务,主进程负责调度
import time from multiprocessing import Process def run1(): for i in range(5): print("sunck is ...
- Spring事务管理-传播行为-隔离级别
事务是逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 事务的特性:ACID 原子性:事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生 一致性:事务前后数据的完整性约束保持一致 ...
- PHP实现上传视频的功能
首先前台HTML表单代码如下: <html> <head> <meta http-equiv="Content-Type" content=" ...
- 【leetcode】977. Squares of a Sorted Array
题目如下: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...
- leetcode-160周赛-5241-铺瓷砖
题目描述: 方法一:动态规划 class Solution: def f(self, n, m): if n < m: n, m = m, n if (n, m) in self.mem: re ...