寒假养成计划——Day7

        昨天做的题太少了,和今天一起发。临近春节,本人稍微的放松几天,年后再继续做题!

A题

题目链接

Did you know you can download more RAM? There is a shop with nn different pieces of software that increase your RAM. The i-th RAM increasing software takes ai GB of memory to run (temporarily, once the program is done running, you get the RAM back), and gives you an additional bi GB of RAM (permanently). Each software can only be used once. Your PC currently has k GB of RAM.

Note that you can't use a RAM-increasing software if it takes more GB of RAM to use than what you currently have.

Since RAM is the most important thing in the world, you wonder, what is the maximum possible amount of RAM achievable?

Input

The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of test cases follows.

The first line of each test case contains the integers n and k (1≤n≤100, 1≤k≤1000). Then two lines follow, each containing n integers describing the arrays a and b (1≤ai,bi≤1000).

Output

For each test case, output a single line containing the largest amount of RAM you can achieve.

Example

input

4
3 10
20 30 10
9 100 10
5 1
1 1 5 1 1
1 1 1 1 1
5 1
2 2 2 2 2
100 100 100 100 100
5 8
128 64 32 16 8
128 64 32 16 8

output

29
6
1
256

Note

In the first test case, you only have enough RAM to run the third software initially, but that increases your RAM to 20 GB, which allows you to use the first software, increasing your RAM to 29 GB. The only software left needs 30 GB of RAM, so you have to stop here.

In the second test case, you can use the first, second, fourth and fifth software that need only 1 GB of RAM per software to run to increase your RAM to 5 GB, and then use the last remaining one to increase your RAM to 6 GB.

In the third test case, all the software need more than 1 GB of RAM to run, so the amount of RAM you have stays at 1 GB.


题解:

        这道题比较的简单,大意就是说RAM可以扩容,问最多可以扩容到多少。首先要明确一点,a数组是需要多少内存才可以扩容,所以要从最小的开始扩,每次扩容完都要判断是否还能继续扩容,最后输出结果即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;

const int N=105;
struct R
{
	int a,b;
}ram[N];
bool cmp(R a,R b)
{
	return a.a<b.a;
}

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n,k;
		scanf("%d%d",&n,&k);
		for (int i=0;i<n;i++)
			scanf("%d",&ram[i].a);
		for (int i=0;i<n;i++)
			scanf("%d",&ram[i].b);
		sort(ram,ram+n,cmp);
		int ans=k;
		for (int i=0;i<n;i++)
		{
			if (ans>=ram[i].a)
				ans+=ram[i].b;
			else
				break;
		}
		printf("%d\n",ans);
	}
	return 0;
}

A题

题目链接

You are given two arrays a and b of n positive integers each. You can apply the following operation to them any number of times:

  • Select an index i (1≤i≤n) and swap ai with bi (i. e. ai becomes bi and vice versa).

Find the minimum possible value of max(a1,a2,…,an)⋅max(b1,b2,…,bn) you can get after applying such operation any number of times (possibly zero).

Input

The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤100) — the length of the arrays.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤10000) where ai is the i-th element of the array a.

The third line of each test case contains n integers b1,b2,…,bn (1≤bi≤10000) where bi is the i-th element of the array b.

Output

For each test case, print a single integer, the minimum possible value of max(a1,a2,…,an)⋅max(b1,b2,…,bn) you can get after applying such operation any number of times.

Example

input

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

output

18
9
2

Note

In the first test, you can apply the operations at indices 2 and 6, then a=[1,4,6,5,1,5] and b=[3,2,3,2,2,2], max(1,4,6,5,1,5)⋅max(3,2,3,2,2,2)=6⋅3=18.

In the second test, no matter how you apply the operations, a=[3,3,3] and b=[3,3,3] will always hold, so the answer is max(3,3,3)⋅max(3,3,3)=3⋅3=9.

In the third test, you can apply the operation at index 1, then a=[2,2], b=[1,1], so the answer is max(2,2)⋅max(1,1)=2⋅1=2.


题解:

        这道题目比较的简单,实际上只需要判断两个数组的最大值在哪个数组里,然后把大的值全部放在有最大值的数组中,小的放在另一个数组里,最后取出两个数组的最大值相乘就能得到结果。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int a[105],b[105];

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n;
		scanf("%d",&n);
		int max1=0,max2=0;
		for (int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			max1=max(max1,a[i]);
		}	
		for (int i=0;i<n;i++)
		{
			scanf("%d",&b[i]);
			max2=max(max2,b[i]);
		}
		bool f=(max1>=max2?1:0);
		int t=0;
		for (int i=0;i<n;i++)
		{
			if (f)
			{
				if (a[i]<b[i])
				{
					t=a[i];
					a[i]=b[i];
					b[i]=t;
				}
			}
			else
			{
				if (a[i]>b[i])
				{
					t=a[i];
					a[i]=b[i];
					b[i]=t;
				}
			}
		}
		printf("%lld\n",(*max_element(a,a+n))*(*max_element(b,b+n)));
	}
	return 0;
}

B题

题目链接

