Codeforces Round #496 (Div. 3) 解题报告

http://codeforces.com/contest/1005

好久没有刷题了···今天做了这一套div3找找手感。

做题还是不能断啊,哎。

A. Tanya and Stairways

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from 11 to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains 33steps, and the second contains 44 steps, she will pronounce the numbers 1,2,3,1,2,3,41,2,3,1,2,3,4.

You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.

The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.

Input

The first line contains nn (1≤n≤10001≤n≤1000) — the total number of numbers pronounced by Tanya.

The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with xx steps, she will pronounce the numbers 1,2,…,x1,2,…,x in that order.

The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.

Output

In the first line, output tt — the number of stairways that Tanya climbed. In the second line, output tt numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.

Examples

input

7
1 2 3 1 2 3 4

output

2
3 4 

input

4
1 1 1 1

output

4
1 1 1 1 

input

5
1 2 3 4 5

output

1
5 

input

5
1 2 1 2 1

output

3
2 2 1 

水题,注意一下都是1的情况和末尾的处理就好。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1234;
int n,m,a[maxn],ans[maxn];
int main()
{
	int i,j;
	while(cin>>n)
	{
		m=0;
		memset(ans,0,sizeof(ans));
		for(i=1;i<=n;i++)
		cin>>a[i];
		for(i=1;i<=n;i++)
		{
			if(a[i]==1&&i!=1)//不是数组第一个位置的数字1,前面的数一定是前一个的台阶数 
			{
				ans[m++]=a[i-1];
			}
		}
		ans[m++]=a[n];//最后直接把末尾的数加进来 
		cout<<m<<endl;
		for(i=0;i<m-1;i++)
		cout<<ans[i]<<' ';
		cout<<ans[i]<<endl;
	}
	return 0;
} 

B. Delete from the Left

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two strings ss and tt. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by 11. You can't choose a string if it is empty.

For example:

  • by applying a move to the string "where", the result is the string "here",
  • by applying a move to the string "a", the result is an empty string "".

You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.

Write a program that finds the minimum number of moves to make two given strings ss and tt equal.

Input

The first line of the input contains ss. In the second line of the input contains tt. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and 2⋅1052⋅105, inclusive.

Output

Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.

Examples

input

test
west

output

2

input

codeforces
yes

output

9

input

test
yes

output

7

input

b
ab

output

1

Note

In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est".

In the second example, the move should be applied to the string "codeforces" 88 times. As a result, the string becomes "codeforces" →→ "es". The move should be applied to the string "yes" once. The result is the same string "yes" →→ "es".

In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.

In the fourth example, the first character of the second string should be deleted.

题意:给你两个字符串,每次操作可以把一个串的第一个字符拿走,求最少需要几步可以让两个串相等(最坏的情况就是两个串都拿成空串就相等了)。

思路:就是从末尾开始看两个串有几个字符相等,一直匹配到第一个不相等的字符,再用两个串的总长度减去匹配数就是答案了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
string s1,s2;
long long ans,cnt,n,m;
int main()
{
	s1.clear();
	s2.clear();
	int i,j;
	while(cin>>s1>>s2)
	{
		cnt=ans=0;
		n=s1.size();
		m=s2.size();
		for(i=0;i<min(n,m);i++)
		{
			if(s1[n-1-i]==s2[m-1-i])//从末尾开始匹配 
			{
				cnt++;//记录匹配个数 
			}
			else
			break;//遇到第一个不匹配的就退出循环 
		}
		ans=n+m-2*cnt;
		cout<<ans<<endl;
		s1.clear();	
		s2.clear();
	}
	return 0;
}

C. Summarize to the Power of Two

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A sequence a1,a2,…,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (i≠ji≠j) such that ai+ajai+aj is a power of two (that is, 2d2d for some non-negative integer dd).

For example, the following sequences are good:

  • [5,3,11][5,3,11] (for example, for a1=5a1=5 we can choose a2=3a2=3. Note that their sum is a power of two. Similarly, such an element can be found for a2a2 and a3a3),
  • [1,1,1,1023][1,1,1,1023],
  • [7,39,89,25,89][7,39,89,25,89],
  • [][].

