标签:iostream acm 数据 结构 namespace
1 5 5 1 2 3 5 6 1 2 3 4 5
4 4 4 4 7
暴力超时 可以用并查集或哈希做下面是并查集的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define M 200000+5
using namespace std;
int f[M];
int find(int x)
{
if(f[x]==-1)return x;
else
return f[x]=find(f[x]);
}
int getfind(int a,int b)
{
int x=find(a);
int y=find(b);
if(x!=y)
f[x]=y;
}
int main()
{
int a,n,m,q,i,k,p;
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&m);
memset(f,-1,sizeof f);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
getfind(a,a+1);
}
while(m--)
{
scanf("%d",&q);
printf("%d\n",find(q));
}
}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define M 100010
using namespace std;
int a[M],b[2*M];
int main()
{
int i,j;
int t;
int n,m;
scanf("%d",&t);
while(t--)
{
memset(b,0,sizeof b);
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[a[i]]=-1;
}
int last=2*M;
for(i=2*M-1;i>=1;i--)
{
if(b[i]==0)
{last=i;b[i]=i;}
else b[i]=last;
}
while(m--)
{
int cc;
scanf("%d",&cc);
printf("%d\n",b[cc]);
}
}
return 0;
}BestCoder Round #3 A hdu 4907 Task schedule,布布扣,bubuko.com
BestCoder Round #3 A hdu 4907 Task schedule
标签:iostream acm 数据 结构 namespace
原文地址:http://blog.csdn.net/fanerxiaoqinnian/article/details/38364775