码迷,mamicode.com
首页 > 其他好文 > 详细

bnu A Matrix 北京邀请赛A题

时间:2014-10-17 23:14:08      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   for   

A Matrix

2000ms
65536KB
 
64-bit integer IO format: %lld      Java class name: Main
Font Size:  
Chaos King loves permutation and matrix, now he is trying to find out some relations between them.
Now Chaos King has a permutation. He wants to transform the permutation to a matrix by the program beneath.
int f[][];
int p[];
int n;
void insert(int r, int x)
{
    for (int i=1; 1; i++)
        if (f[r][i]==0)
        {
            f[r][i]=x;
            return ;
        }
        else if (f[r][i]>x)
        {
            int tmp=f[r][i];
            f[r][i]=x;
            insert(r+1, tmp);
            return ;
        }
}
void Permutation2Matrix()
{
    for (int i=1; i<=n; i++)
        insert(1, p[i]);
}
initially, all elements in f are 0.
 
Now Chaos King has got a matrix from the program above, then he sends this matrix to you and asks you what is the initial permutation. Can you do it?

Input

First line of the input is an integer T indicating the number of cases,
 
For each test case,
  • First line contains 2 integers N (1 ≤ N ≤ 105), M (1 ≤ M ≤ N) indicating length of the permutation and number of rows which do not contains 0 in the matrix.
  • Then M lines, the ith line represent the ith row in the matrix.
    • The first integer in each line is Pi, indicating the number of non-zero elements in this row. Then Pi integers, indicating those elements.
 
It is guaranteed that ∑Pi=N and every element in the matrix is an integer in [1, N]. There are no 2 elements have the same value.
 

Output

For each case, first output the case number as "Case #x: ", and x is the case number.
Then output a permutation of N, indicating the answer of this test case.
 
If there are multiple answers, output the permutation with the largest alphabet order after reversal. (e.g. there are 2 answers: 1 2 3 and 1 3 2. After reversal, 1 2 3 becomes 3 2 1, and 1 3 2 becomes 2 3 1. Because 3 2 1 is larger than 2 3 1 in alphabet order, you should output 1 2 3)
 
If there is no answer, output "No solution".
 

Sample Input

2
3 2
2 1 2
1 3
2 1
2 2 1
 

Sample Output

Case #1: 1 3 2
Case #2: No solution
 

Source

 
 
题意:略了。
思路:略了。
贴个代码
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<vector>
 6 using namespace std;
 7 
 8 vector<int>Q[100005];
 9 vector<int>Hash[100005];
10 int hxl[100005],len;
11 
12 
13 void dfs(int x,int y,int dep)
14 {
15     if(x>dep)return;
16     if(Hash[x][y]==-1)
17     {
18         Hash[x][y]=-2;
19         hxl[++len]=Q[x][y];
20         return;
21     }
22     dfs(x+1,Hash[x][y],dep);
23     hxl[++len]=Q[x][y];
24     Hash[x][y]=-2;
25 }
26 void solve(int n,int m)
27 {
28     for(int i=m;i>=2;i--)
29     {
30         int k=Hash[i][0];
31         int s=Hash[i-1][0];
32         for(int j=k;j>=1;j--)
33         {
34             for(;s>=1;s--)
35             if(Q[i][j]>Q[i-1][s]){
36                 Hash[i-1][s]=j;
37                 s--;
38                 break;
39             }
40         }
41     }
42     for(int i=1;i<m;i++)
43     {
44         int num=0;
45         for(int j=1;j<=Hash[i][0];j++)
46             if(Hash[i][j]!=-1)num++;
47         if(num!=Hash[i+1][0]){
48             printf(" No solution\n");
49             return;
50         }
51     }
52     len = 0;
53     for(int i=1;i<=m;i++)
54     {
55         for(int j=1;j<=Q[i][0];j++)
56         {
57             if(Hash[i][j]==-1)hxl[++len]=Q[i][j];
58             else if(Hash[i][j]==-2)continue;
59             else dfs(i,j,m);
60         }
61     }
62     for(int i=1;i<=len;i++)
63     printf(" %d",hxl[i]);
64     printf("\n");
65 }
66 int main()
67 {
68     int T,n,m,x,y;
69     scanf("%d",&T);
70     for(int t=1;t<=T;t++)
71     {
72         scanf("%d%d",&n,&m);
73         for(int i=1;i<=n;i++){
74             Q[i].clear();
75             Hash[i].clear();
76         }
77         bool flag=false;
78         for(int i=1;i<=m;i++)
79         {
80             scanf("%d",&x);
81             Q[i].push_back(x);
82             Hash[i].push_back(x);
83             for(int j=1;j<=x;j++)
84             {
85                 scanf("%d",&y);
86                 Q[i].push_back(y);
87                 Hash[i].push_back(-1);
88                 if(j>1&&Q[i][j]<=Q[i][j-1]) flag=true;
89             }
90             if(i>1&&Q[i][0]>Q[i-1][0])flag=true;
91         }
92         printf("Case #%d:",t);
93         if(flag==true)printf(" No solution\n");
94         else{
95             solve(n,m);
96         }
97     }
98     return 0;
99 }

 

bnu A Matrix 北京邀请赛A题

标签:style   blog   http   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/tom987690183/p/4032215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!