码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #175 (Div. 2) A~D 题解

时间:2019-02-09 10:19:12      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:perm   exgcd   HERE   ios   operation   img   round   abs   red   

A.Slightly Decreasing Permutations

Permutation p is an ordered set of integers p1,??p2,??...,??pn, consisting of n distinct positive integers, each of them doesn‘t exceed n. We‘ll denote the i-th element of permutation p as pi. We‘ll call number n the size or the length of permutation p1,??p2,??...,??pn.

The decreasing coefficient of permutation p1,?p2,?...,?pn is the number of such i (1?≤?i?<?n), that pi?>?pi?+?1.

You have numbers n and k. Your task is to print the permutation of length n with decreasing coefficient k.

Input

The single line contains two space-separated integers: n,?k (1?≤?n?≤?105,?0?≤?k?<?n) — the permutation length and the decreasing coefficient.

Output

In a single line print n space-separated integers: p1,?p2,?...,?pn — the permutation of length n with decreasing coefficient k.

If there are several permutations that meet this condition, print any of them. It is guaranteed that the permutation with the sought parameters exists.

Examples
Input
Copy
5 2
Output
Copy
1 5 2 4 3
Input
Copy
3 0
Output
Copy
1 2 3
Input
Copy
3 2
Output
Copy
3 2 1
构造题;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 2000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 100000007;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}


ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n, k;

int main()
{
	ios::sync_with_stdio(0);
	cin >> n >> k;
	if (k == 0) {
		for (int i = 1; i <= n; i++)cout << i << ‘ ‘;
		return 0;
	}
	
	for (int i = 1; i < n - k; i++) {
		cout << i << ‘ ‘;
	}
	for (int i = n; i >= n - k; i--)cout << i << ‘ ‘;

	return 0;
}

 

B. Find Marble
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya and Vasya are playing a game. Petya‘s got n non-transparent glasses, standing in a row. The glasses‘ positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.

First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn‘t travel from one glass to another: it moves together with the glass it was initially been put in.

After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya‘s task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.

Input

The first line contains three integers: n,?s,?t (1?≤?n?≤?105; 1?≤?s,?t?≤?n) — the number of glasses, the ball‘s initial and final position. The second line contains n space-separated integers: p1,?p2,?...,?pn (1?≤?pi?≤?n) — the shuffling operation parameters. It is guaranteed that all pi‘s are distinct.

Note that s can equal t.

Output

If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.

Examples
Input
Copy
4 2 1
2 3 4 1
Output
Copy
3
Input
Copy
4 3 3
4 1 3 2
Output
Copy
0
Input
Copy
4 3 4
1 2 3 4
Output
Copy
-1
Input
Copy
3 1 3
2 1 3
Output
Copy
-1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 100000007;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}


ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n, s, t;
int p[maxn];

int main()
{
//	ios::sync_with_stdio(0);
	n = rd(); s = rd(); t = rd();
	for (int i = 1; i <= n; i++)p[i] = rd();
	if (s == t) {
		puts("0"); return 0;
	}
	bool fg = 1;
	int cnt = 0;
	int pos = s;
	while (1) {
		pos = p[pos]; cnt++;
		if (pos == s) {
			fg = 0; break;
		}
		else if (pos == t) {
			break;
		}
	}
	if (fg == 0)cout << -1 << endl;
	else {
		cout << cnt << endl;
	}
	return 0;
}

 

C. Building Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Permutation p is an ordered set of integers p1,??p2,??...,??pn, consisting of n distinct positive integers, each of them doesn‘t exceed n. We‘ll denote the i-th element of permutation p as pi. We‘ll call number n the size or the length of permutation p1,??p2,??...,??pn.

You have a sequence of integers a1,?a2,?...,?an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.

Input

The first line contains integer n (1?≤?n?≤?3·105) — the size of the sought permutation. The second line contains n integers a1,?a2,?...,?an (?-?109?≤?ai?≤?109).

Output

Print a single number — the minimum number of moves.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples
Input
Copy
2
3 0
Output
Copy
2
Input
Copy
3
-1 -1 2
Output
Copy
6
Note

In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2,?1).

In the second sample you need 6 moves to build permutation (1,?3,?2).

贪心地从小到大排序;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 500005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 100000007;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}


ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n;
int a[maxn];

int main()
{
//	ios::sync_with_stdio(0);
	n = rd();
	for (int i = 1; i <= n; i++)a[i] = rd();
	ll tot = 0;
	sort(a + 1, a + 1 + n);
	for (int i = 1; i <= n; i++) {
		tot += 1ll * abs(i - a[i]);
	}
	cout << (ll)tot << endl;
	return 0;
}

 

D. Permutation Sum
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Permutation p is an ordered set of integers p1,??p2,??...,??pn, consisting of n distinct positive integers, each of them doesn‘t exceed n. We‘ll denote the i-th element of permutation p as pi. We‘ll call number n the size or the length of permutation p1,??p2,??...,??pn.

Petya decided to introduce the sum operation on the set of permutations of length n. Let‘s assume that we are given two permutations of length n: a1,?a2,?...,?an and b1,?b2,?...,?bn. Petya calls the sum of permutations a and b such permutation c of length n, where ci?=?((ai?-?1?+?bi?-?1) mod n)?+?1 (1?≤?i?≤?n).

Operation 技术图片 means taking the remainder after dividing number x by number y.

Obviously, not for all permutations a and b exists permutation c that is sum of a and b. That‘s why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x,?y (x?≠?y) and the pair of permutations y,?x are considered distinct pairs.

As the answer can be rather large, print the remainder after dividing it by 1000000007 (109?+?7).

Input

The single line contains integer n (1?≤?n?≤?16).

Output

In the single line print a single non-negative integer — the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109?+?7).

Examples
Input
Copy
3
Output
Copy
18
Input
Copy
5
Output
Copy
1800
技术图片
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 500005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}


ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

ll ans[20];
ll fac[20];
ll a[20], b[20];
ll res;
bool use[20], vis[20];
int n;
void dfs(int pos,int up) {
	if (pos == up + 1) {
		res++;
	}
	for (int i = 1; i <= up; i++) {
		if (use[i])continue;
		int C = (a[pos] + i - 2) % up + 1;
		if (vis[C])continue;
		use[i] = 1; vis[C] = 1;
		dfs(pos + 1, up);
		use[i] = 0; vis[C] = 0;
	}
}


int main()
{
//	ios::sync_with_stdio(0);
	n = rd();
	fac[0] = 1ll;
	for (int i = 1; i <= 16; i++)fac[i] = (i*fac[i - 1]) % mod;
/*	for (int j = 1; j <= 16; j++) {
		for (int i = 1; i <= j; i++)a[i] = i;
		ms(vis); ms(use);
		res = 0; dfs(1, j);
		printf("j=%d , ans=%d\n ", j, res);
	}
	*/
	ans[1] = 1; ans[2] = 0; ans[3] = 3;
	ans[4] = 0; ans[5] = 15; ans[6] = 0; ans[7] = 133;
	ans[8] = 0; ans[9] = 2025; ans[10] = 0;
	ans[11] = 37851; ans[12] = 0; ans[13] = 1030367;
	ans[14] = 0; ans[15] = 36362925; ans[16] = 0;
	cout << (ll)ans[n] * fac[n] % mod << endl;
	
	return 0;
}

 

Codeforces Round #175 (Div. 2) A~D 题解

标签:perm   exgcd   HERE   ios   operation   img   round   abs   red   

原文地址:https://www.cnblogs.com/zxyqzy/p/10357142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!