CF1263(A,B,C)

A. Sweet Problem

You have three piles of candies: red, green and blue candies:

the first pile contains only red candies and there are r candies in it,
the second pile contains only green candies and there are g candies in it,
the third pile contains only blue candies and there are b candies in it.
Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can’t eat two candies of the same color in a day.

Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.

Input

The first line contains integer t (1≤t≤1000) — the number of test cases in the input. Then t test cases follow.

Each test case is given as a separate line of the input. It contains three integers r, g and b (1≤r,g,b≤108) — the number of red, green and blue candies, respectively.

Output

Print t integers: the i-th printed integer is the answer on the i-th test case in the input.

Example

input

6
1 1 1
1 2 1
4 1 1
7 4 10
8 1 4
8 2 8

output

1
2
2
10
5
9

Note

In the first example, Tanya can eat candies for one day only. She can eat any pair of candies this day because all of them have different colors.

In the second example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and green and blue candies on the second day.

In the third example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and red and blue candies on the second day. Note, that two red candies will remain uneaten.

分析:

题意:给你三种颜色糖果的数量,每天必须吃两个不同颜色的糖果,问最多可以吃多少天。
数学问题。先将三个数从小到大排序。
当a[1]+a[2]<a[3]时,res=a[1]+a[2],即每天吃最少的或第二少的以及最多的糖果。
当a[1]+a[2]>=a[3]时,res=(a[1]+a[2]+a[3])/2,即尽可能少的剩下糖果。

代码:

#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
int a[5];
int cmp(int x,int y)
{
	return x<y;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int i,res=0;
		for(i=1;i<=3;i++)
			cin>>a[i];
		sort(a+1,a+4,cmp);
		if(a[1]+a[2]<a[3])
			res=a[1]+a[2];
		else	
			res=(a[1]+a[2]+a[3])/2;
		cout<<res<<endl;
	}
	return 0;
}

B. PIN Codes

A PIN code is a string that consists of exactly 4 digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0.

Polycarp has n (2≤n≤10) bank cards, the PIN code of the i-th card is pi.

Polycarp has recently read a recommendation that it is better to set different PIN codes on different cards. Thus he wants to change the minimal number of digits in the PIN codes of his cards so that all n codes would become different.

Formally, in one step, Polycarp picks i-th card (1≤i≤n), then in its PIN code pi selects one position (from 1 to 4), and changes the digit in this position to any other. He needs to change the minimum number of digits so that all PIN codes become different.

Polycarp quickly solved this problem. Can you solve it?

Input

The first line contains integer t (1≤t≤100) — the number of test cases in the input. Then test cases follow.

The first line of each of t test sets contains a single integer n (2≤n≤10) — the number of Polycarp’s bank cards. The next n lines contain the PIN codes p1,p2,…,pn — one per line. The length of each of them is 4. All PIN codes consist of digits only.

Output

Print the answers to t test sets. The answer to each set should consist of a n+1 lines

In the first line print k — the least number of changes to make all PIN codes different. In the next n lines output the changed PIN codes in the order corresponding to their appearance in the input. If there are several optimal answers, print any of them.

Example

input

3
2
1234
0600
2
1337
1337
4
3139
3139
3139
3139

output

0
1234
0600
1
1337
1237
3
3139
3138
3939
6139

分析:

题意:给2~10个4位数的纯数字密码,如果里面有相同的两个,就选择其中一个的一位数字改变它,使所有的密码不相同,输出最少改变的次数以及改变后的密码。
暴力循环判断重复与否就可以。
注意改密码的时候不能把密码改成别的密码。所以选定个位置,用一个vis数组判断数字0~9是否在这个位置上出现过。

代码:

#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
char pin[11][10];
int vis[10];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int n,i,j,res=0,k;
		cin>>n;
		for(i=1;i<=n;i++)	
			cin>>pin[i];
		for(i=1;i<=n;i++)
			for(j=i+1;j<=n;j++)
				if(!strcmp(pin[i],pin[j]))
				{
					res++;
					memset(vis,0,sizeof(vis));
					for(k=1;k<=n;k++)
						vis[pin[k][3]-'0']=1;
					for(k=0;k<=9;k++)
						if(vis[k]==0)
						{
							pin[j][3]=k+'0';
							break;
						}		
				}
		cout<<res<<endl;
		for(i=1;i<=n;i++)
			cout<<pin[i]<<endl;
	}
	return 0;
}

C. Everyone is a Winner!

On the well-known testing system MathForces, a draw of n rating units is arranged. The rating will be distributed according to the following algorithm: if k participants take part in this event, then the n rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remain — it is not given to any of the participants.

For example, if n=5 and k=3, then each participant will recieve an 1 rating unit, and also 2 rating units will remain unused. If n=5, and k=6, then none of the participants will increase their rating.

Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.

For example, if n=5, then the answer is equal to the sequence 0,1,2,5. Each of the sequence values (and only them) can be obtained as ⌊n/k⌋ for some positive integer k (where ⌊x⌋ is the value of x rounded down): 0=⌊5/7⌋, 1=⌊5/5⌋, 2=⌊5/2⌋, 5=⌊5/1⌋.

Write a program that, for a given n, finds a sequence of all possible rating increments.

Input

The first line contains integer number t (1≤t≤10) — the number of test cases in the input. Then t test cases follow.

Each line contains an integer n (1≤n≤109) — the total number of the rating units being drawn.

Output

Output the answers for each of t test cases. Each answer should be contained in two lines.

In the first line print a single integer m — the number of different rating increment values that Vasya can get.

In the following line print m integers in ascending order — the values of possible rating increments.

Example

input

4
5
11
1
3

output

4
0 1 2 5
6
0 1 2 3 5 11
2
0 1
3
0 1 3

分析:

题意:给你一个数,求它除以所有正整数可以得到的商(商向下取整)。
可以打个表发现n=109的时候答案不超过106个。
为了节约时间,可以先从1~sqrt(n)循环求答案,再用n除以得到的值。

代码:

#include<bits/stdc++.h>
const int maxn=1e6+10;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
int res[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int n,len=0,i;
		memset(res,0,sizeof(res));
		cin>>n;
		for(i=1;i<=sqrt(n);i++)
			if(n/i!=res[len])
				res[++len]=n/i;
		for(i=len;i>=1;i--)
			if(n/res[i]!=res[i])
				res[++len]=n/res[i];
		res[++len]=0;
		cout<<len<<endl;
		for(i=len;i>=1;i--)
			cout<<res[i]<<" ";
		cout<<endl;
	}
	return 0;
}

记录分数:1464-4=1460。很难受,掉分了。第二题初始化的小问题卡了30分钟,第四题感觉是并查集,但是由于还没怎么学,也不会写。之后再努力!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值