"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 tabletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You ' ve got table a, consisting of n rows, and numbered from 1 to N. Thei-th line of table a contains ci cells, at this for alli (1?<? I? ≤? n) holdsci? ≤? Ci?-? 1.

Let's denote s as the total number of cells of tablea, which is,. We know that each cell of 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 tablea is numbered from 1 to Ci, then let's denote the number written in theJ-th Cell of the I-th row asa i,? J. Your task is to perform several swap operations to rearrange the numbers of the table so as to F Ulfill the following conditions:

    1. for all i ,? J (1?<? I ? ≤? n ;  1?≤? j ? ≤? C i ) holds a i ,? J ? a i ?-? 1,? J ;
    2. for all i ,? J I ? ≤? n ;  1?<? j ? ≤? C i ) holds a i ,? J ? a i ,? 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 thans. You don't have a to minimize the number of operations.

Input

The first line contains a single integer n (1?≤? n ? ≤?50) that is shows the number of the rows in the table. The second line Contains n space-separated integers C i (1?≤? C i ? ≤?50;  c i ? ≤? C i ?-? 1) -the numbers of cells on the corresponding Rows.

Next n lines contain table а. The i-th of them containsci space-separated integers:theJ-th Integer in This line represents ai,? J.

It is guaranteed so all the given numbers ai,? J is positive and does not exceeds. It's guaranteed that 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 nextmLines print the description of these swap operations. In theI-th line print four space-separated integers xi,? Yi,? Pi,? Qi (1?≤? ) Xi,? Pi? ≤?  n; 1?≤? y i? ≤? C xi; 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 ofdistinctTable cells. Print the swaps in the order, in which they should is executed.

Examplesinput
33 2 14 3 56 12
Output
21 1 2 22 1 3 1
Input
144 3 2 1
Output
21 1 1 41 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 of Val node{int X,y,val;} store[3000];//Sort by 1, 2, ... S 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 line//x[i],y[i] number of components table characterization value is the point where I should end up 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++) {//   Assign where each value should be located 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++;} The total number of elements, in fact, the direct CNT is good sum+=c[i];} Sort by value from small to large (store,store+sum,cmp);//start with a bit for each value 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 moveThe past 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 ();p rintf ("% D ", TMP); Tmp=q2.front (); Q2.pop ();p rintf ("%d ", TMP), Tmp=q3.front (); Q3.pop ();p rintf ("%d ", TMP); Tmp=q4.front (); Q4.pop ();p rintf ("%d\n", TMP);} return 0;}



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

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.