2014 ACM ICPC World Finals A baggage。
为什么都看过题解还做了这么久啊(
一个重要的想法是证明答案的下界是不低于 的。
我们发现如果想要构造合法序列那么前后相同的链接数量为 ,一次操作只能造成最多两个合法链接,所以下界操作次数可以证明不低于 。
对于 的答案通过爆搜发现最小的操作次数都是 ,推测 的答案也是 。
发现存在这样一种构造方案,设 为 , 为 :
move(R - 2 , L - 2) , move(L + 2 , R - 2);那么中间的节点就会变为一个 规模的问题,递归处理; 结束后再进行一次
move(L - 1 , R - 5) , move(R - 1 , L - 1);即可解决原问题,因为样例就是这么构造的所以这里的思维难度其实还好((((((
所以有谁教我怎么写比较块的搜索吗。
/*
考虑一个问题的归约。
由于 n >= 3,我们先来看一下 n >= 3 的答案。
搞了 30min。。。
等一下,也就是说我还要把 n <= 4 的情况全部打出来表。。。
*/
#include "bits/stdc++.h"
using namespace std;
#define ll long long
const int Len = 405;
int n,m;
struct Node
{
int x,y;
Node(){x = y = 0;}
Node(int X,int Y){x = X , y = Y;}
};
vector<Node> Psy[15];
inline void pt(int x,int y){printf("%d to %d\n",x,y);}
void work(int N,int ad)
{
int L = ad + 1 , R = ad + 2 * N;//[1,2n]
if(N <= 7)
{
if(N <= 4) for(int i = 0 ; i < Psy[N].size() ; i ++) printf("%d to %d\n",Psy[N][i].x + ad,Psy[N][i].y + ad);
else
{
if(N == 5)
{
pt(R - 2 , L - 2);
pt(L + 2 , R - 2);
pt(R - 4 , L + 2);
pt(L - 1 , R - 4);
pt(R - 1 , L - 1);
}
if(N == 6)
{
pt(R - 2 , L - 2);
pt(R - 5 , R - 2);
pt(L + 1 , R - 5);
pt(R - 6 , L + 1);
pt(L - 1 , R - 6);
pt(R - 1 , L - 1);
}
if(N == 7)
{
pt(R - 6 , L - 2);
pt(L + 4 , R - 6);
pt(R - 2 , L + 4);
pt(L + 2 , R - 2);
pt(R - 5 , L + 2);
pt(L - 1 , R - 5);
pt(R - 1 , L - 1);
}
}
return;
}
else
{
pt(R - 2 , L - 2) , pt(L + 2 , R - 2);
work(N - 4 , ad + 4);
pt(L - 1 , R - 5) , pt(R - 1 , L - 1);
}
}
map<ll,ll> dp,pre[3];//B:1,A:2
int now[Len];
bool check()
{
for(int i = 1 ; i <= 4 * n ; i ++)
{
if(now[i] == 0) continue;
if(now[i])
{
for(int j = i ; j <= i + n - 1 ; j ++) if(now[j] != 2) return 0;
for(int j = i + n ; j <= i + 2 * n - 1 ; j ++) if(now[j] != 1) return 0;
}
return 1;
}
return 0;
}ll bg;
inline ll ds(){ll res = 0,ml = 1;for(int i = 1 ; i <= (n * 4) ; i ++) res += ml * now[i] , ml *= 3;return res;}
inline void rec(ll ml){for(int i = 1 ; i <= (4 * n) ; i ++) now[i] = ml % 3 , ml /= 3;}
inline void bk(ll Now){if(!dp[Now] && Now != bg) dp[Now] = 114;}
inline void Out(){for(int i = 1 ; i <= (4 * n) ; i ++) printf("%d ",now[i]);puts("");}
bool flaw;
void dfs(int as)
{
if(flaw) return;
if(as >= 6) return;
ll Now = ds();bk(Now);
if(as > dp[Now]) return;
dp[Now] = as;
//if(as == 3){printf("%d %lld\n",as,Now);Out();}
if(check())
{
if(as == 5) flaw = 1;
return;
}
for(int i = 1 ; i < (4 * n) ; i ++)
{
if(now[i] && now[i + 1])
{
for(int j = 1 ; j < (4 * n) ; j ++)
{
if(!now[j] && !now[j + 1])
{
int a = now[i] , b = now[i + 1];
now[i] = now[i + 1] = 0;
now[j] = a , now[j + 1] = b;
ll to = ds();bk(to);
if(dp[to] > as + 1)
{
pre[0][to] = Now , pre[1][to] = i - 2 * n , pre[2][to] = j - 2 * n;
dfs(as + 1);
}
now[j] = 0 , now[j + 1] = 0;
now[i] = a , now[i + 1] = b;
}
}
}
}
}
void Output(ll rec)
{
if(rec == bg) return;
Output(pre[0][rec]);
printf("Psy[5].push_back(Node(%lld, %lld));\n",pre[1][rec],pre[2][rec]);
}
int main()
{
/*n = 5;int op = 1;
for(int i = 2 * n + 1 ; i <= 4 * n ; i ++) now[i] = op , op = 3 - op;
bg = ds();
dfs(0);
const int LEN = 2 * n;ll as = 114,ass = 0;
for(int i = 2 * n ; i <= 4 * n ; i ++)
{
ll rs = 0;int l = i - LEN + 1;ll ml = 1;
for(int j = 1 ; j < l ; j ++) ml *= 3;
for(int j = l ; j <= l + n - 1 ; j ++)
{
rs += 2 * ml;
ml *= 3;
}
for(int j = l + n ; j <= l + LEN - 1 ; j ++) rs += ml , ml *= 3;
if(dp[rs] && dp[rs] < as) as = dp[rs] , ass = rs;
}
if(ass) Output(ass);*/
Psy[3].push_back(Node(2, -1));
Psy[3].push_back(Node(5, 2));
Psy[3].push_back(Node(3, -3));
Psy[4].push_back(Node(6, -1));
Psy[4].push_back(Node(3, 6));
Psy[4].push_back(Node(0, 3));
Psy[4].push_back(Node(7, 0));
scanf("%d",&n);work(n , 0);
return 0;
}