codeforces round 201 div2解题报告

来源:互联网 发布:win8.1无法连接windows 编辑:程序博客网 时间:2024/06/05 20:10
A. Difference Row
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You want to arrange n integers a1, a2, ..., an in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.

More formally, let's denote some arrangement as a sequence of integers x1, x2, ..., xn, where sequence x is a permutation of sequence a. The value of such an arrangement is (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).

Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.

Input

The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers a1a2...an (|ai| ≤ 1000).

Output

Print the required sequence x1, x2, ..., xn. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.

Sample test(s)
input
5100 -100 50 0 -50
output
100 -50 0 50 -100 
Note

In the sample test case, the value of the output arrangement is (100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one.

Sequence x1, x2, ... , xp is lexicographically smaller than sequence y1, y2, ... , yp if there exists an integer r (0 ≤ r < p) such that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1.

给定n(2<=n<=100)个数,x1,x2,。。。,xn,求使式子(x1-x2)+(x2-x3)+...+(x[n-1]-xn)值最大的字典序最小的一个这n个数的排列。将式子展开是就x1-xn,使它最大就是将最大值放在x1,最小值放在xn,我们就得到一个答案,但这个不是字典序最小的,所以放好后,将中间的数排序就得到了字典序最小的,另一种是先排序,将最大值和最小值,也就是第一个数和最后一个数交换,就得到了答案。

代码如下:

/*************************************************************************> File Name: a.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月14日 星期五 22时12分06秒 ************************************************************************/#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 110int num[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]);}sort(num, num + n);int tmp = num[0];num[0] = num[n - 1];num[n - 1] = tmp;for (int i = 0; i < n; ++i) {printf("%d%c", num[i], (i == n - 1) ? '\n' : ' ');}}return 0;}

B. Fixed Points
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, sequence [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] are not.

A fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a0, a1, ..., an - 1 if and only if ai = i. For example, permutation [0, 2, 1] has 1 fixed point and permutation [0, 1, 2] has 3 fixed points.

You are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.

Input

The first line contains a single integer n (1 ≤ n ≤ 105). The second line contains n integers a0, a1, ..., an - 1 — the given permutation.

Output

Print a single integer — the maximum possible number of fixed points in the permutation after at most one swap operation.

Sample test(s)
input
50 1 3 4 2
output
3
题目给定一个长度为n的排列a0,a1,。。。,an,这个排列的数为0到n-1,如果ai等于i,我们就说得到了一个分数,现在你可以在n个数中任意交换两个数一次,当然也可以不交换,使得得到的分数最大。

最理想的状况就是,第i位放的是j,第j位放的是i,并且i和j不相等,那么交换这两个数就可以增加两个分数,如果没有这种情况,我们还是可以交换某一位得到一个分数。另一种特殊情况就是给定的数已经是最大分数了,就不需要再交换了。

代码如下:

/*************************************************************************> File Name: b.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月14日 星期五 22时19分11秒 ************************************************************************/#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 100010int num[N];int main(int argc, char *argv[]){int n;while (scanf("%d", &n) != EOF) {int ans = 0;for (int i = 0; i < n; ++i) {scanf("%d", &num[i]);if (num[i] == i) {++ans;}}int flag = 0;for (int i = 0; i < n; ++i) {if (num[i] != i && num[num[i]] == i) {flag = 1;ans += 2;break;}}if (flag == 0) {++ans;}ans = min(ans, n);printf("%d\n", ans);}return 0;}

C. Alice and Bob
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).

If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.

Input

The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.

Output

Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).

Sample test(s)
input
22 3
output
Alice
input
25 3
output
Alice
input
35 6 7
output
Bob
Note

Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.

我们先来考虑两个数的情况,假设两个数a,b,因为是取两个数的绝对值,那么相当于大数减去小数,然后把结果放入集合中,这个操作类似于更相减损术,不知道的可以百度,是一种求最大公约数的算法,不过是通过大数减小数来得到的,当一个很大的数和一个很小的数减的时候,效率就不高了,所以,这两个数a,b,一直这样减下去的话,就一定可以得到a和b的最大公约数m,然后,依次就可以得到2m,3m,等等,直到a和b中的最大数,假设这个最大公约数是m,那么这个序列就有max(a,b)/m个数,所以Alice和Bob最多玩max(a,b)/m - 2局,如果这个数是偶数,那么Bob就赢了,否则Alice就赢了。把这个结论推广到n个数的情况也是适用的,通过相减一定可以得到这n个数的最大公约数,只要判断能玩局数的最大值就行了。

代码如下:

/*************************************************************************> File Name: c.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月14日 星期五 22时34分00秒 ************************************************************************/#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 110int gcd(int a, int b){return (a == 0) ? b : gcd(b % a, a);}int main(int argc, char *argv[]){int n, num;while (scanf("%d", &n) != EOF) {int mx = -INF;int tmp = 0;for (int i = 0; i < n; ++i) {scanf("%d", &num);tmp = gcd(tmp, num);mx = max(mx, num);}if ((mx / tmp - n) % 2 == 1) {printf("Alice\n");} else {printf("Bob\n");}}return 0;}


0 0