Young Table(暴力,交换位置)
2016-04-27 20:03
288 查看
Young Table
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 237B
Appoint description:
System Crawler (2016-04-26)
Description
You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i(1 < i ≤ n)holds ci ≤ ci - 1.
Let's denote s as the total number of cells of table a, that is,
. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct.
Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of thei-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions:
for all i, j(1 < i ≤ n; 1 ≤ j ≤ ci) holds ai, j > ai - 1, j;
for all i, j(1 ≤ i ≤ n; 1 < j ≤ ci) holds ai, j > ai, j - 1.
In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap.
Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations.
Input
The first line contains a single integer n(1 ≤ n ≤ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci(1 ≤ ci ≤ 50; ci ≤ ci - 1) — the numbers of cells on the corresponding rows.
Next n lines contain table а. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j.
It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct.
Output
In the first line print a single integer m(0 ≤ m ≤ s), representing the number of performed swaps.
In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi(1 ≤ xi, pi ≤ n; 1 ≤ yi ≤ cxi; 1 ≤ qi ≤ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed.
Sample Input
Input
Output
Input
Output
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 237B
Appoint description:
System Crawler (2016-04-26)
Description
You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i(1 < i ≤ n)holds ci ≤ ci - 1.
Let's denote s as the total number of cells of table a, that is,
. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct.
Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of thei-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions:
for all i, j(1 < i ≤ n; 1 ≤ j ≤ ci) holds ai, j > ai - 1, j;
for all i, j(1 ≤ i ≤ n; 1 < j ≤ ci) holds ai, j > ai, j - 1.
In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap.
Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations.
Input
The first line contains a single integer n(1 ≤ n ≤ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci(1 ≤ ci ≤ 50; ci ≤ ci - 1) — the numbers of cells on the corresponding rows.
Next n lines contain table а. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j.
It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct.
Output
In the first line print a single integer m(0 ≤ m ≤ s), representing the number of performed swaps.
In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi(1 ≤ xi, pi ≤ n; 1 ≤ yi ≤ cxi; 1 ≤ qi ≤ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed.
Sample Input
Input
3 3 2 1 4 3 5 6 1 2
Output
2 1 1 2 2 2 1 3 1
Input
1 4 4 3 2 1
Output
2 1 1 1 4 1 2 1 3 题解:给N个数,接下来N个数,每个数代表i行的元素个数,要保证数列从左到右递增,从上到下递增;输出一种方案。交换次数,接下来每行输出交换的位置; 刚开始看错了,还以为最少交换次数,没敢写。。。 代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; const int MAXN = 110; int c[MAXN]; int a[MAXN][MAXN]; struct Node{ int x, y; void init(int x = 0, int y = 0){ this->x = x; this->y = y; } }; Node dt[MAXN * MAXN]; struct Nd{ int x, y, nx, ny; void init(int x = 0, int y = 0, int nx = 0, int ny = 0){ this->x = x; this->y = y; this->nx = nx; this->ny = ny; } void print(){ printf("%d %d %d %d\n", x, y, nx, ny); } }; Nd ans[MAXN * MAXN]; Nd operator + (Node a, Node b){ Nd c; c.x = a.x; c.y = a.y; c.nx = b.x; c.ny = b.y; return c; } int main(){ int N; while(~scanf("%d", &N)){ memset(a, 0, sizeof(a)); memset(dt, 0, sizeof(dt)); for(int i = 0; i < N; i++) scanf("%d", c + i); for(int i = 0; i < N; i++){ for(int j = 0; j < c[i]; j++){ scanf("%d", &a[i][j]); dt[a[i][j]].init(i + 1, j + 1); } } int cur = 1, cnt = 0; Node x; for(int i = 0; i < N; i++){ for(int j = 0; j < c[i]; j++){ // printf("cur = %d a[i][j] = %d\n", cur, a[i][j]); if(cur != a[i][j]){ x.init(i + 1, j + 1); ans[cnt] = x + dt[cur]; a[dt[cur].x - 1][dt[cur].y - 1] = a[i][j]; dt[a[i][j]].init(dt[cur].x, dt[cur].y); dt[cur].init(i + 1, j + 1); cnt++; } cur++; } } printf("%d\n", cnt); for(int i = 0; i < cnt; i++){ ans[i].print(); } } return 0; }
相关文章推荐
- 利用caffe训练的模型,写一个测试程序
- 操作系统笔记---内存管理
- iOS url的一些处理
- 第九周上机实践项目——项目1-深复制体验
- 1009. 说反话
- JAVA技术发展——你不知道的J2SE(一)
- OpenGL ES3.0 的初探
- 人生小结2
- C#语言中的小知识点
- Android Handler机制
- 说说javap命令
- hdu 2215 & hdu 3932(最小覆盖圆)
- uva10870 矩阵
- Swing开发之JComboBox篇
- 根据两点经纬度计算距离
- Android---通过http访问服务器
- andorid---通过Viewpager实现图片滑动以及缩放
- 《啊哈算法》第四章 万能的搜索
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
- android---高德地图(1)---显示一张简单地图