Codeforces Round #656 (Div. 3)ABC

A. Three Pairwise Maximums

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

You are given three positive (i.e. strictly greater than zero) integers x, y and z.
Your task is to find positive integers a, b and c such that x=max(a,b), y=max(a,c) and z=max(b,c), or determine that it is impossible to find such a, b and c.
You have to answer t independent test cases. Print required a, b and c in any (arbitrary) order.

Input

The first line of the input contains one integer t (1 ≤ t ≤ 2⋅ 1 0 4 10^4 ) — the number of test cases. Then t test cases follow.
The only line of the test case contains three integers x, y, and z (1≤x,y,z≤ 1 0 9 10^9 ).

Output

For each test case, print the answer:

  • “NO” in the only line of the output if a solution doesn’t exist;
  • or “YES” in the first line and any valid triple of positive integers a, b and c (1≤a,b,c≤109) in the second line. You can print a, b and c in any order.

Example

input

5
3 2 3
100 100 100
50 49 49
10 30 20
1 1000000000 1000000000

output

YES
3 2 1
YES
100 100 100
NO
NO
YES
1 1 1000000000

题意:

给出x,y,z三个整数(其中x是a和b的最大值,y是a和c的最大值,z是b和c的最大值)。要求判断是否存在a,b,c三个数满足,如果存在输出YES并且输出任意一个例子;如果不存在输出NO。

题解:

(具体看代码:水题)
根据例子可以看出,给出的x,y,z三个数存在以下几种情况:

  1. 三个相同的数
  2. 三个数中其中两个数相同且相同的两个数比较大,如:3 3 2
  3. 三个数中其中两个数相同且相同的两个数比较小,如:3 2 2
  4. 三个不同的数

根据例子可以看出:
YES的情况有:1,2
NO的情况有:3,4
水题废话不多说上AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set> 
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int x, y, z;
		cin >> x >> y >> z;
		if (x == y && x == z && y == z)
		{
			cout << "YES" << endl;
			cout << x << " " << y << " " << z << endl;
			continue;
		}
		if (x != y && x != z && y != z)
		{
			cout << "NO" << endl;
			continue;
		}
		if ((x == y && x != z))
		{
			if (x > z)
			{
				cout << "YES" << endl;
				cout << x << " " << z << " " << z << endl;
			}
			else
			{
				cout << "NO" << endl;
			}
		}
		if ((x == z && x != y))
		{
			if (x > y)
			{
				cout << "YES" << endl;
				cout << x << " " << y << " " << y << endl;
			}
			else
			{
				cout << "NO" << endl;
			}
		}
		if ((y == z && x != y))
		{
			if (y > x)
			{
				cout << "YES" << endl;
				cout << y << " " << x << " " << x << endl;
			}
			else
			{
				cout << "NO" << endl;
			}
		}
	}
	return 0;
}

其实这个代码也不复杂,就是拷贝多几次相同的运算,优化起来也很简单,直接刚开始就调整三个x,y,z数字的顺序直接从大到小(或者从小到大),就不用写显得代码那么复杂了。

B. Restore the Permutation by Merger

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4,3,5,1,2], [3,2,1] are permutations, and [1,1], [0,1], [2,2,1,4] are not.

There was a permutation p[1…n]. It was merged with itself. In other words, let’s take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.

For example, if p=[3,1,2] some possible results are: [3,1,2,3,1,2], [3,3,1,1,2,2], [3,1,3,1,2,2]. The following sequences are not possible results of a merging: [1,3,2,1,2,3], [3,1,2,3,2,1], [3,3,1,2,2,1].

For example, if p=[2,1] the possible results are: [2,2,1,1], [2,1,2,1]. The following sequences are not possible results of a merging: [1,1,2,2], [2,1,1,2], [1,2,2,1].

Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1 ≤ t ≤ 400) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1 ≤ n ≤ 50) — the length of permutation. The second line of the test case contains 2n integers a 1 a_1 , a 2 a_2 ,…, a 2 n a_{2n} (1 ≤ a i a_i ≤ n), where a i a_i is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.

Output

For each test case, print the answer:

For each test case, print the answer: n integers p 1 p_1 , p 2 p_2 ,…, p n p_n (1≤ p i p_i ≤n), representing the initial permutation. It is guaranteed that the answer exists and is unique.

Example

input

5
2
1 1 2 2
4
1 3 1 4 3 4 2 2
5
1 2 1 2 3 4 3 5 4 5
3
1 2 3 1 2 3
4
2 3 2 4 1 3 4 1

output

1 2 
1 3 4 2 
1 2 3 4 5 
1 2 3 
2 3 4 1 

题意:

输入一个数字,限定了要求输出的数字的个数。
按顺序从左到右消除重复的数字。

题解:

