Codeforces Round #277.5 (Div. 2) 解题报告

Codeforces Round #277.5 (Div. 2) 解题报告
A. SwapSort
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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 ij (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.

Sample test(s)
input
5
5 2 5 1 4
output
2
0 3
4 2
input
6
10 20 20 40 60 60
output
0
input
2
101 100
output
1
0 1
通过交换将一个序列变成有序的,因为不需要最小化,所以直接使用选择排序的算法,每次交换记录交换的下标,最多需要交换n-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;
}

B. BerSU Ball
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

Print a single number — the required maximum possible number of pairs.

Sample test(s)
input
4
1 4 6 2
5
5 1 5 7 9
output
3
input
4
1 2 3 4
4
10 11 12 13
output
0
input
5
1 1 1 1 1
3
1 2 3
output
2
这个题目可以有好多中方法做,我用的是LCS,容易理解,记得要先排序.我写排序的时候,范围还写错了.好坑.不过使用贪心也可以,复杂度更低.
代码如下:
/*************************************************************************
	> 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;
}

C. Given Length and Sum of Digits...
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The single line of the input contains a pair of integers ms (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.

Output

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).

Sample test(s)
input
2 15
output
69 96
input
3 0
output
-1 -1
求m位数的所有位上数字和为s的最大值和最小值,不能有前导0.这道题目用贪心都可以做的,对于最小值最高位填1,后边填0,然后在最低位填上若干9,某一位上填某个数.最大数,最高几位填9,然后某个数,最低几位填0.这样最简单.
我想复杂了,用dp做的,居然还过了,不过复杂度是10^7的.用dp[i][j][k],表示i位数最高位是j的能否组成所有数位上和为k的数.转移方程很简单,最后,遍历一遍,得到最大值,最小值.
代码如下:
/*************************************************************************
	> 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值