Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two (DFS)
题意:给你一个长度为\(n\)的序列\(a\).对它重新排列,使得\(a_{i+1}=a_{i}/3\)或\(a_{i+1}=2*a_{i}\).输出重新排列后的序列.
题解:经典DFS,遍历这个序列,对每个数dfs,每次dfs使\(len+1\),当\(len=n\)时,说明这个序列已经满足条件了,输出并且标记一下,防止还有另外的情况导致多输出.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
using namespace std;
typedef pair<int,int> PII;
typedef pair<long,long> PLL; int n;
bool st[N];
int len;
ll ans[N];
ll a[N];
bool ok;
void dfs(int x,int len){
if(len==n && !ok){
ok=1;
for(int i=1;i<=n;++i) printf("%lld ",ans[i]);
return;
}
st[x]=true;
for(int i=1;i<=n;++i){
if(!st[i] && (a[i]*3==ans[len] || ans[len]*2==a[i])){
ans[len+1]=a[i];
dfs(i,len+1);
}
}
st[x]=false;
} int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>n;
for(int i=1;i<=n;++i) cin>>a[i]; for(int i=1;i<=n;++i){
ans[1]=a[i];
memset(st,0,sizeof(st));
dfs(i,1);
} return 0;
}
Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two (DFS)的更多相关文章
- 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= ...
- Codeforces Round #622 (Div. 2) A. Fast Food Restaurant(全排列,DFS)
Codeforces Round #622 (Div. 2) A. Fast Food Restaurant 题意: 你是餐馆老板,虽然只会做三道菜,上菜时还有个怪癖:一位客人至少上一道菜,且一种菜最 ...
- Codeforces Round #479 (Div. 3)解题报告
题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...
- Codeforces Round #479 (Div. 3) 题解 977A 977B 977C 977D 977E 977F
A. Wrong Subtraction 题目大意: 定义一种运算,让你去模拟 题解: 模拟 #include <iostream> #include <cstdio> ...
- Codeforces Round #479 (Div. 3)题解
CF首次推出div3给我这种辣鸡做,当然得写份博客纪念下 A. Wrong Subtraction time limit per test 1 second memory limit per test ...
- Codeforces Round #479 (Div. 3)
手速场2333,这群人贼牛逼!手速贼快! A. Wrong Subtraction time limit per test 1 second memory limit per test 256 m ...
- Codeforces Round #479 (Div. 3)解题代码
A. Wrong Subtraction #include <bits/stdc++.h> using namespace std; int main() { int n,k; cin&g ...
- Codeforces Round #479 (Div. 3) A. Wrong Subtraction
题目网址:http://codeforces.com/contest/977/problem/A 题解:给你一个数n,进行k次变换,从末尾开始-1,512变成511,511变成510,510会把0消掉 ...
- Codeforces Round #479 (Div. 3) C. Less or Equal
题目地址:http://codeforces.com/contest/977/problem/C 题解:给一串数组,是否找到一个数x,找到k个数字<=x,找到输出x,不能输出-1.例如第二组,要 ...
随机推荐
- python_元组(tuple)
#tuple(),元组不可以修改,不能对其进行增加或删除操作,元组是有序的 #1.定义 tu_1 = () #定义一个空元组 tu_2 = (1,2,'alex',[3,4],(5,6,7),True ...
- 【葵花宝典】一天掌握Docker
第1章Docker 概述 1-1 Docker是什么 没有虚拟化技术的原始年代 我们仔细想想,在没有计算虚拟化技术的"远古"年代,如果我们要部署一个应用程序(Application ...
- 人工智能"眼睛"——摄像头
摄像头机器视觉人工智能的"眼睛",其重要性在嵌入式领域不言而喻.但是如何理解和使用摄像头却是一个非常棘手的问题.本文主要针对调试摄像头过程中遇到的问题,对摄像头的基本原理及概述进行 ...
- Java 给Word不同页面设置不同背景
Word文档中,可直接通过[设计]-[页面颜色]页面颜色,通过Java代码可参考如下设置方法: 1. 设置单一颜色背景 doc.getBackground().setType(BackgroundTy ...
- Failed to start ssh.service: Unit not found.
Failed to start ssh.service: Unit not found. 报错内容: [Centos7@localhost ~]$ service ssh start Redirect ...
- Java SPI机制详解
Java SPI机制详解 1.什么是SPI? SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制.SPI是一种动态替换发现的机制, 比如有个 ...
- Fody,告别烦人的INotifyPropertyChanged,最简方式实现通知!
INotifyPropertyChanged 我不是针对谁,我是说在座的各位 相信所有学wpf的,都写过类似下面的代码: 实现INotifyPropertyChanged public class M ...
- OpenStack (nova 计算服务)
nova介绍 Nova 负责维护和管理云环境的计算资源,Nova这个模块很重要,可以说是 OpenStack 的最核心的服务模块之一,以至于在 OpenStack 的初期版本里大部分的云系统管理功能都 ...
- python3 自动部署MariaDB主从复制
master import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.rep ...
- (31)sed命令完全攻略
1.sed简介 Vim 采用的是交互式文本编辑模式,你可以用键盘命令来交互性地插入.删除或替换数据中的文本.但本节要讲的 sed 命令不同,它采用的是流编辑模式,最明显的特点是,在 sed 处理数据之 ...