Codeforces 237B Young Table 【构造】

题目链接:Codeforces 237B Young Table

B. Young Table
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:

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.

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行,每行有c[i]个数,每次你可以交换任意两个不同的数。现在要求每行非降序排列、每列非降序排列。让你输出交换方案。

思路:全sort下,因为m限制为m <= s,那么我们从左到右、从上到下扫一遍就可以了。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <queue>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e6 + 1;
const int MOD = 1073741824;
const int INF = 0x3f3f3f3f;
void add(LL &x, LL y) { x += y; x %= MOD; }
int Map[60][60];
int a[4000];
int next[4000][4];
int c[60];
map<int, pii> fp;
int main()
{
    int n;
    while(scanf("%d", &n) != EOF) {
        for(int i = 1; i <= n; i++) {
            scanf("%d", &c[i]);
        }
        int top = 0; fp.clear();
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= c[i]; j++) {
                scanf("%d", &Map[i][j]);
                fp[Map[i][j]] = pii(i, j);
                a[top++] = Map[i][j];
            }
        }
        sort(a, a+top);
        int ans = 0; int id = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= c[i]; j++) {
                int x = fp[a[id]].fi;
                int y = fp[a[id]].se;
                if(a[id] != Map[i][j]) {
                    next[ans][0] = i;
                    next[ans][1] = j;
                    next[ans][2] = x;
                    next[ans++][3] = y;
                    fp[Map[i][j]] = pii(x, y);
                    swap(Map[i][j], Map[x][y]);
                }
                id++;
            }
        }
        printf("%d\n", ans);
        if(ans) {
            for(int i = 0; i < ans; i++) {
                printf("%d %d %d %d\n", next[i][0], next[i][1], next[i][2], next[i][3]);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值