The Red Button
The Red Button
问题
2
数据2
3
数据3
4
数据4
16
0 1 0
数据2
-1
数据3
0 1 3 2 0
数据4
0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0
对于30%的数据2<=n<=20
对于100%的数据2<=n<=105
解法
一开始的思路是DFS,每个节点最多有两个方向,可以就走,不能就回溯找另一个方向,这样数量大之后就会TLE,自测120多就出不来结果
TLE代码:
#include<bits/stdc++.h>
using namespace std; const int maxn=1e5+10;
int n;
int len;
int dist[maxn];
bool vis[maxn];
bool dfs(int k,int d)
{
if(d==n-1&&(k*2==n||k*2+1==n))
{
dist[d]=k;
dist[n]=0;
return true;
}
dist[d]=k;
// cout<<d<<" :"<<k<<endl;
int ne=(k*2)%n;
if(vis[ne]==false)
{
vis[ne]=true;
if(dfs(ne,d+1))
return true;
vis[ne]=false;
} int nex=(k*2+1)%n;
if(vis[nex]==false)
{
vis[nex]=true;
if(dfs(nex,d+1))
return true;
vis[nex]=false;
} return false;
} int main()
{
int i,j;
cin>>n;
vis[0]=true;
if(n&1)
cout<<"-1"<<endl;
else
{
if(dfs(0,0))
{
for(i=0;i<=n;i++)
{
if(i!=0)
cout<<" ";
cout<<dist[i];
}
} }
return 0;
}
正确解法:
只需标记所有节点一遍即可,第一个走头无路的点就是终点,第二个走投无路的点是倒数第二个终点。。。。
因此,只需标记完所有节点一次,就可得出结果的倒叙。反序后再加上0,就为最终答案。对于偶数直接输出-1

正确代码:
#include<bits/stdc++.h>
using namespace std; const int maxn=1e5+10;
int n; vector<int> dist;
bool vis[maxn];
void dfs(int k)
{
vis[k]=true;
if(!vis[(k*2)%n])
dfs((k*2)%n);
if(!vis[(k*2+1)%n])
dfs((k*2+1)%n);
dist.push_back(k);
} int main()
{
int i,j;
cin>>n;
vis[0]=true;
if(n&1)
cout<<"-1"<<endl;
else
{
dfs(0);
reverse(dist.begin(),dist.end());
dist.push_back(0);
for(i=0;i<dist.size();i++)
cout<<dist[i]<<" ";
cout<<endl;
}
return 0;
}
The Red Button的更多相关文章
- CodeForces - 325E:The Red Button (哈密尔顿 转 欧拉回路)
Piegirl found the red button. You have one last chance to change the inevitable end. The circuit und ...
- BootStrap中的button使用
原文地址:http://www.phloxblog.in/bootstrap-buttons/#.U5xYso2fclm 站点中事件的触发往往依赖于button或者超链接.因此,button能够觉得是 ...
- ReactNative入门(安卓)——API(下)
LayoutAnimation - layout动画 当布局发生改变时的动画模块,它有两个方法: 1. 最常用的方法是 LayoutAnimation.configureNext(conf<Ob ...
- 漫谈Nuclear Web组件化入门篇
目前来看,团队内部前端项目已全面实施组件化开发.组件化的好处太多,如:按需加载.可复用.易维护.可扩展.少挖坑.不改组件代码直接切成服务器端渲染(如Nuclear组件化可以做到,大家叫同构)... 怎 ...
- 基于Nuclear的Web组件-Todo的十一种写法
刀耕火种 刀耕火种是新石器时代残留的农业经营方式.又称迁移农业,为原始生荒耕作制. var TodoApp = Nuclear.create({ add: function (evt) { evt.p ...
- CSS 高级布局技巧
随着 IE8 逐渐退出舞台,很多高级的 CSS 特性都已被浏览器原生支持,再不学下就要过时了. 用 :empty 区分空元素 兼容性:不支持 IE8 /*假如我们有以上列表:*/ <div cl ...
- webpack
webpack 通过一个主文件 .js ,webpack把这个文件所有的依赖文件,都处理打包成js文件 webpack 可以干嘛?1.执行打包 (把require()模块化整合成一个js文件给html ...
- CSS 代码技巧与维护 ★ Mozilla Hacks – the Web developer blog
原文链接:https://hacks.mozilla.org/2016/05/css-coding-techniques/ 译文链接 :http://www.zcfy.cc/article/css-c ...
- 深入学习jQuery选择器系列第八篇——过滤选择器之伪子元素选择器
× 目录 [1]通用形式 [2]反向形式 [3]首尾元素 [4]唯一元素 前面的话 本文是子元素选择器的续篇,主要介绍关于nth-of-type()选择器的内容.该部分内容并非没有出现在<锋利的 ...
随机推荐
- DNS 是什么?如何运作的?
前言 我们在上一篇说到,IP 地址的发明把我们纷乱复杂的网络设备整齐划一地统一在了同一个网络中. 但是类似于 192.168.1.0 这样的地址并不便于人类记忆,于是发明了 域名(Domain Nam ...
- CF1474-B. Different Divisors
CF1474-B. Different Divisors 题意: 题目给出你一个\(d\),要求你找出一个数字\(y\),找到的\(y\)至少有四个整数因子并且任意两个因子之间的差至少为\(d\). ...
- Redis 搭建与配置
Redis 简介 Redis 是一款开源的,ANSI C 语言编写的,高级键值(Key-Value)缓存和支持永久存储 NoSQL 数据库产品, Redis 采用内存(In-Memory)数据集(Da ...
- 接口测试框架Requests
目录 Requests Requests安装 Requests常见接口请求方法构造 请求目标构造 header构造 cookie 构造请求体 Get Query请求 Form请求参数 JSON请求体构 ...
- ThinkCMF框架任意内容包含漏洞分析复现(写入shell+文件包哈)
ThinkCMF框架任意内容包含漏洞分析复现 0x00 简介 ThinkCMF是一款基于PHP+MYSQL开发的中文内容管理框架,底层采用ThinkPHP3.2.3构建.ThinkCMF提出灵活的应用 ...
- Tensorflow+InternalError: Blas GEMM launch failed
[参考1:]https://stackoverflow.com/questions/37337728/tensorflow-internalerror-blas-sgemm-launch-failed ...
- npm ci All In One
npm ci All In One npm 性能优化 npm ci 使用干净的面板安装项目 https://docs.npmjs.com/cli/v6/commands/npm-ci # npm cl ...
- Free Video Player All In One
Free Video Player All In One VLC media player https://github.com/videolan/vlc VideoLAN https://www.v ...
- event duplication bind bug & h5 dataset flag solution
event duplication bind bug & h5 dataset flag solution https://codepen.io/xgqfrms/full/PaRBEy/ OK ...
- auto switch HTTP protocol Chrome Extension
auto switch HTTP protocol Chrome Extension HTTPS auto switch to HTTP VPN https://chrome.google.com/w ...