【打CF,学算法——二星级】CodeForces 237B Young Table (构造)

【CF简介】

提交链接:CF 237B

题面:

B. Young Table

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 the i-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:

  1. for all i,?j
    (1?<?i?≤?n; 1?≤?j?≤?ci) holds
    ai,?j?>?ai?-?1,?j;
  2. 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.

Examples

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行矩阵,每行分别有Ci个元素,其中Ci大于等于Ci+1,元素总个数为s,每个矩阵位置都有一个不同的值(从1到s),问能否给出一种方案,使得每行的元素从左到右递增,同一列的元素从上到下递增。

解题:

因为题目没限制最少需要几步,构造出一个合法解,只要求不超过s步,因为元素个数只有s个,我们只需要把元素按照1,2,3,...s的顺序排列,移到对应位置即可,最多只需要s-1步,故始终可以构造出一个合法解。

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
//node记录值为val的点的当前位置
struct node
{
	int x,y,val;
}store[3000];
//排序时按1,2,..s的方式排序
bool cmp(node a,node b)
{
	return a.val<b.val;
}
//arr存储当前各位置存储的是什么数值
//c数组记录每行多少个元素
//x[i],y[i]数组分表表征值为i的点最后应该处于的位置
int arr[55][55],c[55],x[3000],y[3000];
//队列记录操作方式
queue <int> q1,q2,q3,q4;
int main()
{
	//数据读入
	int n,cnt=0,sum=0,tmp;
	scanf("%d",&n);
    for(int i=1;i<=n;i++)
		scanf("%d",&c[i]);
	//数据处理
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=c[i];j++)
		{
		   //分配每个值应处的位置
		   x[cnt+1]=i;
		   y[cnt+1]=j;
           scanf("%d",&arr[i][j]);
		   //记录某值当前的位置
		   store[cnt].val=arr[i][j];
           store[cnt].x=i;
		   store[cnt].y=j;
		   cnt++;
		}
		//总元素个数,其实直接cnt就好
		sum+=c[i];
	}
	//按值从小到大排序
	sort(store,store+sum,cmp);
	//开始为每个值挪位
	for(int i=1;i<=sum;i++)
	{
		//如果该值已处于其应处的位置,则不操作
       if(store[i-1].x==x[i]&&store[i-1].y==y[i])
		   continue;
	   else
	   {
		   //否则开始交换
          q1.push(store[i-1].x);
		  q2.push(store[i-1].y);
		  //i要移过去的位置当前所占的值为v
		  int v=arr[x[i]][y[i]];
		  //该位置分配给i
		  arr[x[i]][y[i]]=i;
		  //i原位置分配给v
		  arr[store[i-1].x][store[i-1].y]=v;
		  store[v-1].x=store[i-1].x;
		  store[v-1].y=store[i-1].y;
		  //记录交换位置
		  q3.push(x[i]);
		  q4.push(y[i]);
	   }
	}
	//输出,此处输出过于复杂,其实一个队列即可,一次弹出4个元素
	printf("%d\n",q1.size());
	while(!q1.empty())
	{
		tmp=q1.front();
		q1.pop();
		printf("%d",tmp);
		tmp=q2.front();
		q2.pop();
		printf(" %d",tmp);
		tmp=q3.front();
		q3.pop();
		printf(" %d",tmp);
		tmp=q4.front();
		q4.pop();
		printf(" %d\n",tmp);
	}
	return 0;
}
时间: 2024-10-10 22:42:33

【打CF,学算法——二星级】CodeForces 237B Young Table (构造)的相关文章

【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)

[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/B 题面: B. Gerald is into Art time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Gerald bought two very rare paintings at the Sotheby's a

【打CF,学算法——二星级】Codeforces 705B Spider Man (简单博弈)

[CF简介] 题目链接:CF 705B 题面: B. Spider Man time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of v

【打CF,学算法——二星级】Codeforces 699B - One Bomb (技巧)

[CF简介] 题目链接:CF 699B 题面: B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a description of a depot. It is a rectangular checkered field of n?×?m size. Each cell in a

【打CF,学算法——二星级】Codeforces 703B Mishka and trip (统计)

[CF简介] 题目链接:CF 703B 题面: A - Mishka and trip Time Limit:1000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 703B Description Little Mishka is a great traveller and she visited many countries. After thinking

【打CF,学算法——二星级】CF 520B Two Buttons

[CF简单介绍] 提交链接:Two Buttons 题面: B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya has found a strange device. On the front panel of a device there are: a red button, a blu

Codeforces 1098B. Nice table 构造

原文链接https://www.cnblogs.com/zhouzhendong/p/CF1098B.html 题解 首先,我们来证明一个结论: 合法的矩阵要么满足每列只有两种字符,要么满足每行只有两种字符. 然后直接枚举就好了. 代码并不是那么好写. 代码 #include <bits/stdc++.h> using namespace std; typedef long long LL; LL read(){ LL x=0; char ch=getchar(); while (!isdig

1169: 零起点学算法76——绝对公正的裁判

1169: 零起点学算法76--绝对公正的裁判 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 510  Accepted: 336[Submit][Status][Web Board] Description 大家知道我们学校的OnlineJudge吗?,你知道他会告诉你什么呢? Compiling : 您提交的代码正在被编译.Running : 您的程序正在OJ上运行.Judging : OJ

1165: 零起点学算法72——首字母变大写

1165: 零起点学算法72--首字母变大写 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 705  Accepted: 439[Submit][Status][Web Board] Description 输入一个英文句子,将每个单词的第一个字母改成大写字母. Input 输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行. Output 请输出按照要求改写后的英文句

1127: 零起点学算法34——继续求多项式

1127: 零起点学算法34--继续求多项式 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 3481  Accepted: 1985[Submit][Status][Web Board] Description 输入1个正整数n, 计算1+(1+2)+(1+2+3)+...+(1+2+3+...+n) Input 输入正整数n(多组数据) Output 输出1+(1+2)+(1+2+3)+...+