Codeforces Round #661 (Div. 3) B.C

今天补了一下cf题,发现昨天有一场比赛结果忘记参加了(懊悔),于是今天除了做了以前的比赛之外,还补了一下昨晚的题目。

B. Gifts Fixing

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have nn gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The ii-th gift consists of aiai candies and bibi oranges.

During one move, you can choose some gift 1≤i≤n1≤i≤n and do one of the following operations:

  • eat exactly one candy from this gift (decrease aiai by one);
  • eat exactly one orange from this gift (decrease bibi by one);
  • eat exactly one candy and exactly one orange from this gift (decrease both aiai and bibi by one).

Of course, you can not eat a candy or orange if it's not present in the gift (so neither aiai nor bibi can become less than zero).

As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a1=a2=⋯=ana1=a2=⋯=an and b1=b2=⋯=bnb1=b2=⋯=bn (and aiai equals bibi is not necessary).

Your task is to find the minimum number of moves required to equalize all the given gifts.

You have to answer tt independent test cases.

Input

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

The first line of the test case contains one integer nn (1≤n≤501≤n≤50) — the number of gifts. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the number of candies in the ii-th gift. The third line of the test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1091≤bi≤109), where bibi is the number of oranges in the ii-th gift.

Output

For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.

Example

input

Copy

5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4

output

Copy

6
16
0
4999999995
7

题目总结:本来这道题挺水的,不应该整理,但补的时候数据范围又设错了,标记一下做教训。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long a[1005],b[1005];
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		long long t,mina,minb;
		cin>>t;
	  for(int i=1;i<=t;i++)
	  {
		cin>>a[i]; 
	  }
	  mina=a[1];
	    for(int i=2;i<=t;i++)
	  {
			if(a[i]>=mina)
			{
				mina=mina;
			}
			else
			{
				mina=a[i];
		   }
	  }
	  for(int i=1;i<=t;i++)
	  {
	  	cin>>b[i];
	  }
	  minb=b[1];
	    for(int i=2;i<=t;i++)
	  {
			if(b[i]>=minb)
			{
				minb=minb;
			}
			else
			{
				minb=b[i];
			}
	  }
	  long long sum=0;
	  for(int i=1;i<=t;i++)
	  {
	  	sum=sum+max(a[i]-mina,b[i]-minb);
	  }
	  cout<<sum<<endl;
}
return 0;
}

C. Boats Competition

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn people who want to participate in a boat competition. The weight of the ii-th participant is wiwi. Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight.

So, if there are kk teams (a1,b1)(a1,b1), (a2,b2)(a2,b2), ……, (ak,bk)(ak,bk), where aiai is the weight of the first participant of the ii-th team and bibi is the weight of the second participant of the ii-th team, then the condition a1+b1=a2+b2=⋯=ak+bk=sa1+b1=a2+b2=⋯=ak+bk=s, where ss is the total weight of each team, should be satisfied.

Your task is to choose such ss that the number of teams people can create is the maximum possible. Note that each participant can be in no more than one team.

You have to answer tt independent test cases.

Input

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

The first line of the test case contains one integer nn (1≤n≤501≤n≤50) — the number of participants. The second line of the test case contains nn integers w1,w2,…,wnw1,w2,…,wn (1≤wi≤n1≤wi≤n), where wiwi is the weight of the ii-th participant.

Output

For each test case, print one integer kk: the maximum number of teams people can compose with the total weight ss, if you choose ss optimally.

Example

input

Copy

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

output

Copy

2
3
4
1
2

题意:给n个人,两个人一个小船,要求每个小船上的人体重都相同,问最多多少小船。

题目思路:能暴力绝对不用别的方法,本来只是试下暴力能不能过,结果直接卡在test2上了,重写了好几遍才过,结果答案就是原来用的暴力思路,从 2到2*n枚举,然后每次保留最大的队伍数,还是写代码不大熟练。

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[55],c[55],b[55];
int main(){
	int t,n,i,sum,s,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
		   c[i]=0;
	    }
		for(i=1;i<=n;i++)
		{
		  scanf("%d",&a[i]);
		  c[a[i]]++;
	    }
		int w=0;
		for(s=n*2;s>=1;s--)
		{
			sum=0;
			for(j=1;j<=50;j++)
			{
			 b[j]=c[j];
		    }
			for(j=1;j<=n;j++)
			{
			  if(0<s-a[j]&&s-a[j]<=n&&b[s-a[j]]>0)
			{
				b[a[j]]--,b[s-a[j]]--;
				if(b[a[j]]>=0&&b[s-a[j]]>=0)
				{
				sum++;
				}
			}
		   }
			w=max(w,sum);
		}
		printf("%d\n",w);
	}
	return 0;
}
 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值