Note that, by definition, an empty sequence (with a length of 00) is good.

For example, the following sequences are not good:

  • [16][16] (for a1=16a1=16, it is impossible to find another element ajaj such that their sum is a power of two),
  • [4,16][4,16] (for a1=4a1=4, it is impossible to find another element ajaj such that their sum is a power of two),
  • [1,3,2,8,8,8][1,3,2,8,8,8] (for a3=2a3=2, it is impossible to find another element ajaj such that their sum is a power of two).

You are given a sequence a1,a2,…,ana1,a2,…,an. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.

Input

The first line contains the integer nn (1≤n≤1200001≤n≤120000) — the length of the given sequence.

The second line contains the sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

Output

Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all nn elements, make it empty, and thus get a good sequence.

Examples

input

6
4 7 1 5 4 9

output

1

input

5
1 2 3 4 5

output

2

input

1
16

output

1

input

4
1 1 1 1023

output

0

Note

In the first example, it is enough to delete one element a4=5a4=5. The remaining elements form the sequence [4,7,1,4,9][4,7,1,4,9], which is good.

题意:给你一段整数序列,问最少需要移除几个数可以让这个序列成为一个“好序列”。一个“好序列”的定义是,这个序列中的每一个数都能找到除了它自身之外的一个数,它俩相加之后是2的幂次方。

思路:这个题给的数据1e5,我想到了n*logn的做法。我用了一个power数组存的2的n次方。每次用power[j]和a[i]作差后的值通过二分在a中寻找有没有相同的,找到了给vis[i]赋1,代表这个数不用被删除。最后统计vis里面有几个0就是答案。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=123456;
int m,n,a[maxn],vis[maxn];
long long power[100];
void solve()
{
	int ans,temp,i,j;
	ans=0;
	sort(a,a+n);//对a排序,之后二分 
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			temp=power[j]-a[i];
			if(temp>a[n-1]||temp<0)//如果temp太大或太小就跳过 
			continue;
			else
			{
				if(*lower_bound(a,a+n,temp)==temp||*upper_bound(a,a+n,temp)==temp)//二分去a数组里找作差后的数 
				{
					if(temp!=a[i])
					{
						vis[i]=1;
						break;
					}
					else if((i!=0&&a[i-1]==temp)||(i!=(n-1)&&a[i+1]==temp))//这里判断不能是原来的数本身,
											//因为是有序数组,所以a[i]相邻的数有和它一样的就可以保留这个数,比如4 4                      
					{
						vis[i]=1;
						break;
					}
				}
			}
		}
	}
	for(i=0;i<n;i++)
	{
		if(vis[i]==0)
		ans++;
	}
	cout<<ans<<endl;
}
int main()
{
	int i,j;
	power[0]=1;
	m=1;
	for(i=1;power[i-1]<1e11;i++)
	{
		power[i]=power[i-1]*2;
		m++;
	}
	while(cin>>n)
	{
		memset(vis,0,sizeof(vis));
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		solve();
	}
	return 0;
}

D. Polycarp and Div 3

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp likes numbers that are divisible by 3.

He has a huge number ss. Polycarp wants to cut from it the maximum number of numbers that are divisible by 33. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after mm such cuts, there will be m+1m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 33.

For example, if the original number is s=3121s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|213|1|21. As a result, he will get two numbers that are divisible by 33.

Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.

What is the maximum number of numbers divisible by 33 that Polycarp can obtain?

Input

The first line of the input contains a positive integer ss. The number of digits of the number ss is between 11 and 2⋅1052⋅105, inclusive. The first (leftmost) digit is not equal to 0.

Output

Print the maximum number of numbers divisible by 33 that Polycarp can get by making vertical cuts in the given number ss.

Examples

input

3121

output

2

input

6

output

1

input

1000000000000000000000000000000000

output

33

input

201920181

output

4

Note

In the first example, an example set of optimal cuts on the number is 3|1|21.

In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 33.

In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 3333 digits 0. Each of the 3333digits 0 forms a number that is divisible by 33.

In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 00, 99, 201201 and 8181 are divisible by 33.

