Educational Codeforces Round 78 (Rated for Div. 2)

不禁留下图论水平不行的泪水,看着自己的名次从500降到700,第一次div2做的这么稳呢,感觉ABC三个题都挺好的。  F是一个数论题,感觉不是很难,但我没读懂套例子也没套过,今晚先到这里吧,静等hack,然后DF必补!!!!!

A - Shuffle Hashing

给你一个操作让字符串h分为s1+p(可变换)+s2 输入p和h问可否分解

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e6+19;
ll n;
int a[maxx]={0};
map<int,int>mp;
int main()
{
	ll t,i,j,k,m;
	ll x,y;	
	
	cin>>t;
	while(t--)
	{
		mp.clear();
		cin>>n;
		for(i=1;i<=2*n;i++) scanf("%d",&a[i]);
		x=0;y=0;
			mp[0]=2*n+1;
		for(i=2*n;i>=n+1;i--)
		{
			if(a[i]==1) x++;
			else y++;
			mp[x-y]=i;
		}
	
		x=0;y=0;
		for(i=1;i<=n;i++)
		{
			if(a[i]==1) x++;
			else y++;
		}
		ll maxl=10000000;
		for(i=n;i>=0;i--)
		{
			int o=y-x;
			if(mp[o])
			{
				maxl=min(maxl,n-i+mp[o]-n-1);
			}
			if(a[i]==1) x--;
			else y--;
		}
		printf("%lld\n",maxl);
	}
	
	
	return 0;
}

 

B - A and B

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two integers aa and bb. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by 11; during the second operation you choose one of these numbers and increase it by 22, and so on. You choose the number of these operations yourself.

For example, if a=1a=1 and b=3b=3, you can perform the following sequence of three operations:

  1. add 11 to aa, then a=2a=2 and b=3b=3;
  2. add 22 to bb, then a=2a=2 and b=5b=5;
  3. add 33 to aa, then a=5a=5 and b=5b=5.

Calculate the minimum number of operations required to make aa and bb equal.

Input

The first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The only line of each test case contains two integers aa and bb (1≤a,b≤1091≤a,b≤109).

Output

For each test case print one integer — the minimum numbers of operations required to make aa and bb equal.

Example

input

Copy

3
1 3
11 11
30 20

output

Copy

3
0
4

Note

First test case considered in the statement.

In the second test case integers aa and bb are already equal, so you don't need to perform any operations.

In the third test case you have to apply the first, the second, the third and the fourth operation to bb (bb turns into 20+1+2+3+4=3020+1+2+3+4=30).

给你x,y,每次使一个数+1+2+3+4.......问最多多少次加到x=by 解一个方程a+b=前n项和a-b=x和y的差值,注意判断边界,也是个不错的小思维题。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e5+19;
ll n;
char p[2000],h[2000];
int biaoji[300];
int mp[300];
int main()
{
	ll t,i,j,k,m;
	ll a,b,x,y;	
	
	cin>>t;
	while(t--)
	{
		cin>>a>>b;
		x=abs(a-b);
		if(a==b) {
			printf("0\n");
			continue;
		}
		for(i=1;i;i++)
		{
			k=(i*(i+1))/2;
			if((k+x)%2==0&&(k+x)/2>=x)
			{
				printf("%lld\n",i);
				break;
			}
		}
	}
	
	
	return 0;
}

 

C - Berry Jam

 

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were 2n2n jars of strawberry and blueberry jam.

All the 2n2n jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly nn jars to his left and nn jars to his right.

For example, the basement might look like this:

 

Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right.

Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same.

For example, this might be the result:

He has eaten 11 jar to his left and then 55 jars to his right. There remained exactly 33 full jars of both strawberry and blueberry jam.

Jars are numbered from 11 to 2n2n from left to right, so Karlsson initially stands between jars nn and n+1n+1.

What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left?

Your program should answer tt independent test cases.

Input

The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105).

The second line of each test case contains 2n2n integers a1,a2,…,a2na1,a2,…,a2n (1≤ai≤21≤ai≤2) — ai=1ai=1 means that the ii-th jar from the left is a strawberry jam jar and ai=2ai=2 means that it is a blueberry jam jar.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case print the answer to it — the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left.

Example

input

Copy

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

output

Copy

6
0
6
2

Note

The picture from the statement describes the first test case.

In the second test case the number of strawberry and blueberry jam jars is already equal.

In the third test case Karlsson is required to eat all 66 jars so that there remain 00 jars of both jams.

In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave 11 jar of both jams.

题意:给你2*n桶两种不同的东西,小人在n和n+1之间,每次小人移走左边或右边的一个东西,问至少几次使得两种东西一样多,同样是个不错的思维题啊!首先你可以记录前缀和,后面的从后往前记录后缀和,然后如果两个位置可以的条件就是第一个第一种+第二个的第一种等于两个的第二种,但这样时间复杂度就不行了,所以我们记录右边的后缀和x-y(x是第一种的数量,y是第二种的数量)此点记录map值中,如果存在两个x-y相等的位置,让map记录前面的位置即可,因为前面所需的代价要小,然后记录前n个的前缀,最后后从n到1扫一遍记录最小值即可,还是注意边界处和特判

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e6+19;
ll n;
int a[maxx]={0};
map<int,int>mp;
int main()
{
	ll t,i,j,k,m;
	ll x,y;	
	
	cin>>t;
	while(t--)
	{
		mp.clear();
		cin>>n;
		for(i=1;i<=2*n;i++) scanf("%d",&a[i]);
		x=0;y=0;
			mp[0]=2*n+1;
		for(i=2*n;i>=n+1;i--)
		{
			if(a[i]==1) x++;
			else y++;
			mp[x-y]=i;
		}
	
		x=0;y=0;
		for(i=1;i<=n;i++)
		{
			if(a[i]==1) x++;
			else y++;
		}
		ll maxl=10000000;
		for(i=n;i>=0;i--)
		{
			int o=y-x;
			if(mp[o])
			{
				maxl=min(maxl,n-i+mp[o]-n-1);
			}
			if(a[i]==1) x--;
			else y--;
		}
		printf("%lld\n",maxl);
	}
	
	
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值