lsnu集训第一天下午场比赛

//A
#include <stdio.h>
#include <stack>
using namespace std;


int main()
{
	char s[100005];
	while(scanf("%s",&s)!=EOF){
		int index=0;
		stack<char> sta;
		
		while(s[index]){
			if(s[index]=='('){
				sta.push('(');
			}
			else if(s[index]==')'){
				sta.pop();
			}
			else if(s[index]=='B'){
				printf("%d\n",sta.size());
				break;
			}
			index++;
		}
	}
	return 0;
}
//B
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;

vector<int> v[26];


const int maxs=2*1e5+5; 
void solve(){
	int len;
	char s[maxs],temp[maxs];
	while(scanf("%d%s",&len,&s)!=EOF){
		int index=0;
		while(s[index]){
			v[s[index]-'a'].push_back(index);
			index++;
		}
		
		int n;
		scanf("%d",&n);
		int count[26];
		for(int i=0;i<n;i++){
			scanf("%s",&temp);
			for(int j=0;j<26;j++) count[j]=0;
			
			int tindex=0;
			while(temp[tindex]){
				count[temp[tindex]-'a']++;
				tindex++;
			}
			
			int ans=0;
			for(int j=0;j<26;j++) if(count[j]) ans=std::max(ans,v[j][count[j]-1]);
			
			printf("%d\n",ans+1);
		}
	}
	
}


int main(){
	solve();
	return 0;
}
//C
#include <iostream>
#include <map>
using namespace std;


int main()
{
	std::ios::sync_with_stdio(false);
    std::cin.tie(0);
	map<string,int> ma;
	
	int n;
	while(cin>>n){
		if(n==0) break;
		string s;
		ma.clear();
		for(int i=0;i<n;i++){
			cin>>s;
			if(ma.count(s)) ma[s]++;
			else ma[s]=0;
		}
		int tmax=0;
		for(map<string,int>::iterator ite=ma.begin();ite!=ma.end();ite++){
			if(ite->second>tmax){
				tmax=ite->second;
				s=ite->first;
			}
		}
		cout<<s<<endl;
	} 
	return 0;
}

 

 

 

A - Keanu Reeves

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 s of length n consisting of only zeroes and ones. We need to cut s 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 3 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≤100) — the length of the string s

.

The second line contains the string s

of length n

consisting only from zeros and ones.

Output

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

In the second line, output k 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.

 

1.题是什么?

    现在有这么一个长度n(100以内)的01字符串 ,现在要你将之切割为尽可能少的k个字符串使这k个字符串内包含的01个数皆不相同。

2.思路

    一道奇怪的题,其实只用先遍历一遍原串是否01个数已经不相同,已不相同那便不用切割,k为1输出原串就好,相同则选取最简单的方式,即将第一个字符切割出来,剩下的自然必然01个数不同,此为k为2的答案。

3.ac代码

#include <iostream> 
#include <stdio.h>
using namespace std;

const int maxn=1e2+5;

void solve(){
	int n;
	char s[maxn];
	scanf("%d%s",&n,&s);
	int sum=0;
	for(int i=0;i<n;i++){
		if(s[i]=='1') sum++;
		else sum--;
	}
	if(sum==0) printf("2\n%c %s\n",s[0],s+1);
	else printf("1\n%s\n",s); 
	
}

int main(){
	solve();
	return 0;
} 

B - Number Circle

You are given n 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+1 and 8>1+6

.

Input

The first line contains a single integer n (3≤n≤1e5) — the number of numbers.

The second line contains n integers a1,a2,…,an (1≤ai≤1e9) — 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.

 

1.题是什么?

    现在给你1e5个数字,要你将这1e5个数字排成一个环且满足任何一个数字小于周边的两个数字之和,可以则输出YES并输出顺时针输出顺序,不可以则输出NO。

2.思路

  其实我们只需要将这n个数字排序即可使得第一个到第n-1个数字全部满足要求,此时第n个数字若也满足要求,则直接输出,若不满足要求,则将第n个与第n-1个互换,互换之后若依旧不满足则情况必然为NO,因为此时说明第二大与第三大的数字之和都没第一大的数字大,不存在其他和比第一个大的一对数字,若互换之后满足则直接输出此种情况。

3.ac代码

#include <iostream> 
#include <algorithm>
#include <stdio.h>
using namespace std;

const int maxn=1e5+5;

void solve(){
	int n,a[maxn];
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	sort(a,a+n);
	swap(a[n-2],a[n-1]);
	if(a[n-2]>=a[n-1]+a[n-3]){
		printf("NO\n");
	}
	else{
		printf("YES\n");
		for(int i=0;i<n-1;i++){
			printf("%d ",a[i]);
		}
		printf("%d\n",a[n-1]);
	}
}
	
int main(){
	solve();
	return 0;
} 

 

C - Candies!

 

Consider a sequence of digits of length 2^{k} [a1,a2,…,a2^{k}]. We perform the following operation with it: replace pairs (a2i+1,a2i+2) with (a2i+1+a2i+2)mod10 for 0≤i<2k−1. For every i where a2i+1+a2i+2≥10 we 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 by 10 (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≥10 and 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≥1.

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 n s1,s2,…sn. You have to answer q queries of the form (li,ri), where for i-th query you have to output f([sli,sli+1,…,sri]). It is guaranteed that ri−li+1 is of form 2k for some nonnegative integer k.

Input

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

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

The third line contains a single integer q(1≤q≤1e5) — the number of queries.

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

Output

Output q lines, in i-th line output single integer — f([sli,sli+1,…,sri]), answer to the i-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

Note

The first example illustrates an example from the statement.

f([7,3,1,7])=1 : sequence of operations is [7,3,1,7]→[(7+3)mod10,(1+7)mod10] = [0,8] and one candy as 7+3≥10 → [(0+8)mod10] = [8], so we get only 1 candy.

f([9])=0 as we don't perform operations with it.

 

1.题是什么

  

2.思路

实质是一道区间和问题,建议使用线段树或者树状数组解决。我的线段树博客

3.ac代码

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

const int maxn=1e5+5;
int bit[maxn+1],n;
 
int sum(int i){
	int s=0;
	while(i>0){
		s+=bit[i];
		i-=i&-i;
	}
	return s;
}
 
void add(int i,int x){
	while(i<=n){
		bit[i]+=x;
		i+=i&-i;
	}
}
 
void init(int n){
	int tn=n,t;
	n=1;
	while(n<tn) n=(n<<1);
	memset(bit,0,2*n-1);
	for(int i=1;i<=tn;i++){
		scanf("%d",&t);
		add(i,t);
	}
}


void solve(){
	scanf("%d",&n);
	init(n);
	
	int q,l,r;
	scanf("%d",&q);
	while(q--){
		scanf("%d%d",&l,&r);
		printf("%d\n",(sum(r)-sum(l))/10);
	}
}
	
int main(){
	solve();
	return 0;
} 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章