A. Nearest Minimums
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

Input

The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1(1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Output

Print the only number — distance between two nearest minimums in the array.

Examples
input
2
3 3
output
1
input
3
5 6 5
output
2
input
9
2 1 3 5 4 1 2 3 1
output
3


思路:暴力

#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

const int N=1e5+5;
int INF=0x3f3f3f3f;
int a[N];
vector <int> vec;

int main(void){
    int n;
    cin >> n;
    int mmin=INF;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        mmin=min(mmin,a[i]);
    }
    int index;
    for(int i=1;i<=n;i++)
        if(a[i]==mmin)  vec.push_back(i);
    int minn=vec[1]-vec[0];
    for(int i=2;i<vec.size();i++)   minn=min(minn,vec[i]-vec[i-1]);
    cout << minn << endl;
}



B. Two Cakes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces.

Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met:

  1. Each piece of each cake is put on some plate;
  2. Each plate contains at least one piece of cake;
  3. No plate contains pieces of both cakes.

To make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.

Help Ivan to calculate this number x!

Input

The first line contains three integers na and b (1 ≤ a, b ≤ 1002 ≤ n ≤ a + b) — the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.

Output

Print the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.

Examples
input
5 2 3
output
1
input
4 7 10
output
3
Note

In the first example there is only one way to distribute cakes to plates, all of them will have 1 cake on it.

In the second example you can have two plates with 3 and 4 pieces of the first cake and two plates both with 5 pieces of the second cake. Minimal number of pieces is 3.


思路:暴力枚举所有方案数

#include <bits/stdc++.h>

using namespace std;

int t[300];
vector <int> res;

int main(void){
	int	n,a,b;
	cin >> n>> a>> b;
	int ans=-0x3f3f3f3f;
	for(int i=1;i<=n-1;i++){
		int t1=a/i;
		int t2=b/(n-i);
		res.push_back(min(t1,t2));
	}
	for(int i=0;i<res.size();i++)	ans=max(ans,res[i]);
	cout << ans << endl;
}


C. Three Garlands
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.

When a garland is switched on, it periodically changes its state — sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds xx + kix + 2kix + 3ki and so on.

Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x1x2 and x3 (not necessarily distinct) so that he will switch on the first garland during x1-th second, the second one — during x2-th second, and the third one — during x3-th second, respectively, and during each second starting from max(x1, x2, x3) at least one garland will be lit.

Help Mishka by telling him if it is possible to do this!

Input

The first line contains three integers k1k2 and k3 (1 ≤ ki ≤ 1500) — time intervals of the garlands.

Output

If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.

Otherwise, print NO.

Examples
input
2 2 3
output
YES
input
4 2 3
output
NO
Note

In the first example Mishka can choose x1 = 1x2 = 2x3 = 1. The first garland will be lit during seconds 1, 3, 5, 7, ..., the second — 2, 4, 6, 8, ..., which already cover all the seconds after the 2-nd one. It doesn't even matter what x3 is chosen. Our choice will lead third to be lit during seconds 1, 4, 7, 10, ..., though.

In the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit.


思路:最小值《=3分类讨论,这题不知道正解是什么

#include <bits/stdc++.h>

using namespace std;

int cnt1,cnt2,cnt3,cnt4;

int main(void){
	int a[5];
	cin >>a[1]>>a[2]>>a[3];
	sort(a+1,a+4);
	bool flag=false;
	if(a[1]==1)	flag=true;
	else if(a[1]==2&& (a[2]==2||a[3]==2)) flag=true;
	else if(a[1]==3&&a[2]==3&&a[3]==3)	flag=true;
	if(a[1]==2&&a[2]==4&&a[3]==4)	flag=true;
	if(flag)	cout <<"YES"<<endl;
	else	cout <<"NO"<<endl;
	return 0;
}

D. Inversion Counting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1)(3, 1)(4, 1)(4, 3).

You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].

After each query you have to determine whether the number of inversions is odd or even.

Input

The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.

The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.

Then m lines follow, i-th line containing two integers liri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.

Output

Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and evenotherwise.

Examples
input
3
1 2 3
2
1 2
2 3
output
odd
even
input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
output
odd
odd
odd
even
Note

The first example:

  1. after the first query a = [2, 1, 3], inversion: (2, 1);
  2. after the second query a = [2, 3, 1], inversions: (3, 1)(3, 2).

The second example:

  1. a = [1, 2, 4, 3], inversion: (4, 3);
  2. a = [3, 4, 2, 1], inversions: (3, 1)(4, 1)(3, 2)(4, 2)(4, 3);
  3. a = [1, 2, 4, 3], inversion: (4, 3);
  4. a = [1, 4, 2, 3], inversions: (3, 2)(4, 2).

