bnu A Matrix 北京邀请赛A题
A Matrix
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]);
}
Input
- 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.
Output
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>
#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题的更多相关文章
- BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...
- 2014 ACM/ICPC 北京邀请赛 部分 题解
题目链接:http://acm.bnu.edu.cn/bnuoj/problem.php?search=2014+ACM-ICPC+Beijing+Invitational+Programming+C ...
- hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String
hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just String http://www.bnuoj.co ...
- 2013长沙网络赛H题Hypersphere (蛋疼的题目 神似邀请赛A题)
Hypersphere Time Limit: 1 Second Memory Limit: 32768 KB In the world of k-dimension, there's a ...
- 2013 ACM/ICPC南京邀请赛B题(求割点扩展)
题目链接:http://icpc.njust.edu.cn/Contest/194/Problem/B B - TWO NODES 时间限制: 10000 MS 内存限制: 65535 KB 问题描述 ...
- XTU 1264 - Partial Sum - [2017湘潭邀请赛E题(江苏省赛)]
2017江苏省赛的E题,当时在场上看错了题目没做出来,现在补一下…… 题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id ...
- bnu 29378 Adidas vs Adivon 基础题
Adidas vs Adivon Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java ...
- 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 ...
- 2014 北京邀请赛ABDHJ题解
A. A Matrix 点击打开链接 构造,结论是从第一行開始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstrin ...
随机推荐
- JSP out乱码
在form method="post" 写post 在接受页面jsp代码前面加request.setCharacterEncoding("UTF-8");
- [reprint]malloc与calloc的区别
[http://blog.163.com/crazy20070501@126/] 转自某自由人的博客: malloc与calloc的区别 函数malloc()和calloc()都可以用来动态分配内存空 ...
- javascript 判断浏览器的ie版本,替换html标签
/* var browser=navigator.appName var b_version=navigator.appVersion var version=b_version.split(&quo ...
- run()和star()区别
run()和star()区别 run()-->只是thread类的一个普通方法调用 star()-->用来启动线程,实现多线程运行
- Android设计模式---观察者模式小demo(一)
1,今天刚好看到了设计模式这一块来,而观察者模式是我一直想总结的,先来看看观察者模式的简单的定义吧 "当一个对象改变时,他的所有依赖者都会受到通知,并自动更新." 一般我们项目中就 ...
- Deep learning的一些教程 (转载)
几个不错的深度学习教程,基本都有视频和演讲稿.附两篇综述文章和一副漫画.还有一些以后补充. Jeff Dean 2013 @ Stanford http://i.stanford.edu/infose ...
- 8. 星际争霸之php设计模式--享元模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- zw·准专利·高保真二值图细部切分算法
zw·准专利·高保真二值图细部切分算法 高保真二值图细部切分算法,是中国字体协会项目的衍生作品. 说准专利算法,是因为对于图像算法的标准不了解,虽然报过专利,但不是这方面的,需要咨询专 ...
- 安装Debian的正确方法
一.说明: Debian7.0.0的安装镜像文件有3个DVD,安装基本系统只用到第一个镜像文件,即DVD1 其它镜像文件是附带的软件包. 附Debian 7.0.0系统镜像下载地址: 32位:http ...
- Spring MVC 和 Spring 总结
1. 为什么使用Spring ? 1). 方便解耦,简化开发 通过Spring提供的IoC容器,可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合. 2). AOP编程的 ...