Codeforces Round #617 (Div. 3) A-E2题解

A - Array with Odd Sum

You are given an array $$$a$$$ consisting of $$$n$$$ integers.

In one move, you can choose two indices $$$1 \le i, j \le n$$$ such that $$$i \ne j$$$ and set $$$a_i := a_j$$$. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose $$$i$$$ and $$$j$$$ and replace $$$a_i$$$ with $$$a_j$$$).

Your task is to say if it is possible to obtain an array with an odd (not divisible by $$$2$$$) sum of elements.

You have to answer $$$t$$$ independent test cases.

Input

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases.

The next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2000$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ ($$$\sum n \le 2000$$$).

Output

For each test case, print the answer on it — "YES" (without quotes) if it is possible to obtain the array with an odd sum of elements, and "NO" otherwise.

Example

Input

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

Output

YES
NO
YES
NO
NO

思路:只要序列中有奇数且有奇数个就yes偶数个有一个以上偶数也是是yes因为用偶数可以替代他

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 2009;
int a[maxn];
int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        int n;
        scanf("%d", &n);
        for(int i = 1;i <= n;++i) {
            scanf("%d", &a[i]);
        }
        int sum1 = 0, sum2 = 0;
        for(int i = 1;i <= n;++i) {
            if(a[i] & 1) {
                sum1++;
            }
            else {
                sum2++;
            }
        }
        if(sum1 & 1) {
            printf("YES\n");
        }
        else {
            if(sum2 >= 1 && sum1 >= 1) {
                printf("YES\n");
            }
            else {
                printf("NO\n");
            }
        }
    }
    return 0;
}

Food Buying

Mishka wants to buy some food in the nearby shop. Initially, he has ss burles on his card.

Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number 1≤x≤s1≤x≤s, buy food that costs exactly xx burles and obtain ⌊x10⌋⌊x10⌋ burles as a cashback (in other words, Mishka spends xx burles and obtains ⌊x10⌋⌊x10⌋ back). The operation ⌊ab⌋⌊ab⌋ means aa divided by bb rounded down.

It is guaranteed that you can always buy some food that costs xx for any possible value of xx.

Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.

For example, if Mishka has s=19s=19 burles then the maximum number of burles he can spend is 2121. Firstly, he can spend x=10x=10 burles, obtain 11 burle as a cashback. Now he has s=10s=10 burles, so can spend x=10x=10 burles, obtain 11 burle as a cashback and spend it too.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The next tt lines describe test cases. Each test case is given on a separate line and consists of one integer ss (1≤s≤1091≤s≤109) — the number of burles Mishka initially has.

Output

For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.

Example

Input

6
1
10
19
9876
12345
1000000000

Output

1
11
21
10973
13716
1111111111

思路:贪心的思路每次全部用完然后不停的循环直到没有

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int main() {
	ll t;
	scanf("%lld", &t);
	while(t--) {
		ll n;
		scanf("%lld", &n);
		ll all = n;
		while(n / 10) {
			ll g = n % 10;
			ll sum = n / 10;
			n = sum + g;
			all += sum;
		}
		printf("%lld\n", all);
	}
	return 0;
} 

C. Yet Another Walking Robot

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0)(0,0). Its path is described as a string ss of length nn consisting of characters 'L', 'R', 'U', 'D'.

Each of these characters corresponds to some move:

  • 'L' (left): means that the robot moves from the point (x,y)(x,y) to the point (x−1,y)(x−1,y);
  • 'R' (right): means that the robot moves from the point (x,y)(x,y) to the point (x+1,y)(x+1,y);
  • 'U' (up): means that the robot moves from the point (x,y)(x,y) to the point (x,y+1)(x,y+1);
  • 'D' (down): means that the robot moves from the point (x,y)(x,y) to the point (x,y−1)(x,y−1).

The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye)(xe,ye), then after optimization (i.e. removing some single substring from ss) the robot also ends its path at the point (xe,ye)(xe,ye).

This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string ss).

Recall that the substring of ss is such string that can be obtained from ss by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The next 2t2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the robot's path. The second line of the test case contains one string ss consisting of nn characters 'L', 'R', 'U', 'D' — the robot's path.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

Output

For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers ll and rr such that 1≤l≤r≤n1≤l≤r≤n — endpoints of the substring you remove. The value r−l+1r−l+1 should be minimum possible. If there are several answers, print any of them.

Example

input

Copy

4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR

output

Copy

1 2
1 4
3 4
-1
思路:开一个map套pair桶一下有用过的就更新答案即可
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
map<pair<int, int>, int>A;
const int maxn = 2e5+9;
char s[maxn];
int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		A.clear();
		int n;
		scanf("%d", &n);
		scanf("%s", s+1);
		int sum1 = 0, sum2 = 0;
		int ans1 = -1, ans2 = -1;
		pair<int, int> b;
		b.first = sum1, b.second = sum2;
		A[b] = 1;
		int max1 = inf;
		for(int i = 1;i <= n;++i) {
			if(s[i] == 'L') {
				sum1--;
			}
			else if(s[i] == 'R') {
				sum1++;
			}
			else if(s[i] == 'U') {
				sum2++;
			}
			else {
				sum2--;
			}
			b.first = sum1, b.second = sum2;
			if(A[b]) {
				int g1 = A[b], g2 = i;
				if(max1 > g2-g1+1) {
					max1 = g2-g1+1;
					ans1 = g1, ans2 = g2;
				}
			}
			A[b] = i+1;		
		}
		if(ans1 == -1 && ans2 == -1) {
			printf("-1\n");
		}
		else {
			printf("%d %d\n", ans1, ans2);
		}
	}
	return 0;
}

