1188, 1189 - Codeforces Round #572

1188, 1189 - Codeforces Round #572(题解)

A.Keanu Reeves

time limit per test 1 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
After playing Neo in the legendary "Matrix" trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem.

Let’s call a string consisting of only zeroes and ones good if it contains different numbers of zeroes and ones. For example, 1, 101, 0000 are good, while 01, 1001, and 111000 are not good.

We are given a string ss of length nn consisting of only zeroes and ones. We need to cut ss into minimal possible number of substrings s1,s2,…,sk such that all of them are good. More formally, we have to find minimal by number of strings sequence of good strings s1,s2,…,sk such that their concatenation (joining) equals s, i.e. s1+s2+⋯+sk=s

For example, cuttings 110010 into 110 and 010 or into 11 and 0010 are valid, as 110, 010, 11, 0010 are all good, and we can’t cut 110010 to the smaller number of substrings as 110010 isn’t good itself. At the same time, cutting of 110010 into 1100 and 10 isn’t valid as both strings aren’t good. Also, cutting of 110010 into 1, 1, 0010 isn’t valid, as it isn’t minimal, even though all 33 strings are good.

Can you help Keanu? We can show that the solution always exists. If there are multiple optimal answers, print any.

Input

The first line of the input contains a single integer n (1≤n≤1001≤n≤100) — the length of the string ss.

The second line contains the string s of length nn consisting only from zeros and ones.

Output

In the first line, output a single integer k (1≤k1≤k) — a minimal number of strings you have cut ss into.

In the second line, output kk strings s1,s2,…,sk separated with spaces. The length of each string has to be positive. Their concatenation has to be equal to s and all of them have to be good.

If there are multiple answers, print any.

Examples

input

1
1

output

1
1

input

2
10

output

2
1 0

input

6
100011

output

2
100 011

Note

In the first example, the string 1 wasn’t cut at all. As it is good, the condition is satisfied.

In the second example, 1 and 0 both are good. As 10 isn’t good, the answer is indeed minimal.

In the third example, 100 and 011 both are good. As 100011 isn’t good, the answer is indeed minimal.

题意

签到题,给你一个字符串s,让你把这个字符串拆分成最小的字串,使每个子串中的0和1数目不相同。问最小的字串数。可以很轻松发现在s的0和1相等的时候只要在第一个字母后切一下,不相等的话不用切就好了

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,count_0=0,count_1=0,i;
	string s;
	cin>>n;
	cin>>s;
	for(i=0;i<n;i++)
	{
		if(s[i]=='0')
		count_0++;
		else
		count_1++;
	}
	if(count_0==count_1)
	{
		cout<<2<<endl;
		cout<<s[0]<<' ';
		for(int i=1;i<n;i++)
		cout<<s[i];
		return 0;
	}
	else
	cout<<1<<endl;
	cout<<s;

	return 0;
}

B. Number Circle

time limit per test 1 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
You are given nn numbers a1,a2,…,an. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?

For example, for the array [1,4,5,6,7,8], the arrangement on the left is valid, while arrangement on the right is not, as 5≥4+1and 8>1+6
在这里插入图片描述

Input

The first line contains a single integer nn (3≤n≤10^5) — the number of numbers.

The second line contains nn integers a1,a2,…,an (1≤ai≤10^9) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).

Output

If there is no solution, output “NO” in the first line.

If there is a solution, output “YES” in the first line. In the second line output n numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.

Examples

input

3
2 4 3

output

YES
4 2 3

input

5
1 2 3 4 4

output

YES
4 4 2 1 3

input

3
13 8 5

output

NO

input

4
1 10 100 1000

output

NO

Note

One of the possible arrangements is shown in the first example:

4<2+3

2<4+3

3<4+2

One of the possible arrangements is shown in the second example.

No matter how we arrange 13,8,5 in a circle in the third example, 13 will have 8 and 5 as neighbors, but 13≥8+5
There is no solution in the fourth example.

题意

给你一个数组,问你是否可以将其拼成一个圆环,使每个数都小于其两边的数。我们可以用两个数组,先给这些数排序,建立两个数组是sh,ch,再找出最大值,最大值存入一个数组中sh,一个数组从排序后的数从后往前,每隔一个数存入该数组sh,另一个数存入另一个数组ch,再建立一个数组b,sh数组从前往后存入b,ch数组从后往前存入b,这样b中存的就是一个优化的环,只要暴力判断b中每个数是否小于相邻两个数之和即可,若不可以输出NO,可以把b数组输出.

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int main()
{
	int n,count=0;
	int number[maxn],circle[maxn];
	vector<int>small,big;
	cin>>n;
	for(int i=0;i<n;i++)
	cin>>number[i];
	sort(number,number+n);
	big.push_back(number[n-1]);
	for(int i=n-2;i>=0;i--)
	{
		if(i%2==0)
		big.push_back(number[i]);
		else
		small.push_back(number[i]);
	}
	for(int i=0;i<big.size();i++)
	{
		circle[count]=big[i];
		count++;	
	}
	for(int i=small.size()-1;i>=0;i--)
	{
		circle[count]=small[i];
		count++;
	}
	bool flag=true;
	for(int i=1;i<n-1;i++)
	{
		if(circle[0]>=circle[1]+circle[n-1])
		{
			flag=false;
			break;
		}
		if(circle[i]>=circle[i-1]+circle[i+1])
		{
			flag=false;
			break;
		}
	}
	if(flag==false)
	{
		cout<<"NO"<<endl;
		return 0;
	}
	else
	{
		cout<<"YES"<<endl;
		for(int i=0;i<n;i++)
		cout<<circle[i]<<' ';
		return 0;
	}
	
}

