In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.
Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array:a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times.
If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
5 5 2 5 1 4
2 0 3 4 2
6 10 20 20 40 60 60
0
2 101 100
1 0 1
/*************************************************************************
> File Name: a.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: 2014年11月17日 星期一 23时21分57秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
const double esp = 1e-5;
#define N 3010
int num[N], ans1[N], ans2[N];
int main(int argc, char *argv[])
{
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i) {
scanf("%d", &num[i]);
}
int k = 0;
for (int i = 0; i < n - 1; ++i) {
int idx = i;
for (int j = i; j < n; ++j) {
if (num[idx] > num[j]) {
idx = j;
}
}
if (idx != i) {
int tmp = num[idx];
num[idx] = num[i];
num[i] = tmp;
ans1[k] = i;
ans2[k] = idx;
++k;
}
}
printf("%d\n", k);
for (int i = 0; i < k; ++i) {
printf("%d %d\n", ans1[i], ans2[i]);
}
}
return 0;
}
The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.
We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.
For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of boys. The second line contains sequence a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is the i-th boy's dancing skill.
Similarly, the third line contains an integer m (1 ≤ m ≤ 100) — the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 ≤ bj ≤ 100), where bj is the j-th girl's dancing skill.
Print a single number — the required maximum possible number of pairs.
4 1 4 6 2 5 5 1 5 7 9
3
4 1 2 3 4 4 10 11 12 13
0
5 1 1 1 1 1 3 1 2 3
2
/*************************************************************************
> File Name: b.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: 2014年11月17日 星期一 23时43分05秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
const double esp = 1e-5;
#define N 110
int dp[N][N], num1[N], num2[N], n, m;
int check(int i, int j)
{
if (abs(num1[i] - num2[j]) <= 1) {
return 1;
} else {
return 0;
}
}
int main(int argc, char *argv[])
{
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; ++i) {
scanf("%d", &num1[i]);
}
scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
scanf("%d", &num2[i]);
}
sort(num1 + 1, num1 + n + 1);
sort(num2 + 1, num2 + m + 1);
clr(dp, 0);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (check(i, j)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
printf("%d\n", dp[n][m]);
}
return 0;
}
You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.
The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.
In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1 -1" (without the quotes).
2 15
69 96
3 0
-1 -1
/*************************************************************************
> File Name: c.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: 2014年11月17日 星期一 23时55分39秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
const double esp = 1e-5;
#define N 110
int dp[N][15][9 * N], m, s, ans[N];
int main(int argc, char *argv[])
{
while (scanf("%d%d", &m, &s) != EOF) {
clr(dp, 0);
for (int i = 0; i < 10; ++i) {
dp[1][i][i] = 1;
}
for (int i = 2; i <= m; ++i) {
for (int j = 0; j < 10; ++j) {
for (int k = 0; k <= s; ++k) {
for (int l = 0; l < 10; ++l) {
//把j写成l了,真是坑
if (k - j >= 0 && dp[i - 1][l][k - j] == 1) {
dp[i][j][k] = 1;
}
}
}
}
}
int pos = -1;
for (int i = 1; i < 10; ++i) {
if (dp[m][i][s] == 1) {
pos = i;
break;
}
}
if (pos < 0) {
if (m == 1 && s == 0) {
printf("0 0\n");
} else {
printf("-1 -1\n");
}
} else {
int i = m;
int sum = s;
int cnt = 0;
while (i >= 1) {
ans[cnt++] = pos;
--i;
sum -= pos;
for (int j = 0; j < 10; ++j) {
if (dp[i][j][sum] == 1) {
pos = j;
break;
}
}
}
for (i = 0; i < cnt; ++i) {
printf("%d", ans[i]);
}
printf(" ");
i = m;
sum = s;
cnt = 0;
for (int i = 10; i >= 0; --i) {
if (dp[m][i][s] == 1) {
pos = i;
break;
}
}
while (i >= 1) {
ans[cnt++] = pos;
--i;
sum -= pos;
for (int j = 10; j >= 0; --j) {
if (dp[i][j][sum] == 1) {
pos = j;
break;
}
}
}
for (i = 0; i < cnt; ++i) {
printf("%d", ans[i]);
}
printf("\n");
}
}
return 0;
}