题意:有n个数字,问把[l,r]反过来后,逆序数是奇数还是偶数

思路:可以求出原始序列的逆序数个数cnt。当部分序列被倒置后,仅影响该序列的顺序和逆序个数。长度为n的序列, tot=顺序+逆序=(n-1)*n/2,那么令n=r-l+1。对于总答案的贡献值为cnt(顺序)-cnt(逆序)  如果tot是偶数,说明顺序个数与逆序奇偶性相同,不影响原来的奇偶性。如果是奇数,那么相反。


考察:n*(n-1)/2个逆序数对=顺序数+逆序数。 对答案贡献为cnt(顺序)-cnt(逆序)  。 根据tot来决定奇偶性的变化


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int a[2000];
ll cnt;

int main(void){
	int n,m;
	cin >>n;
	for(int i=1;i<=n;i++)	scanf("%d",&a[i]);
	//for(int i=1;i<=n;i++)	printf("%d",a[i]);
	for(int i=2;i<=n;i++){
		for(int j=1;j<i;j++){
			if(a[j]>a[i])	cnt++;
		}
	}
	//cout <<"cnt="<<cnt<<endl;
	cin >>m;
	for(int i=1;i<=m;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		ll t=1LL*(r-l+1)*(r-l)/2;
		if(t%2==1){
			if(cnt%2==1)	cout<<"even"<<endl;
			else	cout<<"odd"<<endl;
			cnt++;
		}
		else{
			if(cnt%2==1)	cout<<"odd"<<endl;
				else	cout<<"even"<<endl;
		}
	}
}

E. Stack Sorting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).

You may perform the following operations until both a and s are empty:

  • Take the first element of a, push it into s and remove it from a (if a is not empty);
  • Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).

You can perform these operations in arbitrary order.

If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.

For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:

  1. Remove 3 from a and push it into s;
  2. Remove 1 from a and push it into s;
  3. Remove 1 from s and append it to the end of b;
  4. Remove 2 from a and push it into s;
  5. Remove 2 from s and append it to the end of b;
  6. Remove 3 from s and append it to the end of b.

After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable[2, 3, 1] is not stack-sortable.

You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k qi = pi, and qk > pk). You may not swap or change any of first kelements of the permutation.

Print the lexicographically maximal permutation p you can obtain.

If there exists no answer then output -1.

Input

The first line contains two integers n and k (2 ≤ n ≤ 2000001 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.

The second line contains k integers p1p2, ..., pk (1 ≤ pi ≤ n) — the first k elements of p. These integers are pairwise distinct.

Output

If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.

Otherwise print -1.

Examples
input
5 3
3 2 1
output
3 2 1 5 4 
input
5 3
2 3 1
output
-1
input
5 1
3
output
3 2 1 5 4 
input
5 2
3 4
output
-1

思路:对于当前的a[]i,有3种情况

1.是需要的数

2.不是需要的数,但比栈中所有元素都要小。 如果比任何一个大,肯定不满足条件,输出-1

若满足,输出前k个元素


栈中可能有残留的元素,那么输出有约束条件的数字(比上一个元素小,比下一个栈元素大)


再逆序从最大输出。但这题被Stack 给TLE了,手动模拟就A了,不清楚原因。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD	1000000007;
#define bug1 cout <<"bug1"<<endl
#define bug2 cout <<"bug2"<<endl
#define bug3 cout <<"bug3"<<endl
using namespace std;
typedef long long ll;

const int MAX_N=2e5+5;
int a[MAX_N];
int used[MAX_N];
int s[MAX_N];

int main(void){
	int n,k;
	cin >> n>>k;
	int b=1,top=0,flag=1;
	for(int i=1;i<=k;i++)	scanf("%d",&a[i]);
	for(int i=1;i<=k;i++){
		if(a[i]==b)	used[a[i]]=1,b++;
		else if(top==0||a[i]<s[top-1])	used[a[i]]=1,s[top++]=a[i];
		else{
			flag=0;
			break;
		}
		while(top&&s[top-1]==b)	b++,top--;
	}
	if(!flag)	cout<<"-1"<<endl;
	else{
		for(int i=1;i<=k;i++)	printf("%d ",a[i]);
		if(top){
			int t=0;
			while(top){
				for(int i=s[top-1]-1;i>t;i--){
					if(!used[i]){
						used[i]=1;
						printf("%d ",i);
					}
				}
				t=s[top-1];
				top--;
			}
		}
		for(int i=n;i>0;i--){
			if(!used[i])	printf("%d ",i);
		}
	}
}