Codeforces Round #147 (Div. 2) / 237B Young Table (搜索)
2013-08-26 09:16
344 查看
B. Young Table
http://codeforces.com/problemset/problem/237%2FB
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) holdsci ≤ 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 test(s)
input
output
input
output
注意到这么一句话:You do not have to minimize the number of operations.(你不必须使你的交换操作次数最小。)
所以对每个位置都从1~s编个号,只要数字不对应就交换到目标位置,详见代码。
完整代码:
http://codeforces.com/problemset/problem/237%2FB
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) holdsci ≤ 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 test(s)
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
注意到这么一句话:You do not have to minimize the number of operations.(你不必须使你的交换操作次数最小。)
所以对每个位置都从1~s编个号,只要数字不对应就交换到目标位置,详见代码。
完整代码:
/*30ms,132KB*/ #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; #define X first #define Y second ///上面的技巧很好~ vector<pair<pair<int, int>, pair<int, int> > > ans; pair<int, int> num[2510];///这个数所在的坐标 int a[105][105], n, c[105]; int main(void) { 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++) { scanf("%d", &a[i][j]); num[a[i][j]].X = i , num[a[i][j]].Y = j; } //////////////////////// int cnt = 0; ///每个数只要不在位子上就交换到目标位置 for (int i = 1; i <= n; i++) for (int j = 1; j <= c[i]; j++) { cnt++; if (a[i][j] == cnt) continue; ans.push_back(make_pair(make_pair(i, j), num[cnt])); int key = a[i][j]; swap(a[i][j] , a[num[cnt].X][num[cnt].Y]); swap(num[key] , num[cnt]); } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); i++) printf("%d %d %d %d\n" , ans[i].X.X , ans[i].X.Y , ans[i].Y.X , ans[i].Y.Y); return 0; }
相关文章推荐
- codeforces round 309 div1 Nudist Beach 二分+搜索
- Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
- Croc Champ 2013 - Round 2 (Div. 2 Edition) 贪心+ 搜索+剪枝 + 数学
- codforces Codeforces Round #409 div2 C 二分搜索
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
- 牛腩购物网31:产品搜索的制作,投票的制作,投票的div的长度
- Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索
- cf202-div 1-B - Apple Tree:搜索,数论,树的遍历
- Codeforces Round #426 (Div. 1) C. Ever-Hungry Krakozyabra(搜索)
- topcoder-SRM565-div2-第二题-500分--搜索/动态规划
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry dp 搜索
- Codeforces Round #295 (Div. 2)---B. Two Buttons( bfs步数搜索记忆 )
- Codeforces Round #197 (Div. 2) C. Xenia and Weights - 搜索
- Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)
- Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)
- C. Ice Cave (CF #301 (Div. 2) 搜索bfs)
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 前缀最小值+贪心+二分搜索
- Codeforces Round #313 (Div. 2) D. Equivalent Strings(简单搜索)
- Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索
- Codeforces Round #175 (Div. 2) D. Permutation Sum(暴力搜索)