P3034 [USACO11DEC]牛摄影Cow Photography

假如想象 提交于 2019-12-03 21:12:41

题目描述

The cows are in a particularly mischievous mood today! All Farmer John wants to do is take a photograph of the cows standing in a line, but they keep moving right before he has a chance to snap the picture.

Specifically, each of FJ's N (1 <= N <= 20,000) cows has a unique integer ID number. FJ wants to take a picture of the cows standing in a line in a very specific ordering, represented by the contents of an array A[1...N], where A[j] gives the ID number of the jth cow in the ordering. He arranges the cows in this order, but just before he can press the button on his camera to snap the picture, a group of zero or more cows (not necessarily a contiguous group) moves to a set of new positions in the lineup. More precisely, a group of zero or more cows steps away from the line, with the remaining cows shifting over to close the resulting gaps in the lineup. The cows who stepped away then re-insert themselves at different positions in the lineup (not necessarily at the locations they originally occupied). Frustrated but not deterred, FJ again arranges his cows according to the ordering in A, but again, right before he can snap a picture, a different group of zero or more cows moves to a set of new positions in the lineup.

The process above repeats for a total of five photographs before FJ gives up. Given the contents of each photograph, see if you can reconstruct the original intended ordering A. Each photograph shows an ordering of the cows that differs from A in that some group of zero or more cows has moved. However, a cow only moves in at most one photograph: if a cow is part of the group that moves in one photograph, she will not actively move in any of the other four photographs (although she could end up at a different index as a consequence of other cows around her moving, of course).

Farmer John居然拍了五张照片,每张表示初始化牛位置的移动后的状态。然后求出FJ这五头牛初始化的样子。

输入格式

* Line 1: The number of cows, N (1 <= N <= 20,000).

* Lines 2..5N+1: The next 5N lines describe five orderings, each one a block of N contiguous lines. Each line contains the ID of a cow, an integer in the range 0...1,000,000,000.

输出格式

* Lines 1..N: The intended ordering A, one ID per line.

输入输出样例

输入 #1
5 
10 
20 
30 
40 
50 
20 
10 
30 
40 
50 
30 
10 
20 
40 
50 
40 
10 
20 
30 
50 
50 
10 
20 
30 
40 
输出 #1
10 
20 
30 
40 
50 

说明/提示

There are 5 cows, with IDs 10, 20, 30, 40, and 50. In each of the 5 photos, a different cow moves to the front of the line (at most one cow moves in each photo here, but it is possible in other inputs that multiple cows could move in a particular photo).

The correct original ordering A[1..5] is 10, 20, 30, 40, 50.

思路

我们可以发现,如果一头牛在三张照片中在另一头牛的前面,那么它在原生序列中就在另一头牛的前面,因为一头牛只有一次机会在一张照片中改变自己和另一头牛的相对位置,在下一张照片中就必须要回去

所以代码就很好写了,我们用map记录每头牛在每一张照片中的位置,然后给任意一张照片排序,比较的时候就按上文的方法就可以了:

代码

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<tr1/unordered_map>
using namespace std;

const int N=20010;

int n,x;
int a[N];

tr1::unordered_map<int,int>ma[6];

bool cmp(int x,int y) {
	int num=0;
	for(int i=1; i<=5; i++)
		if(ma[i][x]<ma[i][y])
			num++;
	return num>=3;
}

int main() {
	scanf("%d",&n);
	for(int i=1; i<=5; i++)
		for(int j=1; j<=n; j++) {
			scanf("%d",&x);
			a[j]=x;
			ma[i][x]=j;
		}
	sort(a+1,a+1+n,cmp);
	for(int i=1; i<=n; i++)
		printf("%d\n",a[i]);
	return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!