有谁来hack一下我的做法吗
查看原帖
有谁来hack一下我的做法吗
329698
youdu666楼主2022/8/26 13:19

算法:堆、贪心,20pts

#include<cstdio>
#include<bitset>
#include<cstring>
using namespace std;
inline int read()
{
	int x=0,y=1;
	char c=getchar();
	while(c>'9'||c<'0')
	{
		if(c=='-')
		y=-1;
		c=getchar();
	}
	while(c<='9'&&c>='0')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*y;
}
const int N=505;
struct heap{
	int x,ip;
}hp[N];
int l;
int n,a[N][N],go[N],ans[N],gtn;
bitset<N> usd;
inline void S(int a,int b)
{
	hp[0]=hp[a];
	hp[a]=hp[b];
	hp[b]=hp[0];
}
inline void up(int x)
{
	int k=0,j=x;
	while(j>1)
	{
		k=j>>1;
		if(hp[k].x>hp[j].x) S(k,j);
		j=k;
	}
}
inline void down(int x)
{
	int k=0,j=x;
	while(j<<1<=l)
	{
		k=j<<1;
		if(k<l&&hp[k|1].x<hp[k].x) k|=1;
		if(hp[k].x<hp[j].x) S(k,j);
		j=k;
	}
}
signed main()
{
	n=read();
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			a[i][j]=read();
		hp[++l]=(heap){1,i};
		go[i]=1;
		up(l);
	}
	while(gtn<n)
	{
		int c=hp[1].ip,d=a[hp[1].ip][hp[1].x];
//		printf("%d %d\n",c,d);
		hp[1]=hp[l--];
		down(1);
		if(ans[c]) continue;
		if(go[c]<n)
		hp[++l]=(heap){++go[c],c};
		if(usd[d]) continue;
		usd[d]=1;
		gtn++;
		ans[c]=d;
	}
	for(int i=1;i<=n;i++)
	printf("%d\n",ans[i]);
}
2022/8/26 13:19
加载中...