Young Table(暴力,交换位置)
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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
3 3 2 1 4 3 5 6 1 2
2 1 1 2 2 2 1 3 1
1 4 4 3 2 1
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 = ;
int c[MAXN];
int a[MAXN][MAXN];
struct Node{
int x, y;
void init(int x = , int y = ){
this->x = x;
this->y = y;
}
};
Node dt[MAXN * MAXN];
struct Nd{
int x, y, nx, ny;
void init(int x = , int y = , int nx = , int ny = ){
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, , sizeof(a));
memset(dt, , sizeof(dt));
for(int i = ; i < N; i++)
scanf("%d", c + i);
for(int i = ; i < N; i++){
for(int j = ; j < c[i]; j++){
scanf("%d", &a[i][j]);
dt[a[i][j]].init(i + , j + );
}
}
int cur = , cnt = ;
Node x;
for(int i = ; i < N; i++){
for(int j = ; j < c[i]; j++){
// printf("cur = %d a[i][j] = %d\n", cur, a[i][j]);
if(cur != a[i][j]){
x.init(i + , j + );
ans[cnt] = x + dt[cur];
a[dt[cur].x - ][dt[cur].y - ] = a[i][j];
dt[a[i][j]].init(dt[cur].x, dt[cur].y);
dt[cur].init(i + , j + );
cnt++;
}
cur++;
}
}
printf("%d\n", cnt);
for(int i = ; i < cnt; i++){
ans[i].print();
}
}
return ;
}
Young Table(暴力,交换位置)的更多相关文章
- 用C语言把双向链表中的两个结点交换位置,考虑各种边界问题。
用C语言把双向链表中的两个结点交换位置,考虑各种边界问题. [参考] http://blog.csdn.net/silangquan/article/details/18051675
- ListView实现Item上下拖动交换位置 并且实现下拉刷新 上拉加载更多
ListView实现Item上下拖动交换位置 并且实现下拉刷新 上拉加载更多 package com.example.ListViewDragItem; import android.app.Ac ...
- js 实现数组元素交换位置
/** * 数组元素交换位置 * @param {array} arr 数组 * @param {number} index1 添加项目的位置 * @param {number} index2 删除项 ...
- Android DynamicGrid:拖曳交换位置
Android DynamicGrid:拖曳交换位置 Android DynamicGrid是一个第三方开源项目,DynamicGrid在github上的项目主页是:https://github ...
- C语言 选择排序算法原理和实现 从数组中 找出最小的元素然后交换位置
#include <stdio.h> int main(void) { /* 选择排序算法 原理:从数组中 找出最小的元素然后交换位置: */ int a[10] = {9,5,10,7, ...
- C语言:对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中。-在数组中找出最小值,并与第一个元素交换位置。
//对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中. #include <stdio.h& ...
- Codeforces Round #308 (Div. 2) A. Vanya and Table 暴力
A. Vanya and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/552/pr ...
- Java&Selenium处理页面Table以及Table中随机位置的数据
一.摘要 前一段时间公司小伙伴刚刚接触自动化,遇到的一个问题,页面新创建的数据保存后,出现在table中的某个位置,并不一定是第一行还是第几行,这种情况下如何去操控它 本篇博文将介绍处理这个问题的一种 ...
- java例题_35 找到最大值和最小值并交换位置
1 /*35 [程序 35 最大最小交换] 2 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组. 3 */ 4 5 /*分析 6 * 1.先初始化一个数组,然后从键盘获得值 ...
随机推荐
- QString转换为char*
QString在Qt里相当于C++里的std::string,或者是C里的c style string.不过,QString跟编码相关,在低层想把一个QString发送出去相当麻烦,尤其对方用的不是Q ...
- python学习之路-4 内置函数和装饰器
本篇涉及内容 内置函数 装饰器 内置函数 callable() 判断对象是否可以被调用,返回一个布尔值 1 2 3 4 5 6 7 8 9 10 11 num = 10 print(callabl ...
- 关于c语言的一个小bug(c专家编程)
不多说,说了都是累赘!直接看代码吧! #include <stdio.h> int array[] = {23, 34, 12, 17, 204, 99, 16}; #define TOT ...
- 高性能WEB开发(6) - web性能測试工具推荐
WEB性能測试工具主要分为三种.一种是測试页面资源载入速度的,一种是測试页面载入完成后页面呈现.JS操作速度的,另一种是整体上对页面进行评价分析,以下分别对这些工具进行介绍,假设谁有更好的工具也请一起 ...
- Java学习——接口Interface
接口: 初期理解可以认为是一个特殊的抽象类 当抽象类中的方法都是抽象的,那么该类可以通过接口的形式来表示.class用于定义类interface 用于定义接口 接口定义时,格式特点:1,接口中常量见定 ...
- MyEclipse安装xfire插件
xfire因为过时,MyEclipse已经将其支,如果想用只能手动添加了. 安装步骤: 1.点击help┈┈┈→install from site出现下图 在第一栏里,输出http://dist.co ...
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...
- css3属性:column分栏
css3选择器中提出了分栏的属性,其浏览器支持情况为:Internet Explorer 10 和 Opera 支持 column 属性,Firefox 支持替代的 -moz-column 属性,Sa ...
- re模块
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用这一内嵌于 Python 的语言工具,尽管不能 ...
- POJ Find The Multiple 1426 (搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22576 Accepted: 929 ...