本文主要是介绍Codeforces 384B. Multitasking(冒泡),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers.
Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j.
Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most (at most pairs). Help Iahub, find any suitable array.
Input
The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 ≤ x ≤ 106 holds.
Output
On the first line of the output print an integer p, the size of the array (p can be at most ). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices.
If there are multiple correct answers, you can print any.
Examples
inputCopy
2 5 0
1 3 2 5 4
1 4 3 2 5
outputCopy
3
2 4
2 3
4 5
inputCopy
3 2 1
1 2
2 3
3 4
outputCopy
1
2 1
Note
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
题意:
n个长度为m的序列。
每次选择i,j位置的数交换(对所有序列的i,j位置)。只有 a [ i ] > a [ j ] a[i]>a[j] a[i]>a[j]时才能进行交换。
求最多(m-1)*m/2次交换使得序列为递增或者递减。
思路:
注意到 a [ i ] > a [ j ] a[i]>a[j] a[i]>a[j]才能交换这个条件,所以不妨就将这n个序列当成一个,按照冒泡的方式将最前面的数依次往前(或者往后)移动。
在依次往前移动的时候,可以保证最大的数的数最多n-1次移到最后面,次大的数最多n-2次移到次后面。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>using namespace std;typedef long long ll;
const int maxn = 3e5 + 7;
const int mod = 1e9 + 7;int a[1005][105];int main() {int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i = 1;i <= n;i++) {for(int j = 1;j <= m;j++) {scanf("%d",&a[i][j]);}}printf("%d\n",m * (m - 1) / 2);for(int i = 1;i <= m;i++) {if(k == 0) {for(int j = 1;j <= m - i;j++) {printf("%d %d\n",j,j + 1);}} else {for(int j = m - 1;j >= i;j--) {printf("%d %d\n",j + 1,j);}}}return 0;
}
这篇关于Codeforces 384B. Multitasking(冒泡)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!