P3034 [USACO11DEC] Cow Photography G/S 题解

P3034 [USACO11DEC] Cow Photography G/S 题解

模拟赛考到的题,来发题解。

解法

因为题中说如果一头奶牛在拍其中一张照片时移动了,它在拍其他四张照片的时候都不会移动,所以设有两头奶牛 A A A B B B,其中 A A A 应该在 B B B 的前面,那只有当奶牛 A A A 跑到 B B B 后面或者 奶牛 B B B 跑到 A A A 前面这两种情况能让两头奶牛的相对位置互换,所以对于这 5 5 5 张照片,如果一头奶牛在另一头奶牛之前的位置超过 5 − 2 = 3 5-2=3 52=3 次,则表明这头奶牛应该在那头奶牛的前面,他们的相对位置就确定下来了。我们只需要确定所有奶牛的相对位置即可。知道了所有奶牛的相对位置即可知道所有奶牛的绝对位置。这个可以在排序的过程中实现。

时间复杂度 O ( n log ⁡ n ) \mathcal{O}(n \log n) O(nlogn),瓶颈在于排序。

代码

#include<bits/stdc++.h>
using namespace std;
namespace fast_IO
{
	/*
	fast Input/Output
	*/
};
using namespace fast_IO;
int n;
struct cow
{
	int x,pos[6];
	inline bool operator<(const cow &rhs) const
	{
		int cnt=0;
		for(int i=1;i<=5;i++) if(pos[i]<rhs.pos[i]) cnt++;
		return cnt>=3;
	}
};
cow a[20010];
unordered_map<int,int> mp;
int main()
{
	read(n);
	for(int i=1,x;i<=n;i++) read(x),a[i].x=x,a[i].pos[1]=i,mp[x]=i;
	for(int i=2,x;i<=5;i++) for(int j=1;j<=n;j++) read(x),a[mp[x]].pos[i]=j;
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++) write(a[i].x),write('\n');
	fwrite(Ouf,1,p3-Ouf,stdout),fflush(stdout);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值