"Play CF, learn algorithm--Two star" Codeforces 237B Young Table (construction)

Source: Internet
Author: User
Tags value store

"About CF"

Submit Link: CF 237B


Surface:

B. Young Table time limit per test 2 seconds memory limit per test megabytes input standard input output standard OUTP Ut

You ' ve got table A, consisting of n rows, and numbered from 1 to N. The I-th Line of table A contains CI cells, at, and all I (1 < i≤n) holds ci≤ci-1.

Let's denote s as the total number of cells of table A, which is,. We know that each cell of the the table contains a single integer from 1 to S, at this all written integers is distinct.

Let's assume that the cells of the i-th row of table A is numbered from 1 to CI and 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 of the table so as to fulfill the following Condi Tions: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 is allowed to choose and different cells of the table and swaps 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 I T. Similarly, the number that's recorded in the second of the selected cells, are written in the first cell after the SWA P.

Rearrange the numbers in the required manner. Note that is allowed to perform any number of operations, and not more than S. You don't have a 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 correspond ing 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 the given numbers AI, J is positive and does not exceed S. It is guaranteed and all AI, J is 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. 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 is 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









----------------------------------------------------------------------------------------------I'm a split line---------------------- -------------------------------------------------------------------------------------------


Test instructions

Given an irregular n-row matrix, each line has CI elements, where CI is greater than or equal to ci+1, the total number of elements is s, each matrix position has a different value (from 1 to s), ask whether a scheme, so that each row of elements from left to right increment, the same column of elements from top to bottom increment.


Solving:

Because the topic does not limit at least a few steps, constructs a legal solution, only requires not more than S step, because the number of elements only s, we just need to put the elements in accordance with the order of 1,2,3,...S, moved to the corresponding position, at most only need s-1 step, it can always construct a legal solution.


Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <queue> using namespace std; The node record value is the current position of the point Val of the struct node {int x,y,val;}
STORE[3000]; Sort by 1, 2, ... s the way to sort bool CMP (node A,node b) {return a.val<b.val;}//arr stores what values are stored at the current location//c array records how many elements per row//x[i],y[i] Number of component tables the point at which the value is I should be at last
to the position of int arr[55][55],c[55],x[3000],y[3000];
Queue record operation mode queue <int> q1,q2,q3,q4;
	int main () {//data read into int n,cnt=0,sum=0,tmp;
    scanf ("%d", &n);
	for (int i=1;i<=n;i++) scanf ("%d", &c[i]);
		   Data processing for (int i=1;i<=n;i++) {for (int j=1;j<=c[i];j++) {//assigns each value where the position should be x[cnt+1]=i;
           Y[cnt+1]=j;
		   scanf ("%d", &arr[i][j]);
           Record the current position of a value store[cnt].val=arr[i][j];
		   Store[cnt].x=i;
		   Store[cnt].y=j;
		cnt++;
	}//Total element number, in fact, the direct CNT is good sum+=c[i];
	}//sorted by value from small to large sort (store,store+sum,cmp); 
		Start for each value move for (int i=1;i<=sum;i++) {//If the value is already in its place, do not operate if (Store[i-1].x==x[i]&&store[i-1].y==y[i])   Continue
		  else {//otherwise start swapping q1.push (store[i-1].x);
		  Q2.push (STORE[I-1].Y);
		  I want to move past the position currently occupies a value of V int v=arr[x[i]][y[i]];
		  The position is assigned to I arr[x[i]][y[i]]=i;
		  The original position of I is assigned to 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;
		  Record Exchange position Q3.push (X[i]);	  
	   Q4.push (Y[i]);
	}}//output, where the output is too complex, in fact a queue can be, a popup 4 elements 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; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.