Codeforces Round #756 (Div. 3) (AB题解)

A. Make Even

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp has an integer nn that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times:

  • Reverse the prefix of length ll (in other words, ll leftmost digits) of nn. So, the leftmost digit is swapped with the ll-th digit from the left, the second digit from the left swapped with (l−1l−1)-th left, etc. For example, if n=123456789n=123456789 and l=5l=5, then the new value of nn will be 543216789543216789.

Note that for different operations, the values of ll can be different. The number ll can be equal to the length of the number nn — in this case, the whole number nn is reversed.

Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible.

Help Polycarp. Determine the minimum number of operations he needs to perform with the number nn to make it even or determine that this is impossible.

You need to answer tt independent test cases.

Input

The first line contains the number tt (1≤t≤1041≤t≤104) — the number of test cases.

Each of the following tt lines contains one integer nn (1≤n<1091≤n<109). It is guaranteed that the given number doesn't contain the digit 0.

Output

Print tt lines. On each line print one integer — the answer to the corresponding test case. If it is impossible to make an even number, print -1.

Example

input

4
3876
387
4489
3

output

0
2
1
-1

Note

In the first test case, n=3876n=3876, which is already an even number. Polycarp doesn't need to do anything, so the answer is 00.

In the second test case, n=387n=387. Polycarp needs to do 22 operations:

  1. Select l=2l=2 and reverse the prefix 38–––738_7. The number nn becomes 837837. This number is odd.
  2. Select l=3l=3 and reverse the prefix 837––––837_. The number nn becomes 738738. This number is even.

It can be shown that 22 is the minimum possible number of operations that Polycarp needs to do with his number to make it even.

In the third test case, n=4489n=4489. Polycarp can reverse the whole number (choose a prefix of length l=4l=4). It will become 98449844 and this is an even number.

In the fourth test case, n=3n=3. No matter how hard Polycarp tried, he would not be able to make an even number.

题目链接A

题目大意:

给你一个数 每次操作只能对位置1-位置n(就是拿开头换任何一个位置的数)

求最少操作次数,得到偶数

解题思路:

他操作的结果是得到一个偶数

那么就有以下几种情况

1.本身就是偶数,不需要移动(0)

2.序列里没有偶数(-1)

3.开头就是偶数,直接移动至末尾就是偶数了(1)

4.剩下的都是(2), 这个情况是序列里有偶数,开头是奇数

AC code:

#include<bits/stdc++.h>

using namespace std;

int main()
{
	int t;
	while (cin >> t)
	{
		while (t --)
		{
			char a[100];
			cin >> a;
			int l = strlen(a);
			int f=0;
			//此段判断序列中是否存在偶数 
			for(int i = 0;i < l; i++) 
				if((a[i]-'0')%2==0) f= 1;	
			if(!f)
			{
				cout << -1 << endl;
				continue;
			}
			//判断末位是否为偶数 
			if((a[l-1]-'0')%2==0)
			{
				cout << 0 << endl;
				continue;
			}
			//判断开头是否为偶数 
			if((a[0]-'0')%2==0)
			{
				cout << 1 << endl;
				continue;
			}
			//其他情况都不是就是中间存在偶数,开头不是奇数,结尾不是偶数 
			cout << 2 << endl;	
		}
	}
	
	return 0;
}

 

B. Team Composition: Programmers and Mathematicians

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The All-Berland Team Programming Contest will take place very soon. This year, teams of four are allowed to participate.

There are aa programmers and bb mathematicians at Berland State University. How many maximum teams can be made if:

  • each team must consist of exactly 44 students,
  • teams of 44 mathematicians or 44 programmers are unlikely to perform well, so the decision was made not to compose such teams.

Thus, each team must have at least one programmer and at least one mathematician.

Print the required maximum number of teams. Each person can be a member of no more than one team.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases.

This is followed by descriptions of tt sets, one per line. Each set is given by two integers aa and bb (0≤a,b≤1090≤a,b≤109).

Output

Print tt lines. Each line must contain the answer to the corresponding set of input data — the required maximum number of teams.

Example

input

6
5 5
10 1
2 3
0 0
17 2
1000000000 1000000000

output

2
1
1
0
2
500000000

题目链接B

题目大意:

程序员科学家组队,每队4人,不能全是程序员或全是科学家。

求最大队伍数

解题思路:

直接a,b中较小的和(a+b)/4取最小

#include <iostream>
#include <algorithm>
 
using namespace std;
 
int main ()
{
	int t, a, b;
	while(cin >> t)
	{
		while (t--)
		{
			cin >> a >> b;
			int tmp;
			tmp = min(min(a,b),(a+b)/4);
			cout << tmp << endl;		
		}
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值