Codeforces Round 201 div2 Problem Solving report

Source: Internet
Author: User
Tags integer printf sort time limit cmath
A. Difference Row time limit per test 2 seconds memory limit per test megabytes input standard input output standard o Utput

You want to arrange n integers a1, a2, ..., an on some order in a row. Let's define the value of a 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, the output of 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 a1, A2, ..., an (|ai|≤1000). Output

Print the required sequence x1, x2, ..., xn. Sequence x should be the lexicographically smallest permutation of a then corresponds to an arrangement of the largest POS Sible value. Sample Test (s) input

5
100-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 have a larger value, and among all arrangements with the value of $, the output arrangement is the L exicographically 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.

Given n (2<=n<=100) number, x1,x2, ... , Xn, the permutation of the smallest number of dictionaries with the largest dictionary order (X1-X2) + (x2-x3) +...+ (X[N-1]-XN) value. The expansion is the X1-XN, so that it is the largest value in the X1, the minimum is placed in the Xn, we get an answer, but this is not the smallest dictionary order, so put in the middle of the number of orders to get the smallest dictionary order, the other is the first sort, the maximum and the minimum value, That is, the first number and the last number exchange, you get the answer.

The code is as follows:

/************************************************************************* > File name:a.cpp > Author:gwq > mail:gwq5210@qq.com > Created time:2014 November 14 Friday 22:12 06 seconds **************************************************** /#include <cmath> #include <ctime> #include <cctype> #include <climits> # Include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <set> #incl Ude <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, siz
EOF (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-ary int 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 megabytes input standard input output Put

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

A fixed point of a function was a point which 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 was a fixed point of permutation A0, A1, ..., an-1 if and only if Ai = i. For example, permutation [0, 2, 1] have 1 fixed point and permutation [0, 1, 2] have 3 fixed points.

You are given permutation a. You is allowed to swap from elements of the permutation at most once. Your task is to maximize the number of a fixed points in the resulting permutation. Note that is allowed to make at the 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

5
0 1 3 4 2
Output
3
Topic given a length of n permutation a0,a1, ... , an, the number of this permutation is 0 to n-1, if AI equals I, we say get a score, now you can exchange any number of two in the N number once, of course also can not exchange, make the score maximum.

Ideally, the position I put is J, the J position is I, and I and J are not equal, then the exchange of these two numbers can be increased by two points, if not, we can still exchange one to get a score. Another special case is that a given number is already the maximum score, and there is no need to swap again.

The code is as follows:

/************************************************************************* > File name:b.cpp > Author:gwq > mail:gwq5210@qq.com > Created time:2014 November 14 Friday 22:19 11 seconds **************************************************** /#include <cmath> #include <ctime> #include <cctype> #include <climits> # Include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <set> #incl Ude <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, siz
EOF (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 100010 int 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 megabytes input standard input Output standard OU Tput

It's so boring in the summer holiday, isn ' t it? So Alice and Bob has 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 are the current) can choose the distinct of 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'll finally win the game if both players play optimally. Remember that Alice always moves first. Input

The first line contains a 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

2
2 3
Output
Alice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.