HDU-4665 Unshuffle 搜索 | 2-SAT

时间:2022-12-09 23:03:52

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4665

  本题的2-SAT建图颇为复杂,有时间再来填坑(自己写的一直挂着,标程建图太复杂了)。。。然后用暴力搜索,用一个栈保存第一个序列就可以了,因为题目是SPG,并且重复的最多只有四个,所以搜索很好过。。

 //STATUS:C++_AC_62MS_276KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD= ,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int num[N],sta[N],vis[N];
int T,n,top1,top2,mid; bool dfs(int u)
{
if(top2==mid)return true;
if(top1<mid){
vis[u]=;
sta[top1++]=num[u];
if(dfs(u+))return true;
top1--;
}
if(num[u]==sta[top2]){
vis[u]=;
top2++;
if(dfs(u+))return true;
vis[u]=;
top2--;
}
return false;
} int main(){
// freopen("in.txt","r",stdin);
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
mid=n>>;
for(i=;i<n;i++)
scanf("%d",&num[i]); top1=top2=;
dfs(); for(i=;i<n;i++)
printf("%d",vis[i]);
putchar('\n');
}
return ;
}

2-SAT(WA):

 /*   WA   */
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD= ,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v;
}e[N<<]; int first[N<<],next[N<<],vis[N<<],S[N<<];
int num[N];
int T,n,mt,cnt; void adde(int a,int b)
{
e[mt].u=a,e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
} int dfs(int u)
{
if(vis[u^])return ;
if(vis[u])return ;
int i;
vis[u]=;
S[cnt++]=u;
for(i=first[u];i!=-;i=next[i]){
if(!dfs(e[i].v))return ;
}
return ;
} int Twosat()
{
int i,j;
for(i=;i<n;i+=){
if(vis[i] || vis[i^])continue;
cnt=;
if(!dfs(i)){
while(cnt)vis[S[--cnt]]=;
if(!dfs(i^))return ;
}
}
return ;
} int cnt2[N],tot[N],w[N][];
void Init()
{
int i,j,t;
mt=;mem(vis,);
mem(first,-);
mem(tot,);
for(i=;i<n;i++)w[num[i]][tot[num[i]]++]=i;
mem(cnt2,);
for(i=;i<n;i++){
t=num[i];
cnt2[t]++;
if(cnt2[t]==){
adde((i<<)^,i<<);
adde(i<<,(w[t][tot[t]-]<<)^);
adde(w[t][tot[t]-]<<,(i<<)^);
if(tot[t]==){
for(j=w[t][]+;j<n;j++){
if(w[num[j]][]==j && w[num[j]][]<j && w[num[j]][]>i){
adde(i<<,(w[t][]<<)^);
break;
}
}
}
}
else if(cnt2[t]==){
adde(w[t][]<<,(w[t][]<<)^);
adde((w[t][]<<)^,w[t][]<<);
adde(w[t][]<<,(w[t][]<<)^);
adde((w[t][]<<)^,w[t][]<<);
}
}
} int main(){
freopen("in.txt","r",stdin);
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%d",&num[i]); Init();
n<<=;
int t=Twosat();
// printf("%d\n",t); for(i=;i<n;i+=)
printf("%d",vis[i]);
putchar('\n');
}
return ;
}

HDU-4665 Unshuffle 搜索 | 2-SAT的更多相关文章

  1. HDU 4665 Unshuffle (2013多校6 1011 )

    Unshuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. HDU 4665 Unshuffle DFS找一个可行解

    每层找一对相等的整数,分别放在两个不同的串中. 参考了学弟的解法,果断觉得自己老了…… #include <cstdio> #include <cstring> #includ ...

  3. hdu 4665 搜索

    思路:直接搜索 #include<iostream> #include<cstdio> #include<algorithm> #include<cstrin ...

  4. hdu 5468&lpar;莫比乌斯&plus;搜索&rpar;

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  5. HDU 4499&period;Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  6. HDU 1045 &lpar;DFS搜索&rpar;

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  7. HDU 1180 &lpar;BFS搜索&rpar;

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  8. HDU 2531 &lpar;BFS搜索&rpar;

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  9. HDU 1026 &lpar;BFS搜索&plus;优先队列&plus;记录方案&rpar;

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

随机推荐

  1. zoj Fibonacci Numbers &lpar; java &comma; 简单 ,大数&rpar;

    题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; imp ...

  2. Google中rel&equals;&quot&semi;canonical&quot&semi;的相关解释和用法

    转载原地址 http://blog.sina.com.cn/s/blog_673b01740100jxlz.html 近听到很多SEO 对在页面的规范版本用规范 URL 标签( canonical U ...

  3. 一步一步制作yaffs&sol;yaffs2根文件系统&lpar;二&rpar;---安装BusyBox,构造&sol;bin、&sol;sbin、&sol;usr、linuxr

    开发环境:Ubuntu 12.04 开发板:mini2440  256M NandFlash   64M SDRAM 交叉编译器:arm-linux-gcc 4.4.3点此可下载 BusyBox版本: ...

  4. zendstudio正则匹配查询

    Ctrl+H之后,显示的File Search标签页为Containing text. Alt+/ 帮助提示正则匹配的语法. 例子如下: select type from table where id ...

  5. c&num;下载文件案例

    public static void HttpDown(string fileName, System.Web.UI.Page p_Page,string floder) { string path ...

  6. mac环境下安装xampp

    首先下载XAMPP,然后配置虚拟域名hosts,再配置Apache服务, 配置Apache服务 1.打开/Applications/XAMPP/xamppfiles/etc/httpd.conf文件, ...

  7. Angular随笔第二课

    一.  列表表格以及其它迭代型元素 ng-repeat 可能是最有用的angular指令了,它可以根据集合中的项目一次创建一组元素的多份拷贝.不管在什么地方,只要你想创建一组事物的列表,你就可以使用这 ...

  8. js数组和对象相等判断、拷贝详解&lpar;结合几个现象讲解引用数据类型的趣事&rpar;

    序言 最近遇到几个js引用数据类型造成的bug,今天结合bug详细分析一下,避免以后再犯,也希望能帮大家提个醒,强化js基本功. 目录 1.浅拷贝.深拷贝,解决变量赋值相互影响问题 2.判断2个数组. ...

  9. sf-1 算法

    算法基础 算法 算法(Algorithm):一个计算过程,解决问题的方法 DNiklaus Wirth:“程序=数据结构+算法” 时间复杂度 时间复杂度:用来评估算法运行效率的一个式子 时间复杂度-小 ...

  10. list集合与HashMap的使用

    List<Map<String, Object>> arrays= new ArrayList<Map<String, Object>>(); Hash ...