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.
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).
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.
5 100 -100 50 0 -50
100 -50 0 50 -100
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 110
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;
}
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.
The first line contains a single integer n (1 ≤ n ≤ 105). The second line contains n integers a0, a1, ..., an - 1 — the given permutation.
Print a single integer — the maximum possible number of fixed points in the permutation after at most one swap operation.
5 0 1 3 4 2
3
最理想的状况就是,第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 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;
}
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.
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.
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
2 2 3
Alice
2 5 3
Alice
3 5 6 7
Bob
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.
代码如下:
/*************************************************************************
> 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 110
int 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;
}