Codeforces Round #550 (Div. 3)

A. Diverse Strings

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.

Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps.

You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".

Input

The first line contains integer nn (1≤n≤1001≤n≤100), denoting the number of strings to process. The following nn lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 11 and 100100, inclusive.

Output

Print nn lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

Example

input

Copy

8
fced
xyz
r
dabcef
az
aa
bad
babc

output

Copy

Yes
Yes
Yes
Yes
No
No
No
No

连续的字符,顺序可以乱,但在字母表上排序后必须是连续的

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200;
char ss[N];
int vis[30];
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		cin>>ss;
		memset(vis,0,sizeof(vis));
		int l=strlen(ss);
		bool f=1;
		int mx=0,mi=110;
		for(int i=0;i<l;i++)
		{
			vis[ss[i]-'a']=1;
			mx=max(mx,ss[i]-'a');
			mi=min(mi,ss[i]-'a');
		}
		for(int i=mi;i<=mx;i++)
		{
			if(vis[i]) continue;
			else 
			{
				f=0;
				break;
			}
		}
		if(f&&mx-mi+1==l)printf("Yes\n");
		else printf("No\n");
	}
}

B. Parity Alternated Deletions

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp has an array aa consisting of nn integers.

He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains n−1n−1 elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.

Formally:

  • If it is the first move, he chooses any element and deletes it;
  • If it is the second or any next move:
    • if the last deleted element was odd, Polycarp chooses any even element and deletes it;
    • if the last deleted element was even, Polycarp chooses any odd element and deletes it.
  • If after some move Polycarp cannot make a move, the game ends.

Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.

Help Polycarp find this value.

Input

The first line of the input contains one integer nn (1≤n≤20001≤n≤2000) — the number of elements of aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1060≤ai≤106), where aiai is the ii-th element of aa.

Output

Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game.

Examples

input

Copy

5
1 5 7 8 2

output

Copy

0

input

Copy

6
5 1 2 4 6 3

output

Copy

0

input

Copy

2
1000000 1000000

output

Copy

1000000

简单的水题

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e3+20;
ll a[N],n,y,b[N],ans;

int main()
{
	cin>>n;
	int ll1=0,ll2=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&y);
		if(y%2==0)	a[++ll1]=y;
		else b[++ll2]=y;
	}
	if(ll1==ll2||abs(ll1-ll2)==1)
	{
		printf("0");
		return 0;
	}
	
	if(ll1<ll2)
	{
		sort(b+1,b+1+ll2);
		ans=0;
		for(int i=1;i<=ll2-ll1-1;i++)
			ans+=b[i];
		printf("%lld\n",ans);
	}
	else 
	{
		sort(a+1,a+1+ll1);
		ans=0;
		for(int i=1;i<=ll1-ll2-1;i++) 
		ans+=a[i];
		printf("%lld\n",ans);
	}
}

C. Two Shuffled Sequences

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing.

Strictly increasing sequence is a sequence of integers [x1<x2<⋯<xk][x1<x2<⋯<xk]. And strictly decreasing sequence is a sequence of integers [y1>y2>⋯>yl][y1>y2>⋯>yl]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

They were merged into one sequence aa. After that sequence aa got shuffled. For example, some of the possible resulting sequences aa for an increasing sequence [1,3,4][1,3,4] and a decreasing sequence [10,4,2][10,4,2] are sequences [1,2,3,4,4,10][1,2,3,4,4,10] or [4,2,1,10,4,3][4,2,1,10,4,3].

This shuffled sequence aa is given in the input.

Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

If there is a contradiction in the input and it is impossible to split the given sequence aa to increasing and decreasing sequences, print "NO".

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output

If there is a contradiction in the input and it is impossible to split the given sequence aa to increasing and decreasing sequences, print "NO" in the first line.

Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

In the second line print nini — the number of elements in the strictly increasing sequence. nini can be zero, in this case the increasing sequence is empty.

In the third line print nini integers inc1,inc2,…,incniinc1,inc2,…,incni in the increasing order of its values (inc1<inc2<⋯<incniinc1<inc2<⋯<incni) — the strictly increasing sequence itself. You can keep this line empty if ni=0ni=0 (or just print the empty line).

In the fourth line print ndnd — the number of elements in the strictly decreasing sequence. ndnd can be zero, in this case the decreasing sequence is empty.

In the fifth line print ndnd integers dec1,dec2,…,decnddec1,dec2,…,decnd in the decreasing order of its values (dec1>dec2>⋯>decnddec1>dec2>⋯>decnd) — the strictly decreasing sequence itself. You can keep this line empty if nd=0nd=0 (or just print the empty line).

ni+ndni+nd should be equal to nn and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).

Examples

input

Copy

7
7 2 7 3 3 1 4

