题目大意:

给出N个捆包,每个捆包有相应的长度和宽度,要求堆叠捆包,使下方的捆包长宽永远大于上方的捆包的长宽。

Input

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

Output

For each case, output one line: The height of the tallest possible tower that can legally be built from the bales.

Sample Input

6
6 9
10 12
9 11
8 10
7 8
5 3

Sample Output

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的更多相关文章

  1. CF 327D - Block Tower 数学题 DFS 初看很难,想通了就感觉很简单

    D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. 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 ...

  3. 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 ...

  4. 【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 ...

  5. bzoj 1647: [Usaco2007 Open]Fliptile 翻格子游戏【dfs】

    这个可以用异或高斯消元,但是我不会呀我用的暴搜 2的m次方枚举第一行的翻转情况,然后后面的就定了,因为对于一个j位置,如果i-1的j位置需要翻,那么一定要翻i的j,因为这是i-1的j最后翻的机会 按字 ...

  6. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  7. BZOJ 1711: [Usaco2007 Open]Dining吃饭

    1711: [Usaco2007 Open]Dining吃饭 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 902  Solved: 476[Submit ...

  8. BZOJ1711: [Usaco2007 Open]Dingin吃饭

    1711: [Usaco2007 Open]Dingin吃饭 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 508  Solved: 259[Submit ...

  9. BZOJ1690: [Usaco2007 Dec]奶牛的旅行

    1690: [Usaco2007 Dec]奶牛的旅行 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 552  Solved: 286[Submit][St ...

随机推荐

  1. array排序(按数组中对象的属性进行排序)

    使用array.sort()对数组中对象的属性进行排序 <template> <div> <a @click="sortArray()">降序& ...

  2. python学习笔记:数据类型——数字、字符串、元祖、字典

    计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义不同的数 ...

  3. 力扣算法——134GasStation【M】

    在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...

  4. Django Model里的__str__以及Meta

    举个栗子,注释已经比较详细了 name = models.CharField(max_length=30,verbose_name='标签名称') #max_length=30里的30在mysql以前 ...

  5. Java中编写一个完美的equals方法

    首先看下Java语言规范对equals方法的要求: 1,自反性,对于任何非控引用x,x.equals(x)都应该返回true. 2,对称性,对于任何引用x和y,如果x.equals(y)返回true, ...

  6. [轉]关于CR0.WP

    关于CR0.WP 我们知道CR0的WP位可以关闭内核写保护.他和页表的R/W位有关.Intel手册中的描述绕来绕去似乎一直没有说到重点. When the processor is in superv ...

  7. v-slot vue2.6新增指令使用指南

    子组件 <template> <div class="wrapper"> <slot name="demo" :msg=" ...

  8. SpringMVC学习(6):数据验证

    在系列(4).(5)中我们展示了如何绑定数据,绑定完数据之后如何确保我们得到的数据的正确性?这就是我们本篇要说的内容 -> 数据验证. 这里我们采用Hibernate-validator来进行验 ...

  9. enovia plm export to sap

    UPC creation UPC 结构 PLM 使用的UPC 是 14个数字组成的,兼容. 前两位为 0,后12位为有效数字,在SAP中0会被忽略,符合国际UPC通用 规则, 前一位为0,后13 位为 ...

  10. HTML + CSS (下)【更新中】

    弹性盒子: 定义:弹性盒子模型是css3中新提出的一种布局方案.是一种为了应对针对不同屏幕宽度不同设备的一整套新的布局方案. 主要是对一个容器中的子元素进行排列.对齐和分配空白空间的方案的调整. 新旧 ...