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.
  1. int f[][];
  2. int p[];
  3. int n;
  4. void insert(int r, int x)
  5. {
  6. for (int i=1; 1; i++)
  7. if (f[r][i]==0)
  8. {
  9. f[r][i]=x;
  10. return ;
  11. }
  12. else if (f[r][i]>x)
  13. {
  14. int tmp=f[r][i];
  15. f[r][i]=x;
  16. insert(r+1, tmp);
  17. return ;
  18. }
  19. }
  20. void Permutation2Matrix()
  21. {
  22. for (int i=1; i<=n; i++)
  23. insert(1, p[i]);
  24. }
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

  1. 2
  2. 3 2
  3. 2 1 2
  4. 1 3
  5. 2 1
  6. 2 2 1
 

Sample Output

  1. Case #1: 1 3 2
  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在这里发生问题了。
                     所以还需要判断上一行的数字把下一行挤下去的个数是否与下一行的个数相等。
 
代码:
  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[];
  9. vector<int>Hash[];
  10. int hxl[],len;
  11.  
  12. void dfs(int x,int y,int dep)
  13. {
  14. if(x>dep)return;
  15. if(Hash[x][y]==-)
  16. {
  17. Hash[x][y]=-;
  18. hxl[++len]=Q[x][y];
  19. return;
  20. }
  21. dfs(x+,Hash[x][y],dep);
  22. hxl[++len]=Q[x][y];
  23. Hash[x][y]=-;
  24. }
  25. void solve(int n,int m)
  26. {
  27. for(int i=m;i>=;i--)
  28. {
  29. int k=Hash[i][];
  30. int s=Hash[i-][];
  31. for(int j=k;j>=;j--)
  32. {
  33. for(;s>=;s--)
  34. if(Q[i][j]>Q[i-][s]){
  35. Hash[i-][s]=j;
  36. s--;
  37. break;
  38. }
  39. }
  40. }
  41. for(int i=;i<m;i++)
  42. {
  43. int num=;
  44. for(int j=;j<=Hash[i][];j++)
  45. if(Hash[i][j]!=-)num++;
  46. if(num!=Hash[i+][]){
  47. printf(" No solution\n");
  48. return;
  49. }
  50. }
  51. len = ;
  52. for(int i=;i<=m;i++)
  53. {
  54. for(int j=;j<=Q[i][];j++)
  55. {
  56. if(Hash[i][j]==-)hxl[++len]=Q[i][j];
  57. else if(Hash[i][j]==-)continue;
  58. else dfs(i,j,m);
  59. }
  60. }
  61. for(int i=;i<=len;i++)
  62. printf(" %d",hxl[i]);
  63. printf("\n");
  64. }
  65. int main()
  66. {
  67. int T,n,m,x,y;
  68. scanf("%d",&T);
  69. for(int t=;t<=T;t++)
  70. {
  71. scanf("%d%d",&n,&m);
  72. for(int i=;i<=n;i++){
  73. Q[i].clear();
  74. Hash[i].clear();
  75. }
  76. bool flag=false;
  77. for(int i=;i<=m;i++)
  78. {
  79. scanf("%d",&x);
  80. Q[i].push_back(x);
  81. Hash[i].push_back(x);
  82. for(int j=;j<=x;j++)
  83. {
  84. scanf("%d",&y);
  85. Q[i].push_back(y);
  86. Hash[i].push_back(-);
  87. if(j>&&Q[i][j]<=Q[i][j-]) flag=true;
  88. }
  89. if(i>&&Q[i][]>Q[i-][])flag=true;
  90. }
  91. printf("Case #%d:",t);
  92. if(flag==true)printf(" No solution\n");
  93. else{
  94. solve(n,m);
  95. }
  96. }
  97. return ;
  98. }

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. 转:windows 7系统安装与配置Tomcat服务器环境

    工具/原料 jdk-7u45-windows-x64(我的系统是64位系统,32位的请选x86下载) apache-tomcat-8.0.0-RC5-windows-x64 方法/步骤   下载说明, ...

  2. SQL 存储和触发器

    存储过程:就像函数一样的会保存在数据库中-->可编程性 --> 存储过程 创建存储过程:create proc JiaFa--需要的参数@a int,@b intas --存储过程的内容 ...

  3. 面向切面编程AOP:基于注解的配置

    Aop编程就是面向编程的羝是切面,而切面是模块化横切关注点. -切面:横切关注点,被模块化的特殊对象. -通知:切面必须要完成的工作 -目标:被通知的对象 -代理:向目标对象应用通知之后创建的对象. ...

  4. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  5. sql字符转换函数大全

    删除空格 有两个函数,TTRIM()和LTRIM(),可以用来从字符串中剪掉空格.函数LTRIM()去除应该字符串前面的所有空格:函数RTRIM()去除一个字符串尾部的所有空格.这些和vbscript ...

  6. II7下配置SSAS通过HTTP 远程链接访问

    IIS7下配置SSAS通过HTTP远程连接 安装环境操作系统:Windows7.Windows Server2008IIS版本:7.5 IIS7下配置SSAS通过HTTP远程连接详细的步骤如下:1.首 ...

  7. C#访问postgresql数据库

    最近开始做C#的DotNet的工作,因为对PostgreSQL数据库比较有兴趣,所以自己研究了一下如何访问PostgreSQL的 数据库的问题. 1.为了访问PostgreSQL数据库,需要从pgfo ...

  8. 夺命雷公狗---node.js---10之POST的接收

    首先我们在项目下创建一个表单,代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. html规范整体结构

    <!DOCTYPE html><html lang="zh"><head> <meta charset="utf-8" ...

  10. Date() 及其 如何验证用户输入的日期是合法的

    1.var someDate = new Date(Date.parse("May 25, 2004"));   <=>  var someDate = new Dat ...