Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n.
Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50).
The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
2 0 1 1 0
2 1
5 0 2 2 1 2 2 0 4 1 3 2 4 0 1 3 1 1 1 0 1 2 3 3 1 0
2 5 4 1 3
In the first case, the answer can be {1, 2} or {2, 1}.
In the second case, another possible answer is {2, 4, 5, 1, 3}.
题意:给了一个数组p,其中有1--n这n个数,然后是一个矩阵a[i][j]=min(p[i],p[j]),i==j的时候,矩阵中的元素是0,现在给出了这个矩阵,求这个数列
解题思路:其实是一道规律题,矩阵中第 i 行出现次数最多的就是p [ i ],如果每个数出现的次数都是相等的,那么这个数是n或者n-1,答案是不固定的,找到这n-1跟n出现得两个位置,然后任意输出就行。
证明:就拿1--5中的3来说,比3小的数有1,2,比3大的数有4,5,那么这一行中肯定就是0,1,2,3,3,的随机排列,1,2肯定也是这样的情况,但是4,5就不一样了,比4大的只有5,所以4是0,1,2,3,4,同样5也是这样,所以我们不能确定4,5的位置,答案就不固定了,
唉,头一次没看到数据范围,直接next_permutation了,果断超时,后来想想,,真水啊,,,还好是A了,
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int num[60],map[60][60],vis[1010];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
for(int i=1;i<=n;i++)
{
num[i]=i;
memset(vis,0,sizeof(vis));
for(int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
vis[map[i][j]]++;
}
int a=0,k=0;
for(int j=1;j<=n;j++)
{
if(vis[j]-1>a)
{
a=j;
}
}
num[i]=a;
}
int f1=0,f2=0;
for(int i=1;i<=n;i++)
if(num[i]==0)
{
if(f1==0) num[i]=n-1,f1=i;
else f2=i,num[i]=n;
}
for(int i=1;i<n;i++)
{
printf("%d ",num[i]);
}
printf("%d\n",num[n]);
}
return 0;
}