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

D - Vasya And Array

Vasya has an array a1,a2,…,an You don't know this array, but he told you mfacts about this array. The i-th fact is a triple of numbers ti, li and ri (0≤ti≤1,1≤li<ri≤n) and it means:

  • if ti=1 then subbarray ali,ali+1,…,ari is sorted in non-decreasing order;
  • if ti=0 then subbarray ali,ali+1,…,ari is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter.

For example if a=[2,1,1,3,2] then he could give you three facts: t1=1,l1=2,r1=4 (the subarray [a2,a3,a4]=[1,1,3] is sorted), t2=0,l2=4,r2=5 (the subarray [a4,a5]=[3,2] is not sorted), and t3=0,l3=3,r3=5 (the subarray [a3,a5]=[1,3,2] is not sorted).

You don't know the array a. Find any array which satisfies all the given facts.

Input

The first line contains two integers n and m (2≤n≤1000,1≤m≤1000).

Each of the next m lines contains three integers ti, li and ri (0≤ti≤1,1≤li<ri≤n).

If ti=1 then subbarray ali,ali+1,…,ari is sorted. Otherwise (if ti=0) subbarray ali,ali+1,…,ari is not sorted.

Output

If there is no array that satisfies these facts in only line print NO (in any letter case).

If there is a solution, print YES (in any letter case). In second line print n

integers a1,a2,…,an (1≤ai≤109) — the array a , satisfying all the given facts. If there are multiple satisfying arrays you can print any of them.

Examples

Input

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

Output

YES
1 2 2 3 5 4 4

Input

4 2
1 1 4
0 2 3

Output

NO

 

1.题是什么

    给你一个数字n,再给你m组(t,l,r)限制条件,t=1表示 [l,r] 区间内数字是非递减的,也就是后面的数字大于等于前面的数字,t=0表示 [l,r] 内数字是并非非递减的,也就是存在某个数字比前面的数字大,关键在n和m以及l,r都是1000以内,要你回答是否存在这种数组并输出任意一种符合的情况。

2.思路

    首先我们要明白 [l,r] 区间内数字是非递减的意味着[l+1,r]内的每个数字必须大于等于前一个数字

    其次我们要合并t=1的所有区间,合并方法参考 区间合并(差分) 

    之后对每个t=0的区间进行判断,若[l+1,r] 内的每个元素都被t=1的区间所要求大于等于前一个数字,那自然不满足条件该输出NO,因为至少得有一个可以小于前面的数字,这一条限制才能满足,不能赶尽杀绝。

   在所有t=0的限制都满足之后,进行构造就好了,由于ai最大能为1e9而数组最大才1000,故而以1e9为开始,在需要递减的区间都依次减一,不需要递减的区间都保持当前值输出就好。

3.ac代码

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

vector<int> v[26];


const int maxs=1e3+5; 
void solve(){
	int n,m;
	scanf("%d%d",&n,&m);
	int vis[maxs];
	pair<int,int> unnondecrease[maxs];
	int count=0;
	for(int j=0;j<maxs;j++) vis[j]=0; 
	int t,l,r;
	for(int i=0;i<m;i++){
		scanf("%d%d%d",&t,&l,&r);
		if(t){
			vis[l+1]++;
			vis[r+1]--;
		}
		else{
			unnondecrease[count++]=make_pair(l,r);
		}
	} 
	for(int i=1;i<maxs;i++) vis[i]+=vis[i-1];
	for(int i=1;i<maxs;i++) vis[i]=(vis[i]>0?1:0)+vis[i-1];
	
	bool ans=false;
	for(int i=0;i<count;i++){
		pair<int,int> pa=unnondecrease[i];
		if(pa.second-pa.first-(vis[pa.second]-vis[pa.first])==0){
			ans=true;
			break;
		}
	}
	
	if(ans){
		printf("NO\n");
		return;
	}
	printf("YES\n");
	int now=1e9;
	printf("%d",now);
	for(int i=2;i<=n;i++){
		if(vis[i]-vis[i-1]>0) printf(" %d",now);
		else printf(" %d",--now);
	}
	printf("\n");
}


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

E - Two Teams

There are n students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.

The i-th student has integer programming skill ai. All programming skills are distinct and between 1 and n , inclusive.

Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and k closest students to the left of him and k closest students to the right of him (if there are less than k students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).

Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.