output

Copy

YES
2
3 7 
5
7 4 3 2 1 

input

Copy

5
4 3 1 5 3

output

Copy

YES
1
3 
4
5 4 3 1 

input

Copy

5
1 1 2 1 2

output

Copy

NO

input

Copy

5
0 1 2 3 4

output

Copy

YES
0

5
4 3 2 1 0 

构造一个递增的数,一个递减的数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int a[N],n,v2[N],v1[N],ll1,ll2;
int main()
{
	cin>>n;
	bool flag=1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		v2[a[i]]++;
		if(v2[a[i]]>=3) flag=0;
	}
	if(flag==0) 
	{
		printf("NO\n");
		return 0;
	}
	sort(a+1,a+1+n);
	ll1=0;
	ll2=0;
	for(int i=1;i<=n;i++)
	{
		if(v2[a[i]]==2)
		{
			ll1++;
			v2[a[i]]--;
			v1[i]=1;
		}
	}
	ll2=n-ll1;
	printf("YES\n");
	printf("%d\n",ll1);
	for(int i=1;i<=n;i++)
		if(v1[i]) 
			printf("%d ",a[i]);

	puts("");
	printf("%d\n",ll2);
	for(int i=n;i>=1;i--)
		if(v1[i]==0)
			printf("%d ",a[i]);

} 

D. Equalize Them All

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array aa consisting of nn integers. You can perform the following operations arbitrary number of times (possibly, zero):

  1. Choose a pair of indices (i,j)(i,j) such that |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai+|ai−aj|ai:=ai+|ai−aj|;
  2. Choose a pair of indices (i,j)(i,j) such that |i−j|=1|i−j|=1 (indices ii and jj are adjacent) and set ai:=ai−|ai−aj|ai:=ai−|ai−aj|.

The value |x||x| means the absolute value of xx. For example, |4|=4|4|=4, |−3|=3|−3|=3.

Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.

It is guaranteed that you always can obtain the array of equal elements using such operations.

Note that after each operation each element of the current array should not exceed 10181018 by absolute value.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output

In the first line print one integer kk — the minimum number of operations required to obtain the array of equal elements.

In the next kk lines print operations itself. The pp-th operation should be printed as a triple of integers (tp,ip,jp)(tp,ip,jp), where tptp is either 11 or 22 (11means that you perform the operation of the first type, and 22 means that you perform the operation of the second type), and ipip and jpjp are indices of adjacent elements of the array such that 1≤ip,jp≤n1≤ip,jp≤n, |ip−jp|=1|ip−jp|=1. See the examples for better understanding.

Note that after each operation each element of the current array should not exceed 10181018 by absolute value.

If there are many possible answers, you can print any.

Examples

input

Copy

5
2 4 6 6 6

output

Copy

2
1 2 3 
1 1 2 

input

Copy

3
2 8 10

output

Copy

2
2 2 1 
2 3 2 

input

Copy

4
1 1 1 1

output

Copy

0

只需要将众数求出来,然后在众数的周围进行加减(1,2,操作)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int a[N],n,vis[N];
struct node
{
	int t,i,j;
	node(int t,int i,int j)
	{
		this->t=t;
		this->i=i;
		this->j=j;
	}
};
vector<node>G;
int main()
{
	cin>>n;
	int num=0,mx=0;
	for(int i=1;i<=n;i++) 
	{
		scanf("%d",&a[i]);
		vis[a[i]]++;
		if(vis[a[i]]>num)
		{
			mx=a[i];
			num=vis[a[i]];
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i]==mx)
		{
			int id=i-1;
			while(1<=id&&a[id]!=mx)
			{
				if(a[id]<a[id+1])
				{
					G.push_back(node(1,id,id+1));
					a[id]=mx;
				}
				else
				{
					G.push_back(node(2,id,id+1));
					a[id]=mx;
				}
				id--;
			}
			int i1=i+1;
			while(i1<=n&&a[i1]!=mx)
			{
				if(a[i1-1]<a[i1])
				{
					G.push_back(node(2,i1,i1-1));
					a[i1]=mx;
				}
				else 
				{
					G.push_back(node(1,i1,i1-1));
					a[i1]=mx;
				}
				i1++;
			}
			i=i1-1;
		}
	}
	int siz=G.size();
	printf("%d\n",siz);
	for(int i=0;i<siz;i++)
	{
		printf("%d %d %d\n",G[i].t,G[i].i,G[i].j);
	}
	return 0;
}

F. Graph Without Long Directed Paths

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self-loops or multiple edges in the given graph.

You have to direct its edges in such a way that the obtained directed graph does not contain any paths of length two or greater (where the length of path is denoted as the number of traversed edges).

Input