You are given an array a of n elements. You can apply the following operation to it any number of times:

  • Select some subarray from aa of even size 2k that begins at position l (1≤l≤l+2⋅k−1≤n, k≥1) and for each i between 0 and k−1 (inclusive), assign the value a_{l+k+i} to a_{l+i}.

For example, if a=[2,1,3,4,5,3], then choose l=1 and k=2, applying this operation the array will become a=[3,4,3,4,5,3].

Find the minimum number of operations (possibly zero) needed to make all the elements of the array equal.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤2⋅10^{4}) — the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤2⋅10^{5}) — the length of the array.

The second line of each test case consists of nn integers a1,a2,…,an (1≤ai≤n) — the elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^{5}.

Output

Print t lines, each line containing the answer to the corresponding test case — the minimum number of operations needed to make equal all the elements of the array with the given operation.

Example

input

5
3
1 1 1
2
2 1
5
4 4 4 2 4
4
4 2 1 3
1
1

output

0
1
1
2
0

Note

In the first test, all elements are equal, therefore no operations are needed.

In the second test, you can apply one operation with k=1 and l=1, set a1:=a2, and the array becomes [1,1] with 1 operation.

In the third test, you can apply one operation with k=1 and l=4, set a4:=a5, and the array becomes [4,4,4,4,4].

In the fourth test, you can apply one operation with k=1 and l=3, set a3:=a4, and the array becomes [4,2,3,3], then you can apply another operation with k=2 and l=1, set a1:=a3, a2:=a4, and the array becomes [3,3,3,3].

In the fifth test, there is only one element, therefore no operations are needed.


题解:

        这道题看着挺唬人的,实际上比较的简单。首先通过上面的描述,我们知道最后数组一定都会变成最后一个元素的值,具体为啥就不证了,比较简单,稍微画画就知道。然后就是那个比较迷的 l 和 k ,这个实际上就是个翻倍的过程。我们把数组倒着存,相当与从第二个元素开始,如果它和第一个元素不一样,则这个指针往后移动 2*i-1 位,否则 i++ 。最后输出总共的次数就行。(如果不能理解的话看下面的这张图)

AC代码:

#include <bits/stdc++.h>
using namespace std;

const int N=2e5+5;
int a[N];

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n;
		scanf("%d",&n);
		for (int i=n;i>=1;i--)
			scanf("%d",&a[i]);
		int ans=0;
		for (int i=2;i<=n;)
		{
			if (a[i]!=a[1])
			{
				ans++;
				i=2*i-1;
			}
			else
				i++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

C题

题目链接

You are given a set of n (n is always a power of 2) elements containing all integers 0,1,2,…,n−1 exactly once.

Find \frac{n}{2} pairs of elements such that:

  • Each element in the set is in exactly one pair.
  • The sum over all pairs of the bitwise AND of its elements must be exactly equal to k. Formally, if aiai and bibi are the elements of the ii-th pair, then the following must hold:

    \sum_{i=1}^{\frac{n}{2}} ai & bi, where & denotes the bitwise AND operation.

If there are many solutions, print any of them, if there is no solution, print −1 instead.

Input

The input consists of multiple test cases. The first line contains a single integer t (1≤t≤400) — the number of test cases. Description of the test cases follows.

Each test case consists of a single line with two integers n and k (4≤n≤2^{16}, n is a power of 2, 0≤k≤n−1).

The sum of n over all test cases does not exceed 2^{16}. All test cases in each individual input will be pairwise different. 

Output

For each test case, if there is no solution, print a single line with the integer −1.

Otherwise, print \frac{n}{2} lines, the i-th of them must contain ai and bi, the elements in the i-th pair.

If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.

Example

input

Copy

4
4 0
4 1
4 2
4 3

output

Copy

0 3
1 2
0 2
1 3
0 1
2 3
-1

Note

In the first test, (0&3)+(1&2)=0.

In the second test, (0&2)+(1&3)=1.

In the third test, (0&1)+(2&3)=2.

In the fourth test, there is no solution.


题解:

        这道题目虽然作为C题,但还是比较容易上手的。先要明确两个事儿:

  • 当n=4,k=3时无解,其他时候均有解
  • 按位与运算,任何数x与2^{i}-1的结果与x一样。

        初始的配对结果是<0, n-1><1, n-2>……配对的时候,如果k≠n-1,则需要交换k和0的位置,使得a[0]\leftrightarrowa[k];否则,要交换a[0]与a[n-2]、a[1]与a[n-4],为的是能够凑出n-2和1,因为1&3=1。最后再输出所有的配对结果就行。

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6;
int a[N];
int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		ll n,k;
		scanf("%lld%lld",&n,&k);
		for (int i=0;i<n;i++)
			a[i]=i;
		if (k==3&&n==4)
			puts("-1");
		else
		{
			if (k!=n-1)
				swap(a[k],a[0]);
			else
			{
				swap(a[0],a[n-2]);
				swap(a[1],a[n-4]);
			}
			for (int i=0;i<n/2;i++)
				cout<<a[i]<<" "<<a[n-i-1]<<endl;
		}
	}
	return 0;
}

ps:本人实力较菜,只能做出这些简单题,后面的题如果有哪位大神可以解决可以私信我,如果这里面有讲的不清楚或者有错误的,望及时指出!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值