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;
} 

 

 

 

 

 

 

 

 

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