用一个flag标志遍历数组消除相同的数字。(具体看代码:水题)

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set> 
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, a[105], flag = 1;
		cin >> n;
		for (int i = 0; i < 2 * n; i++) 
		{
			cin >> a[i];
			for (int j = 0; j < i; j++) 
			{
				if (a[i] == a[j]) 
				{
					flag = 0;//flag = 0就证明之前已经有相同的数字
				}
			}
			if (flag) 
			{
				cout << a[i] << " ";
			}
			flag = 1;
		}
		cout << endl;
	}
	return 0;
}

C. Make It Good

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

You are given an array a consisting of n integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from a to make it a good array. Recall that the prefix of the array a= [ a 1 , a 2 , , a n ] [a_1,a_2,…,a_n] is a subarray consisting several first elements: the prefix of the array a of length k is the array [ a 1 , a 2 , , a k ] ( 0 k n ) . [a_1,a_2,…,a_k] (0 ≤ k ≤ n).

The array b of length m is called good, if you can obtain a non-decreasing array c ( c 1 c 2 c m ) (c_1≤c_2≤⋯≤c_m) from it, repeating the following operation m times (initially, c is empty):

  • select either the first or the last element of b, remove it from b, and append it to the end of the array c.

For example, if we do 4 operations: take b 1 b_1 , then bm, then b m 1 b_{m−1} and at last b 2 b_2 , then b becomes $[b_3,b_4,…,b_{m−3}] $and c = [ b 1 , b m , b m 1 , b 2 ] . c=[b_1,b_m,b_{m−1},b_2].

Consider the following example: b=[1,2,3,4,4,2,1]. This array is good because we can obtain non-decreasing array c from it by the following sequence of operations:

  1. take the first element of b, so b = [ 2 , 3 , 4 , 4 , 2 , 1 ] , c = [ 1 ] ; b=[2,3,4,4,2,1], c=[1];
  2. take the last element of b, so b = [ 2 , 3 , 4 , 4 , 2 ] , c = [ 1 , 1 ] ; b=[2,3,4,4,2], c=[1,1];
  3. take the last element of b, so b = [ 2 , 3 , 4 , 4 ] , c = [ 1 , 1 , 2 ] ; b=[2,3,4,4], c=[1,1,2];
  4. take the first element of b, so b = [ 3 , 4 , 4 ] , c = [ 1 , 1 , 2 , 2 ] ; b=[3,4,4], c=[1,1,2,2];
  5. take the first element of b, so b = [ 4 , 4 ] , c = [ 1 , 1 , 2 , 2 , 3 ] ; b=[4,4], c=[1,1,2,2,3];
  6. take the last element of b, so b = [ 4 ] , c = [ 1 , 1 , 2 , 2 , 3 , 4 ] ; b=[4], c=[1,1,2,2,3,4];
  7. take the only element of b, so b = [ ] , c = [ 1 , 1 , 2 , 2 , 3 , 4 , 4 ] b=[], c=[1,1,2,2,3,4,4] — c is non-decreasing.

Note that the array consisting of one element is good.
Print the length of the shortest prefix of a to delete (erase), to make a to be a good array. Note that the required length can be 0.
You have to answer t independent test cases.

Input

The first line of the input contains one integer t ( 1 t 2 1 0 4 1≤t≤2⋅10^4 ) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n ( 1 n 2 1 0 5 1≤n≤2⋅10^5 ) — the length of a. The second line of the test case contains n integers a 1 , a 2 , , a n a_1,a_2,…,a_n ( 1 a i 2 1 0 5 1≤ai≤2⋅10^5 ), where ai is the i-th element of a.

It is guaranteed that the sum of n does not exceed 2 1 0 5 ( n 2 1 0 5 ) 2⋅10^5 (∑n≤2⋅10^5) .

Output

For each test case, print the answer: the length of the shortest prefix of elements you need to erase from a to make it a good array.

Example

input

5
4
1 2 3 4
7
4 3 3 8 4 5 2
3
1 1 1
7
1 3 1 4 5 3 2
5
5 4 3 2 3

output

0
4
0
2
3

Note

In the first test case of the example, the array a is already good, so we don’t need to erase any prefix.

In the second test case of the example, the initial array a is not good. Let’s erase first 4 elements of a, the result is [ 4 , 5 , 2 ] [4,5,2] . The resulting array is good. You can prove that if you erase fewer number of first elements, the result will not be good.

题意:

好的数列的定义:每次取该数列的头或尾放到另一个数列c中,直至该数列的数全取完,且c是单调递增的。给你一个长度为n的数列,问至少要删去多长的前缀才能使它是一个好的数列。

题解:

从后往前看,找到一个先递增再递减的数列,删去其他多余的。

上AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set> 
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int a[200005];
		int flag = 0;
		int index = 0;
		for (int i = 1; i <= n; i++) cin >> a[i];
		for (int i = n; i > 0; i--)
		{
			if (a[i] > a[i - 1] && flag == 0) flag = 1;
			if (flag && a[i] < a[i - 1])
			{
				index = i - 1;
				break;
			}
		}
		cout << index << endl;
	}
	return 0;
}

大一算法小白,记录自己算法之路的成长历程。

猜你喜欢

转载自blog.csdn.net/m0_46272108/article/details/107449232