A Matrix

Time Limit: 2000ms
Memory Limit: 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.同一行的数字必须是从小到大的。同一列也是。
               2.第i行的个数不能小于第i+1行,要满足>=.
                                     如   1 
                                           2 3 这样的情况是不能存在的,因为,因为2要被挤下来,上面必须有一个数字把它挤下来。3同理。
             3.如果按题意来求,把所以的满足条件来求,先找出所以的可能,然后反转排序,求最满足的。10^5个,有多少可能??
              这样的方法必须要避免。
       那我们要找到一个好的切入点来做。找出一个合适的序列顺利来求。
       例子  1 2 3 4 5 6
               7                 可能的顺是 7  1 2 3 4 5 6   也可能 1 2 3 4 5 7 6 发现后者是最满足要求的,反转后最大。
                                  是说7出现在6前面,换一句话说,就是7要被6挤下来,而不是1 2 3 4 5当中的一个。
            所以,我们可以从最后一行,最后一列开始枚举,分别找到上一行比它小,但最大的那个数字;
            (认为就是被该数字挤下来的。)
       例子   1 2 3 4
                5 6 7
                8 9            变成
 
             我们发现1是没有和谁连接的,没有关系,我们想要使反转最大,那么越小的数字出现在前面,越好,贪心的思想。
          那么原来的序列是  1 5 2 8 6 3 9 7 4 ,发现是可以的。就这样。
      但是也有一个例子是无解的。
              1 4
              2 3  因为3 可以连1  但是2不能连4,   4不能把2挤下去。所以2在这里发生问题了。
                     所以还需要判断上一行的数字把下一行挤下去的个数是否与下一行的个数相等。
 
代码:
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std; vector<int>Q[];
vector<int>Hash[];
int hxl[],len; void dfs(int x,int y,int dep)
{
if(x>dep)return;
if(Hash[x][y]==-)
{
Hash[x][y]=-;
hxl[++len]=Q[x][y];
return;
}
dfs(x+,Hash[x][y],dep);
hxl[++len]=Q[x][y];
Hash[x][y]=-;
}
void solve(int n,int m)
{
for(int i=m;i>=;i--)
{
int k=Hash[i][];
int s=Hash[i-][];
for(int j=k;j>=;j--)
{
for(;s>=;s--)
if(Q[i][j]>Q[i-][s]){
Hash[i-][s]=j;
s--;
break;
}
}
}
for(int i=;i<m;i++)
{
int num=;
for(int j=;j<=Hash[i][];j++)
if(Hash[i][j]!=-)num++;
if(num!=Hash[i+][]){
printf(" No solution\n");
return;
}
}
len = ;
for(int i=;i<=m;i++)
{
for(int j=;j<=Q[i][];j++)
{
if(Hash[i][j]==-)hxl[++len]=Q[i][j];
else if(Hash[i][j]==-)continue;
else dfs(i,j,m);
}
}
for(int i=;i<=len;i++)
printf(" %d",hxl[i]);
printf("\n");
}
int main()
{
int T,n,m,x,y;
scanf("%d",&T);
for(int t=;t<=T;t++)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
Q[i].clear();
Hash[i].clear();
}
bool flag=false;
for(int i=;i<=m;i++)
{
scanf("%d",&x);
Q[i].push_back(x);
Hash[i].push_back(x);
for(int j=;j<=x;j++)
{
scanf("%d",&y);
Q[i].push_back(y);
Hash[i].push_back(-);
if(j>&&Q[i][j]<=Q[i][j-]) flag=true;
}
if(i>&&Q[i][]>Q[i-][])flag=true;
}
printf("Case #%d:",t);
if(flag==true)printf(" No solution\n");
else{
solve(n,m);
}
}
return ;
}

bnu A Matrix 北京邀请赛A题的更多相关文章

  1. BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...

  2. 2014 ACM/ICPC 北京邀请赛 部分 题解

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem.php?search=2014+ACM-ICPC+Beijing+Invitational+Programming+C ...

  3. hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String

    hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just  String http://www.bnuoj.co ...

  4. 2013长沙网络赛H题Hypersphere (蛋疼的题目 神似邀请赛A题)

    Hypersphere Time Limit: 1 Second       Memory Limit: 32768 KB In the world of k-dimension, there's a ...

  5. 2013 ACM/ICPC南京邀请赛B题(求割点扩展)

    题目链接:http://icpc.njust.edu.cn/Contest/194/Problem/B B - TWO NODES 时间限制: 10000 MS 内存限制: 65535 KB 问题描述 ...

  6. XTU 1264 - Partial Sum - [2017湘潭邀请赛E题(江苏省赛)]

    2017江苏省赛的E题,当时在场上看错了题目没做出来,现在补一下…… 题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id ...

  7. bnu 29378 Adidas vs Adivon 基础题

    Adidas vs Adivon Time Limit: 1000ms Memory Limit: 65536KB   64-bit integer IO format: %lld      Java ...

  8. 2013 南京邀请赛 K题 yet another end of the world

    /** 大意:给定一组x[],y[],z[] 确定有没有两个不同的x[i], x[j] 看是否存在一个ID使得 y[i]<=ID%x[i]<=z[i] y[j]<=ID%x[j]&l ...

  9. 2014 北京邀请赛ABDHJ题解

    A. A Matrix 点击打开链接 构造,结论是从第一行開始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstrin ...

随机推荐

  1. hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...

  2. [转]gitHub客户端Desktop的安装使用总结 ---基础篇

    gitHub客户端Desktop的安装使用总结 ---基础篇 发表于2015/12/11 11:41:57  8399人阅读 分类: Android之应用实战 这段时间想把我写的东西上传到github ...

  3. paper 45:latex的使用

    本教程面向对LaTeX完全无认知无基础的新人. 旨在让新人能够用最简单快捷的方式,轻松入门,能够迅速使用LaTeX完成基本的文本编辑. 尤其旨在破除部分新人对LaTeX在传闻中难以学习的恐惧感. 在入 ...

  4. LDA-math-LDA 文本建模

    http://cos.name/2013/03/lda-math-lda-text-modeling/ 5. LDA 文本建模 5.1 游戏规则 对于上述的 PLSA 模型,贝叶斯学派显然是有意见的, ...

  5. Cloudera CDH 、Impala本地通过Parcel安装配置详解

    一.Parcel本地源与Package本地源的区别 本地通过Parcel安装过程与本地通过Package安装过程完全一致,不同的是两者的本地源的配置. 区别如下: Package本地源:软件包是.rp ...

  6. 白盒测试的学习之路----(五)TestNG的参数分离

    之前的测试用例直接嵌套在代码中,不便于维护和测试设计,应该单独把测试用例放在excel内,然后程序从中读取数据到相应的接口内即可.使用ava程序对Microsoft Office格式档案读和写的功能提 ...

  7. C#下调用C++ SDK的编码常识

    一组编码规范,通过C#调用C++ 自动封装的C# SDK,会发现面向对象思想的重要性. C++ SDK可以使用自动封装工具转换成C# SDK.但需要遵守如下规则: 1.如果需要对C#对象进行判断,则分 ...

  8. shell 在文件名后面添加特定数据

    for a in `ls mo-*`;do mv ${a%:} ${a%:}-1;done

  9. 161118、linux(centos) 项目部署阶段相关命令汇总

    1.ssh免密码登陆 主要命令 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys-->添加公钥 service sshd restart ...

  10. Java中删除指定文件夹文件夹下面有内容也删除使用递归方案

    import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java ...