The first line contains two integer numbers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, n−1≤m≤2⋅105n−1≤m≤2⋅105) — the number of vertices and edges, respectively.

The following mm lines contain edges: edge ii is given as a pair of vertices uiui, vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi). There are no multiple edges in the given graph, i. e. for each pair (ui,viui,vi) there are no other pairs (ui,viui,vi) and (vi,uivi,ui) in the list of edges. It is also guaranteed that the given graph is connected (there is a path between any pair of vertex in the given graph).

Output

If it is impossible to direct edges of the given graph in such a way that the obtained directed graph does not contain paths of length at least two, print "NO" in the first line.

Otherwise print "YES" in the first line, and then print any suitable orientation of edges: a binary string (the string consisting only of '0' and '1') of length mm. The ii-th element of this string should be '0' if the ii-th edge of the graph should be directed from uiui to vivi, and '1' otherwise. Edges are numbered in the order they are given in the input.

Example

input

Copy

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

output

Copy

YES
10100

Note

The picture corresponding to the first example:

And one of possible answers:

染色问题,相邻的结点颜色不能相同就可以了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n,m,u,v;
vector<int>G[N];
int vv[N],l[N],vis[N];
bool flag=1;
void dfs(int id,int val,int pre)
{
	int siz=G[id].size();
	if(!flag) return ;
	if(vv[id]!=-1&&vv[id]==vv[pre])//相邻结点是一样的颜色 
	{
		flag=0;
		return ;
	}
	if(vv[id]==-1)	vv[id]=val;//还未染色 
	if(vis[id]) return ;//之前已经拜访过了,相当于vv[id]!=-1,还可以去掉一个数组 
	vis[id]=1; 
	for(int i=0;i<siz;i++)//树递归遍历的常规操作 
	{
		int v=G[id][i];
		if(v==pre) continue;
		dfs(v,1-val,id);
	}
}
int main()
{
	cin>>n>>m;
	for(int i=0;i<=n;i++) vv[i]=-1; 
	//memset(vv,-1,sizeof(vv));
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
		l[i]=u;
	}
	int root=1;
	dfs(root,1,0);
	if(!flag) 
	{
		printf("NO\n");
		return 0;
	}
	printf("YES\n");
	for(int i=1;i<=m;i++)
	{
		if(vv[l[i]]==1) printf("1");
		else printf("0");
	}
}
/*
7 6
1 2
1 4
1 6
3 2
5 4
6 7
*/

补题:

E. Median String

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two strings ss and tt, both consisting of exactly kk lowercase Latin letters, ss is lexicographically less than tt.

Let's consider list of all strings consisting of exactly kk lowercase Latin letters, lexicographically not less than ss and not greater than tt(including ss and tt) in lexicographical order. For example, for k=2k=2, s=s="az" and t=t="bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"].

Your task is to print the median (the middle element) of this list. For the example above this will be "bc".

It is guaranteed that there is an odd number of strings lexicographically not less than ss and not greater than tt.

Input

The first line of the input contains one integer kk (1≤k≤2⋅1051≤k≤2⋅105) — the length of strings.

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

The third line of the input contains one string tt consisting of exactly kk lowercase Latin letters.

It is guaranteed that ss is lexicographically less than tt.

It is guaranteed that there is an odd number of strings lexicographically not less than ss and not greater than tt.

Output

Print one string consisting exactly of kk lowercase Latin letters — the median (the middle element) of list of strings of length kklexicographically not less than ss and not greater than tt.

Examples

input

Copy

2
az
bf

output

Copy

bc

input

Copy

5
afogk
asdji

output

Copy

alvuw

input

Copy

6
nijfvj
tvqhwp

output

Copy

qoztvz

从后往前构造,s1[i]+s2[i]能被2整除,那就取中间那个值。ans[i]=(s1[i]+s2[i])/2

若不能整除,则需要高一位的答案加上一半的值,+13;再用ans[i+1]/26决定ans[i]是否需要进位

ans[i]=((s1[i]+s2[i])+ans[i+1]/26)/2;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
char s1[N],s2[N],ans[N];
int main()
{
	int n;
	cin>>n;
	cin>>s1+1>>s2+1;
	for(int i=1;i<=n;i++)
	{
		s1[i]=s1[i]-'a';
		s2[i]=s2[i]-'a';
	}
	for(int i=n;i>=1;i--)
	{
		int t=s1[i]+s2[i];
		if(t%2==0) ans[i]=t/2;
		else
		{
			ans[i+1]+=13;
			ans[i]=(t+ans[i+1]/26)/2;
			
			ans[i+1]%=26;
			ans[i]%=26;
		}
	}
	
	for(int i=1;i<=n;i++) printf("%c",ans[i]+'a');

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙大学ccsu_deer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值