完全的乱搞题啊。。。 被坑的要死。
拿到题目就觉得是规律题加构造题, 然后找了了几个小时无果,只知道n为奇数的时候是一定无解的,然后当n为偶数的时候可能有很多解,但是如果乱选择的话,很有可能形成无解的情况。
然后想到了类似于估价函数之类的东西, 一开始我就想到了让几个关键的点设价值设成很大,然后在构造解的时候尽量不选这些点,然后。。。 发现数据到30左右就出错了。 然后再进一步的想,把每个点都估计一个价值,价值的大小为到0的距离。 然后就可以保证在构造解的时候尽量选离0远的点,这样一直选,一直选,一直选。。。就不可思议的过了。。。
不知道具体证明,但是思想感觉没有错误。因为这题无解的情况就是过早的选到了0. 如果每次都尽量选离0远的点,局部最优可以成为整体最优。
1 second
256 megabytes
standard input
standard output
Piegirl found the red button. You have one last chance to change the inevitable end.
The circuit under the button consists of n nodes, numbered from 0 to n - 1. In order to deactivate the button, the n nodes must be disarmed in a particular order. Node 0 must be disarmed first. After disarming node i, the next node to be disarmed must be either node(2·i) modulo n or node (2·i) + 1 modulo n. The last node to be disarmed must be node 0. Node 0 must be disarmed twice, but all other nodes must be disarmed exactly once.
Your task is to find any such order and print it. If there is no such order, print -1.
Input consists of a single integer n (2 ≤ n ≤ 105).
Print an order in which you can to disarm all nodes. If it is impossible, print -1 instead. If there are multiple orders, print any one of them.
2
0 1 0
3
-1
4
0 1 3 2 0
16
0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define N 100100 struct node
{
int to,next;
}edge[*N]; void check()
{
int n=;
for(int i=;i<n;i++)
{
int a=i*;
a=a%n;
int b=i*+;
b=b%n;
printf("%d: ",i);
if(a!=i)
printf("%d ",a);
if(b!=i)
printf("%d\n",b);
}
} int mark[N];
int g[N][];
int a[N];
int save[N];
queue<int > que[];
int cnt,pre[N]; void add_edge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=pre[u];
pre[u]=cnt++;
} int main()
{
//check();
//freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
//freopen("//home//chen//Desktop//ACM//out.text","w",stdout); cnt=;
memset(pre,-,sizeof(pre));
memset(g,-,sizeof(g));
memset(mark,,sizeof(mark));
int n;
scanf("%d",&n);
if(n%!=)
{
printf("-1");
return ;
}
for(int i=;i<n;i++)
{
for(int j=;j<;j++)
{
int tmp=*i+j;
tmp=tmp%n;
g[i][j]=tmp;
add_edge(tmp,i);
}
}
memset(a,,sizeof(a));
////////////////////////////////////////// 还是要逆过来搜索
int a1=,b=;
que[a1].push();
int num=;
mark[]=;
while(que[a1].size()!=)
{
num++;
swap(a1,b);
while(que[b].size()!=)
{
int cur=que[b].front();
que[b].pop();
for(int p=pre[cur];p!=-;p=edge[p].next)
{
int v=edge[p].to;
if(mark[v]==)
{
mark[v]=;
a[v]=num;
que[a1].push(v);
}
}
}
} memset(mark,,sizeof(mark));
int cnt1=;
save[cnt1++]=;
mark[]=;
save[cnt1++]=;
int tmp=;
while()
{
int tmp1=-,id;
for(int i=;i<;i++)
{
if(mark[ g[tmp][i] ]==) continue;
if(a[ g[tmp][i] ] > tmp1)
{
tmp1=a[ g[tmp][i] ];
id=g[tmp][i];
}
}
tmp=id;
save[cnt1++]=tmp;
mark[tmp]=;
if(tmp==) break;
}
for(int i=;i<cnt1;i++)
printf("%d ",save[i]);
// if(cnt1!=n+1)
//printf("NO\n");
return ;
}