D. Fight with Monsters

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn monsters standing in a row numbered from 11 to nn. The ii-th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal to bb hp.

You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 00.

The fight with a monster happens in turns.

  1. You hit the monster by aa hp. If it is dead after your hit, you gain one point and you both proceed to the next monster.
  2. Your opponent hits the monster by bb hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster.

You have some secret technique to force your opponent to skip his turn. You can use this technique at most kk times in total (for example, if there are two monsters and k=4k=4, then you can use the technique 22 times on the first monster and 11 time on the second monster, but not 22 times on the first monster and 33 times on the second monster).

Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.

Input

The first line of the input contains four integers n,a,bn,a,b and kk (1≤n≤2⋅105,1≤a,b,k≤1091≤n≤2⋅105,1≤a,b,k≤109) — the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.

The second line of the input contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109), where hihi is the health points of the ii-th monster.

Output

Print one integer — the maximum number of points you can gain if you use the secret technique optimally.

Examples

input

Copy

6 2 3 3
7 10 50 12 1 8

output

Copy

5

input

Copy

1 1 100 99
100

output

Copy

1

input

Copy

7 4 2 1
1 3 5 4 2 7 6

output

Copy

6
思路:用一个桶存一下每个怪兽要被打败消耗的法力次数,然后sort一下贪心的求几个即可
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 2e5+9;
int v[maxn];
vector<int>A;
int main() {
	int n, a, b, k;
	scanf("%d%d%d%d", &n, &a, &b, &k);
	for(int i = 1;i <= n;++i) {
		scanf("%d", &v[i]);
	}
	int sum = 0;
	for(int i = 1;i <= n;++i) {
		int h = v[i] % (a+b);
		if(h == 0) {
			if(b % a == 0) {
				A.push_back(b/a);
			}
			else {
				A.push_back(b/a+1);
			}
		}
		else if(h <= a) {
			sum++;
		}
		else {
			h -= a;
			if(h % a == 0) {
				A.push_back(h/a);
			}
			else {
				A.push_back(h/a+1);
			}
		}
	} 
	sort(A.begin(), A.end());
	for(int i = 0;i < A.size();++i) {
		if(A[i] <= k) {
			k -= A[i];sum++; 
		}
		else {
			break;
		}
	}
	printf("%d\n", sum);
	return 0;
}

E1. String Coloring (easy version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.

You are given a string ss consisting of nn lowercase Latin letters.

You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in ss).

After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.

The goal is to make the string sorted, i.e. all characters should be in alphabetical order.

Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.

Input

The first line of the input contains one integer nn (1≤n≤2001≤n≤200) — the length of ss.

The second line of the input contains the string ss consisting of exactly nn lowercase Latin letters.

Output

If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line.

Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of nn characters, the ii-th character should be '0' if the ii-th character is colored the first color and '1' otherwise).

Examples

input

Copy

9
abacbecfd

output

Copy

YES
001010101

input

Copy

8
aaabbcbb

output

Copy

YES
01011011

input

Copy

7
abcdedc

output

Copy

NO

input

Copy

5
abcde

output

Copy

YES
00000
思路:从前到后先用一个数组记录预设值为-1然后当有一个和这个数要交换就把不符合前面的条件就设为符合的数,当所有的数都不符合就no否则输出这个数组如果是-1就随便设1或0都行 
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 309;
int val[maxn];
int main() {
	int n;
	scanf("%d", &n);
	string s;
	cin >> s;
	val[0] = 1;
	for(int i = 1;i < s.length();++i) {
		val[i] = -1;
		for(int j = 0;j < i;++j) {
			if(s[i] < s[j]) {
				if(val[i] == -1) {
					val[i] = !val[j];
				}
				else if(val[i] == val[j]) {
					printf("NO\n");return 0;
				}
			}
		}
	}
	printf("YES\n");
	for(int i = 0;i < s.length();++i) {
		if(val[i] == -1) {
			printf("1");
		}
		else {
			printf("%d", val[i]);
		}	
	}
	putchar('\n');
	return 0;
}

E2. String Coloring (hard version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.

You are given a string ss consisting of nn lowercase Latin letters.

You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in ss).

After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.

The goal is to make the string sorted, i.e. all characters should be in alphabetical order.

Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of ss.

The second line of the input contains the string ss consisting of exactly nn lowercase Latin letters.

Output

In the first line print one integer resres (1≤res≤n1≤res≤n) — the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.

In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array cc of length nn, where 1≤ci≤res1≤ci≤res and cici means the color of the ii-th character.

Examples

input

Copy

9
abacbecfd

output

Copy

2
1 1 2 1 2 1 2 1 2 

input

Copy

8
aaabbcbb

output

Copy

2
1 2 1 2 1 2 1 1

input

Copy

7
abcdedc

output

Copy

3
1 1 1 1 1 2 3 

input

Copy

5
abcde

output

Copy

1
1 1 1 1 1 
思路:因为要变成的数的种类只有26种最多所有开一个数组统计每种的当前的最大的字母从前往后即可贪心的统计既是最小
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 2e5+9;
int du[30];
int ans[maxn];
int main() {
	int n;
	scanf("%d", &n);
	string s;
	cin >> s;
	int now = 0;
	for(int i = 0;i < s.length();++i) {
		for(int j = 0;j < 26;++j) {
			if(s[i]-'a' >= du[j]) {
				du[j] = s[i]-'a';
				now = max(now, j);
				ans[i] = j;break;		
			}	
		} 
	}
	printf("%d\n", now+1);
	for(int i = 0;i < s.length();++i) {
		printf("%d ", ans[i]+1);
	}
	putchar('\n');
	return 0;
}
发布了219 篇原创文章 · 获赞 43 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_43568078/article/details/104292774