C. Candies!

time limit per test 2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
Consider a sequence of digits of length 2k ,[a1,a2,…,a2k] We perform the following operation with it: replace pairs (a2i+1,a2i+2) with (a2i+1+a2i+2)mod10 for 0≤i<2^k−1. For every ii where a2i+1+a2i+2≥10we get a candy! As a result, we will get a sequence of length 2k−1.

Less formally, we partition sequence of length 2k into 2k−1 pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth ……, the last pair consists of the (2k−1)-th and (2k)-th numbers. For every pair such that sum of numbers in it is at least 10, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum b10 (and don’t change the order of the numbers).

Perform this operation with a resulting array until it becomes of length 1. Let f([a1,a2,…,a2k])denote the number of candies we get in this process.

For example: if the starting sequence is [8,7,3,1,7,0,9,4]then:

After the first operation the sequence becomes [(8+7)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10] == [5,4,7,3], and we get 2 candies as 8+7≥10and 9+4≥10

After the second operation the sequence becomes [(5+4)mod10,(7+3)mod10]== [9,0], and we get one more candy as 7+3≥10

After the final operation sequence becomes [(9+0)mod10]== [9].

Therefore, f([8,7,3,1,7,0,9,4])=3 as we got 3 candies in total.

You are given a sequence of digits of length nn s1,s2,…sn. You have to answer q queries of the form (li,ri), where for ii-th query you have to output f([sli,sli+1,…,sri]). It is guaranteed that ri−li+1is of form 2k for some nonnegative integer k.

Input

The first line contains a single integer n (1≤n≤10^5) — the length of the sequence.

The second line contains n digits s1,s2,…,sn (0≤si≤9).

The third line contains a single integer qq (1≤q≤10^5) — the number of queries.

Each of the next q lines contains two integers li, ri (1≤li≤ri≤n) — ii-th query. It is guaranteed that ri−li+1 is a nonnegative integer power of 2.

Output

Output q lines, in ii-th line output single integer — f([sli,sli+1,…,sri]), answer to the ii-th query.

Examples

input

8
8 7 3 1 7 0 9 4
3
1 8
2 5
7 7

output

3
1
0

input

6
0 1 2 3 3 5
3
1 2
1 4
3 6

output

0
0
1

题意

给你一个数组,计算某个区间内获得的糖果数目。表面很复杂,实则就是问一段区间[l, r]的区间和去除以10并且向下取整得到的数。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int main()
{
	int n,number[maxn],q,l,r;
	int candies[maxn]={0};
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>number[i];
		candies[i]=number[i]+candies[i-1];
	}
	cin>>q;
	while(q--)
	{
		int count_candies=0;
		cin>>l>>r;
		for(int i=l;i<r;i++)
		{
			count_candies=(candies[r]-candies[l-1])/10;
		}
		cout<<count_candies<<endl;
	}
	return 0;
}

D1. Add on a Tree

time limit per test 1 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.

You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.

For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding −0.5 on the path from 4 to 5.

在这里插入图片描述
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?

Leaf is a node of a tree of degree 1. Simple path is a path that doesn’t contain any node twice.

Input

The first line contains a single integer nn (2≤n≤10^5) — the number of nodes.

Each of the next n−1 lines contains two integers u and v (1≤u,v≤n, u≠v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.

Output

If there is a configuration of real numbers written on edges of the tree that we can’t achieve by performing the operations, output “NO”.

Otherwise, output “YES”.

You can print each letter in any case (upper or lower).

Examples

input

2
1 2

output

YES

input

3
1 2
2 3

output

NO

input

5
1 2
1 3
1 4
2 5

output

NO

input

6
1 2
1 3
1 4
2 5
2 6

output

YES

题意

给出一颗树,现在可以任意选择两个叶子节点,为这两个叶子节点之间的简单路径上的所有的边加上一个或者减去一个任意的实数,现问给出的这棵树可否通过有限次操作使得树上的边的权值达到任意的组合状态。

原题的题意即所有的边的权值是否可以最终都变成不同的值。如果想要对任意的边修改权值而不影响到其他边,则这条边就需要被叶子路径经过至少两次,这就要求除了叶子节点以外所有的点的度都要大于等于3

#include<bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 7;
int N, du[maxN];
int main()
{
    scanf("%d", &N);
    for(int i=1, u, v; i<N; i++)
    {
        scanf("%d%d", &u, &v);
        du[u]++;
        du[v]++;
    }
    for(int i=1; i<=N; i++) if(du[i] == 2) { printf("NO\n"); return 0; }
    printf("YES\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值