Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数
传送门:点我
题意:给定n长度的序列,重排成后一个数是前一个数除以三,或者后一个数是前一个数乘二,要求输出这个序列。
思路:大力深搜,对每个数搜除3的和乘2的是否出现过,然后继续搜下去。如果有一个数搜能搜完整个序列,那就输出这个数开始的一整个序列。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long LL;
int ans = ,flag = ;
int n,k;
map<LL,int>ma;
LL b[];
void dfs(LL x){
if(ans == n - ){
flag = ;
for(int i = ; i <= ans; i++){
i == ans ?printf("%I64u\n",b[i]):printf("%I64u ",b[i]);
}
return;
}
if(x%==&&ma[x/] == ){
ans++;
b[ans] = x/;
dfs(x/);
}
if(ma[x*] == ){
ans++;
b[ans] = x*;
dfs(x*);
}
}
int main(){
scanf("%d",&n);
LL a[];
for(int i = ; i < n ; i ++){
cin>>a[i];
ma[a[i]] = ;
}
vector<LL>v;
for(int i = ; i < n ; i++){
ans = ;
b[] = a[i];
dfs(a[i]);
if(flag){
return ;
}
}
}
Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数的更多相关文章
- Divide by three, multiply by two(DFS+思维)
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, an ...
- Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two
传送门 D. Divide by three, multiply by two •题意 给你一个数 x,有以下两种操作,x 可以任选其中一种操作得到数 y 1.如果x可以被3整除,y=x/3 2.y= ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- Network Saboteur (深搜递归思想的特殊使用)
个人心得:对于深搜的使用还是不到位,对于递归的含义还是不太清楚!本来想着用深搜构成一个排列,然后从一到n分割成俩个数组,然后后面发现根本实现不了,思路太混乱.后来借鉴了网上的思想,发现用数组来标志,当 ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
随机推荐
- springboot 整合 redis 共享Session-spring-session-data-redis
参考:https://www.cnblogs.com/ityouknow/p/5748830.html 如何使用 1.引入 spring-boot-starter-redis <dependen ...
- k8s资料转载
K8S入门(二) kubeadmin单机部署 (kubernetes)k8s入门.yum单机版安装.kuberctl指令.k8s服务实例. kubernetes---CentOS7安装kubernet ...
- nginx 配置文件配置
server { listen 80 ; server_name test.com www.test.com; index index.html index.php index.htm; root / ...
- package-info.java
参考文章: http://blog.sina.com.cn/s/blog_93dc666c0101gzlr.html 对于package-info.java我们并不陌生,但又陌生. 在我们每次建立pa ...
- C#中关于@的用法
1. 加在字符串前面,字符串中的 \ 失去转义符的作用,直接写字符串而不需要考虑转义字符 string path = @"C:\Windows\"; // 如果不加 @,编译会提示 ...
- react native 中es6语法解析
react native是直接使用es6来编写代码,许多新语法能提高我们的工作效率 解构赋值 var { StyleSheet, Text, View } = React; 这句代码是ES6 中新增的 ...
- fopen函数出现段错误
昨天写代码的时候突然发现了一个问题,当使用fopen("<filepath>", "r")时,如果filepath不存在,那么fopen函数并不是像 ...
- angularjs 请求数据转换为Form Data传参
在angularjs中配置好服务,有时传参会导致后台借不到值或者后台直接报错: 这就与后台框架有关,如果后台是以public ModelAndView接收接口传过来的参数,这种情况,前台传参的形式比较 ...
- 5.Java中的数组.md
1.Java的数组定义 Java中的定义有两种形式: type[] arraryName; //推荐形式 type arrayName[]; //不推荐 前一种有更好的语义,可读性更好.但是需要注意的 ...
- Java学习05 (第一遍) - JSP与Servlet
JSP 客户端发出Request请求,JSP容器将JSP转译为Servlet的源码,再编译加载到内存执行,结果Response到客户端. Request->JSP->Servlet(jav ...