AOJ -0033 Ball(DFS)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22516
一道需要思考的搜索题。
题意:十个球按给定顺序从图中所示容器中下落,然后挡板可以让球落在左边或者右边,问给定球的顺序,是否存在两边从低到高都是递增的情况。
解1:只要给定的球能够分成两个递增的序列,那么一定是满足条件的,用dfs可以找出以第一个球为首的递增序列,把其标记,然后判断剩下的球是不是也构成一个递增序列。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define maxn 1000000000
#define N 110
using namespace std; int arr[],used[]; void dfs(int pre,int cur)
{
if(cur<&&arr[pre]<arr[cur])
{
used[cur]=;
dfs(cur,cur+);
}
else if(cur<&&arr[pre]>=arr[cur])
dfs(pre,cur+);
}
int main()
{
//freopen("a.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
bool judge=false;
for(int i=;i<;i++)
scanf("%d",&arr[i]);
for(int i=;i<;i++)
{
memset(used,,sizeof(used));
used[i]=;
dfs(i,i+);
int pre=;
judge=false;
for(int j=;j<;j++)
{
if(!used[j])
{
if(pre>=arr[j])
{
judge=true;
break;
}
pre=arr[j];
}
}
if(!judge)
{
printf("YES\n");
break;
}
}
if(judge) printf("NO\n");
}
return ;
}
解2:当然可以采用二进制枚举(第一次知道这个算法),每个球要么落在左边要么在右边,10个球总共2的10次方=1024种状态。0-2013每一个数都代表一种状态。不断枚举直到找到合适的为止。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define maxn 1000000000
#define N 110
using namespace std; int main()
{
//freopen("a.txt","r",stdin);
int t;
int arr[],l[],r[];
scanf("%d",&t);
while(t--)
{
int i,j,cnt;
bool flag;
for(i=;i<;i++) scanf("%d",&arr[i]);
for(i=;i<;i++)
{
int x=,y=;
for(cnt=,j=i;cnt<;cnt++,j>>=)
{
if(j&) l[x++]=arr[cnt];
else r[y++]=arr[cnt];
}
flag=true;
for(j=;j<x;j++)
{
if(l[j]<l[j-]) {flag=false;break;}
}
if(flag)
{
for(j=;j<y;j++)
{
if(r[j]<r[j-]) {flag=false;break;}
}
}
if(flag) {printf("YES\n");break;}
}
if(!flag) printf("NO\n");
}
return ;
}
AOJ -0033 Ball(DFS)的更多相关文章
- AOJ 0033 Ball【DFS】
有一个筒,从A口可以放球,放进去的球可通过挡板DE使其掉进B管或C管里,现有带1-10标号的球按给定顺序从A口放入,问是否有一种控制挡板的策略可以使B管和C管中的球从下往上标号递增. 输入: 第一行输 ...
- aoj 0033 Ball【dfs/枚举】
有一个形似央视大楼(Orz)的筒,从A口可以放球,放进去的球可通过挡板DE使其掉进B裤管或C裤管里,现有带1-10标号的球按给定顺序从A口放入,问是否有一种控制挡板的策略可以使B裤管和C裤管中的球从下 ...
- Aizu 0033 Ball(dfs,贪心)
日文题面...题意:是把一连串的有编号的球往左或者往右边放.问能不能两边都升序. 记录左边和右边最上面的球编号大小,没有就-1,dfs往能放的上面放. #include<bits/stdc++. ...
- Aizu/Aoj 0033 Ball
题目大意: 有编号1到10共10个球,从上方丢下去,入口处可以选择进入左边或者右边,最后10个球全部落下去后如果左右两侧都是从小到大的顺序,则输出YES:否则输出NO. 题目原本的标签枚举,复杂度是2 ...
- AOJ 0033 深度优先搜索
题意:按给定顺序从A口放标号位1-10的10个球,利用挡板可以使球落到B或C,问能否使B和C里的球标号从下往上递增. 分析:对于第 i 个球,若a[i]大于B口上方的球,则可放入B口:若a[i]大于C ...
- aoj 0033 玉
図のように二股に分かれている容器があります.1 から 10 までの番号が付けられた10 個の玉を容器の開口部 A から落とし.左の筒 B か右の筒 C に玉を入れます.板 D は支点 E を中心に左右 ...
- nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……
Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...
- ProgrammingContestChallengeBook
POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...
- HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- Ref 和 Out 区别(演练代码)
一.代码 今天就总结Ref和Out 的总结,这东西,也是经常面试过程中,笔试经常考的,比如:请简述Ref和Out 的区别,或者通过一段代码让你计算这过程的结果.... Out代码实例::: stati ...
- R in action读书笔记(17)第十二章 重抽样与自助法
12.4 置换检验点评 除coin和lmPerm包外,R还提供了其他可做置换检验的包.perm包能实现coin包中的部分功能,因此可作为coin包所得结果的验证.corrperm包提供了有重复测量的相 ...
- linux下php访问sql server设置
安装freeIDS 官网下载地址: wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.18.tar.gz 1.1.到下载目录解压 t ...
- 解决hibernate对Sql Server分页慢的问题
一.hibernate分页 hibernate对MsSql的伪分页 分页是web项目中比不可少的一个功能,数据量大的时候不能全部展示必然要用到分页技术.相信大家对hibernate中的分页都不陌生: ...
- 【java_设计模式】建造者模式
应用场景对生成对象进行链式配置好处:按需配置对象,灵活修改默认配置,比构造方法创建对象更直观. public static void init() {if (okHttpClient == null) ...
- windows sdk编程禁止改变窗体大小
#include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...
- 在mac下做web开发,shell常用的快捷键
Ctrl + A 光标移动到行首 Ctrl + E 光标移动到行末 Ctrl + K 清屏(也可是用clear命令) Command +shift+{} 终端的tab左右切换
- 04Struts2的配置文件
Struts2的配置文件 1.1 struts.xml中的标签详解 1.1.1 constant标签 作用: 用于修改struts2中的常量 属性: name:指定常量的key value:指 ...
- pom.xml配置引用项目时不生效
1 在项目pom.xml配置中引用项目A,但是编译时,取提数引起是B: 2 原因是:[Java Build Path - Projects] 引用的还是老的项目B,删除该引用即可解决.
- Server.MapPath() 用法
Server.MapPath() ./当前目录/网站主目录../上层目录~/网站虚拟目录 如果当前的网站目录为E:\wwwroot 应用程序虚拟目录为E:\wwwroot\company 浏览的页 ...