题意:给你一个长度在1e5数量级的整数,你可以把它们从中间切开,要求剩下的部分是3的倍数的字串长度最多能多少个?不能包含前导0,但是单独的0也算3的倍数。

思路:一个数可以整除3,那么这个数的各个位数相加是3的倍数。所以我们用一个变量cnt从头加到尾,用长度为3的ans数组存放cnt当前余3的值,这里参考抽屉原理。如果cnt余3后ans[cnt%3]!=0那么就代表我们刚才遍历的子串是3的倍数。此时把ans变为初始值。因为cnt一开始为0,所以ans初始值应为{1,0,0}。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=123456;
string s;
int ans[3],a;
void solve()
{
	int i,j,n;
	long long cnt=0;
	n=s.size();
	for(i=0;i<n;i++)
	{
		cnt+=(s[i]-'0');
		cnt%=3;
		if(ans[cnt])
		{
			ans[1]=ans[2]=0;
			cnt=0;
			a++;
		}
		else
		{
			ans[cnt]=1;
		}
	}
	cout<<a<<endl;
}
int main()
{
	while(cin>>s)
	{
		memset(ans,0,sizeof(ans));
		ans[0]=1;
		a=0;
		solve();
		s.clear();
	}
	return 0;
}

E1. Median on Segments (Permutations Edition)

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a permutation p1,p2,…,pnp1,p2,…,pn. A permutation of length nn is a sequence such that each integer between 11 and nn occurs exactly once in the sequence.

Find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of the median of pl,pl+1,…,prpl,pl+1,…,pr is exactly the given number mm.

The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.

For example, if a=[4,2,7,5]a=[4,2,7,5] then its median is 44 since after sorting the sequence, it will look like [2,4,5,7][2,4,5,7] and the left of two middle elements is equal to 44. The median of [7,1,2,9,6][7,1,2,9,6] equals 66 since after sorting, the value 66 will be in the middle of the sequence.

Write a program to find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of the median of pl,pl+1,…,prpl,pl+1,…,pr is exactly the given number mm.

Input

The first line contains integers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 1≤m≤n1≤m≤n) — the length of the given sequence and the required value of the median.

The second line contains a permutation p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). Each integer between 11 and nn occurs in pp exactly once.

Output

Print the required number.

Examples

input

Copy

5 4
2 4 5 3 1

output

Copy

4

input

Copy

5 5
1 2 3 4 5

output

Copy

1

input

Copy

15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9

output

Copy

48

Note

In the first example, the suitable pairs of indices are: (1,3)(1,3), (2,2)(2,2), (2,3)(2,3) and (2,4)(2,4).

题意:给你一个长度为n的序列和一个数m,长度为n的序列里包含从1-n的各个数。问以m为中位数的子段有几个。即取出一个子段从小到大排序后m是中位数,如果元素为偶数个,m则是中间左边的数。

思路:参考了别人的想法,直接上代码吧:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 5e5+10;
int a[maxn*2],vis[maxn*2];
int main()
{
    int n,m;
    int pos = 0;
    scanf("%d%d",&n,&m);
    for(int i = 0 ; i < n ;i ++)
    {
        scanf("%d",&a[i]);
        if(a[i] == m)
        {
            pos = i;
        }
    }
    int cnt = 0;
    for(int i = pos ; i < n ; i++)
    {
        if(a[i] > m) cnt++;
        if(a[i] < m) cnt--;
        vis[maxn + cnt] ++; // 当前指的 maxn + cnt 指的是,比m大的值和比m小的值的差,通俗的讲就是我现在还差 -cnt 个可以凑到0 
    }
    cnt = 0;
    long long ans = 0;
    for(int i = pos ; i >= 0 ; i--)
    {
        if(a[i] < m) cnt ++;//这里--和++反过来了,目的是去匹配右边 
        if(a[i] > m) cnt --;
        ans += vis[maxn + cnt]; // 这个就是 我现在已经凑到 cnt个了,那么他的值就是0,于是就有vis[cnt]个区间了
		//注意一开始我们加了vis[maxn+0] 
        ans += vis[maxn + cnt + 1]; // 仔细想想就是当区间长度是偶数的时候 我们取他的左边那位 
    }
    cout<<ans<<endl;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值