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()选择器的内容.该部分内容并非没有出现在<锋利的 ...
随机推荐
- 【.NET 与树莓派】PWM 调节LED小灯的亮度
在开始本文内容之前,老周先纠正一个错误.在上一篇中,提到过 Arduino 开发板的 Vin 引脚,文中老周说这个供电口的输入电压不能高于 5.5V.这里有错,被卖家给的使用说明忽悠了,上 Ardui ...
- hdu-1159 1087 1257(dp)
本文就最长公共子序列,最长连续递增子序列的长度,最大连续递增子序列的值进行对比. hdu-1159: Common Subsequence Time Limit: 2000/1000 MS (Java ...
- Github Docs All In One
Github Docs All In One docs https://docs.github.com/en https://github.com/github/docs GitHub REST AP ...
- css background transparent All In One
css background transparent All In One opacity ul { max-height: 100px; /* max-height: 187px; */ overf ...
- Web 页面生命周期 All In One
Web 页面生命周期 All In One Web Page LifeCycle All In One refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允 ...
- TensorFlow & Machine Learning
TensorFlow & Machine Learning TensorFlow 实战 传统方式 规则 + 数据集 => 答案 无监督学习 机器学习 神经元网络 答案 + 数据集 =&g ...
- how to fetch html content in js
how to fetch html content in js same origin CORS fetch('https://cdn.xgqfrms.xyz/') .then(function (r ...
- image to cur (cursor icons)
image to cur (cursor icons) mouse-cursor-pointer https://onlineconvertfree.com/convert-format/jpg-to ...
- git config all in one
git config git global config # git global config $ git config $ git config --list --show-origin $ gi ...
- 专注于大数据分析和数字基建,星盟UICI切入资产管理领域
资产管理行业体系庞大,按领域可以大致分为公募.私募.券商.保险.银行.信托六大领域.面对六大领域百万亿级市场,近年来,也出现了不少初创公司针对资产管理的细分领域提供专有解决方案.而星盟全球投资公司就是 ...