Input

The first line of the input contains two integers n and k (1≤k≤n≤2⋅1e5 ) — the number of students and the value determining the range of chosen students during each move, respectively.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤n), where ai is the programming skill of the i-th student. It is guaranteed that all programming skills are distinct.

Output

Print a string of n characters; i-th character should be 1 if i-th student joins the first team, or 2 otherwise.

Examples

Input

5 2
2 4 5 3 1

Output

11111

Input

5 1
2 1 3 5 4

Output

22111

Input

7 1
7 2 1 3 5 4 6

Output

1121122

Input

5 1
2 4 5 3 1

Output

21112

Note

In the first example the first coach chooses the student on a position 3 , and the row becomes empty (all students join the first team).

In the second example the first coach chooses the student on position 4, and the row becomes [2,1] (students with programming skills [3,4,5] join the first team). Then the second coach chooses the student on position 1, and the row becomes empty (and students with programming skills [1,2] join the second team).

In the third example the first coach chooses the student on position 1 , and the row becomes [1,3,5,4,6] (students with programming skills [2,7] join the first team). Then the second coach chooses the student on position 5, and the row becomes [1,3,5] (students with programming skills [4,6] join the second team). Then the first coach chooses the student on position 3, and the row becomes [1] (students with programming skills [3,5] join the first team). And then the second coach chooses the remaining student (and the student with programming skill 1 joins the second team).

In the fourth example the first coach chooses the student on position 3 , and the row becomes [2,1] (students with programming skills [3,4,5] join the first team). Then the second coach chooses the student on position 1, and the row becomes empty (and students with programming skills [1,2] join the second team).

1.题是什么

    先在有n个能力值为1到n的学生排成一行,现在有AB两个教练要轮流在学生中选择队员,A先选,教练一定会优先选当前能力值最高的学生,其左右k个也会顺带的被选入,不足k个时能选多少选多少,被选出来的学生全部出队,然后队伍接在一起,另一个教练继续选,问最终每个学生分别属于哪一队。

2.思路

    使用双向链表维护学生队列,已经被选出的学生从链表中删除,链表接在一起下一个教练继续选。

    特别的由于学生的能力值不重复为1到n,我们可以理解为一个能力数值代表一个学生,在编程时以能力值为键映射想某个学生,比如我代码中的belong数组,belong[2]=1;表示能力值为2的那个学生现在被分到了A教练队中,为0表示还未分配,为2表示在B教练队中。index[2]=5 表示能力值为2的学生在初始队列中的位置为5,这也意味着在链表数组中list[5].data=2.

3.ac代码

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


struct node{
	int data;
	node* left;
	node* right;
};

const int maxn=200005;
int a[maxn];


node list[maxn];
int index[maxn];
int belong[maxn];

node* toleft(node* nownode,int k,int nowcoach){
	if(k==0||nownode==NULL) return nownode;
	if(belong[nownode->data]==0) belong[nownode->data]=nowcoach; 
	return toleft(nownode->left,k-1,nowcoach);
} 

node* toright(node* nownode,int k,int nowcoach){
	if(k==0||nownode==NULL) return nownode;
	if(belong[nownode->data]==0) belong[nownode->data]=nowcoach; 
	return toright(nownode->right,k-1,nowcoach);
} 

void solve(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		index[a[i]]=i;	
	}
	
	for(int i=1;i<=n;i++){
		list[i].data=a[i];
		list[i].left=(i==1?NULL:&list[i-1]);
		list[i].right=(i==n?NULL:&list[i+1]);
	}
	
	int nowmax=n;
	for(int i=0;i<=n;i++) belong[i]=0;
	int nowcoach=1;
	
	while(nowmax){
		belong[nowmax]=nowcoach; 
		node* l=toleft(list[index[nowmax]].left,k,nowcoach);
		node* r=toright(list[index[nowmax]].right,k,nowcoach);
		if(l!=NULL) l->right=r;
		if(r!=NULL) r->left=l;
		while(belong[nowmax]) nowmax--;
		if(nowcoach==1) nowcoach=2;
		else nowcoach=1; 
	}
	
	for(int i=1;i<=n;i++) printf("%d",belong[a[i]]);
	printf("\n");
	
	
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值