CodeForces - 1343F Restore the Permutation by Sorted Segments(思维)

题目链接:点击查看

题目大意:现在有一个长度为 n 的排列 p ,但排列 p 暂时对我们保密,每个样例将会给出 n - 1 个排好序后的子段,换句话说,对于 r ∈ [ 2 , n ] ,存在一个 l 满足 l <  r ,题目会给出排列 p 中 [ l , r ] 这段,只不过是排好序后给出,现在让我们还原排列 p 

题目分析:因为 n 非常小,只有 200 ,所以可以考虑 n^3 的算法,首先应该注意的地方是,对于所有子段的左右区间而言,虽然 l 并不确定,但是 r 是满足 [ 2 , n ] 中每个位置都有且仅有一个的,这也就保证了答案的唯一性,因为假如已经知道了给出的是哪些区间,按照区间的右端点 r 排个序,就会发现当前某个区间内至少会有一个数是前一个区间内不存在的

到这里我们可以类比于拓扑的思想,因为第一个数是未知的,所以我们可以直接 O( n ) 去枚举,现在确定好第一个数是什么了之后,可以对于所有的区间,都将第一个数删去,那么 r == 2 的这组数据在删去了第一个数后,就只剩下了第二个位置的数,同理,每次迭代到 i 时,因为已经将前 i - 1 个数都删去了,那么 r == i 的这组数据按理说应该只剩下第 i 个位置的数了,如此往复就能求出排列 p 了

上述过程我们可以利用 set 完成,因为 set 的查找、插入和删除效率都非常的高,每次找第 i 个位置的数时查找 size == 1 的区间即可,同时需要注意的是,在迭代的过程中,如果存在某一时刻:

  1. 没有 size == 1 的区间
  2. size == 1 的区间个数大于 1 

如果存在上述两种情况之一的话,那就说明此时是无解的

此时我们已经构造出了一种可行方案 p 了,可不能着急输出,具体参见第三个样例,这就提醒我们需要检查一下构造出的方案是否合理,具体的检查方法就是 O( n * n ) 枚举出所有的子段,如果存在 n - 1 个子段可以和题目给出的数据全部一一对应的话,那就说明答案合理,反之不合理

思路很简单,但是实现还是有一定难度的,时间复杂度为 O( n * n * n * logn )

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;

typedef long long LL;

typedef unsigned long long ull;

const int inf=0x3f3f3f3f;

const int N=2e5+100;

int main()
{
#ifndef ONLINE_JUDGE
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
#endif
//	ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--)
	{
		vector<set<int>>seg;
		int n;
		scanf("%d",&n);
		for(int i=1;i<n;i++)
		{
			set<int>st;
			int k;
			scanf("%d",&k);
			while(k--)
			{
				int num;
				scanf("%d",&num);
				st.insert(num);
			}
			seg.push_back(st);
		}
		for(int st=1;st<=n;st++)//枚举第一个数 
		{
			vector<int>ans;
			vector<set<int>>cur=seg;
			bool flag=true;
			for(auto &it:cur)
				if(it.count(st))
					it.erase(st);
			ans.push_back(st);
			for(int i=1;i<n;i++)//迭代n-1次,找出剩余n-1个数 
			{
				vector<int>temp;
				for(auto &it:cur)
					if(it.size()==1)
						temp.push_back(*it.begin());
				if(temp.size()!=1)//无法找到 
				{
					flag=false;
					break;
				}
				ans.push_back(temp.front());
				for(auto &it:cur)
					if(it.count(temp.front()))
						it.erase(temp.front());
			}
			set<set<int>>all(seg.begin(),seg.end());
			if(flag)//构造出数列后,判断是否合理
			{
				for(int l=0;l<n-1;l++)
				{
					set<int>st;
					for(int r=l;r<n;r++)
					{
						st.insert(ans[r]);
						if(all.count(st))
							all.erase(st);
					}
				}
			}
			if(all.empty())//判断合理的话,直接输出答案
			{
				for(auto it:ans)
					printf("%d ",it);
				puts("");
				break;
			}
		}
	}











    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Frozen_Guardian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值