2022/1/14总结

今日份的摸鱼到账咯!我今日学习时间共8:40:00

可是。。。我一道题目没做出来。。。Orz

今天上午一直在尝试单词拼接,下午也一样,结果发现样例一直都过不了(???),可能是我思路错了,下午换了个思路重新写了一遍,发现还是错的(???)然后我就去看书了。


大话数据结构,真的很形象地讲述了数据结构的知识点,而且很有趣。目前我只看了个开头,还没有看到后面比较“高级”的内容。

感觉... ...脑子里塞下了好多东西,但是还没消化(。

为了避免这个总结太水,就分享一下我CF写的3题代码吧(我是真的菜)


 


Polycarp got an array of integers a[1…n]a[1…n] as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become a1=a2=⋯=ana1=a2=⋯=an).

  • In one operation, he can take some indices in the array and increase the elements of the array at those indices by 11.

For example, let a=[4,2,1,6,2]a=[4,2,1,6,2]. He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by 11. As a result, in one operation, he can get a new state of the array a=[5,3,1,7,2]a=[5,3,1,7,2].

What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become a1=a2=⋯=ana1=a2=⋯=an)?

Input

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

The following are descriptions of the input test cases.

The first line of the description of each test case contains one integer nn (1≤n≤501≤n≤50)  — the array aa.

The second line of the description of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109)  — elements of the array aa.

Output

For each test case, print one integer  — the minimum number of operations to make all elements of the array aa equal.

Example

input

Copy

3
6
3 4 2 4 1 2
3
1000 1002 998
2
12 11

output

Copy

3
4
1

Note

First test case:

  • a=[3,4,2,4,1,2]a=[3,4,2,4,1,2] take a3,a5a3,a5 and perform an operation plus one on them, as a result we get a=[3,4,3,4,2,2]a=[3,4,3,4,2,2].
  • a=[3,4,3,4,2,2]a=[3,4,3,4,2,2] we take a1,a5,a6a1,a5,a6 and perform an operation on them plus one, as a result we get a=[4,4,3,4,3,3]a=[4,4,3,4,3,3].
  • a=[4,4,3,4,3,3]a=[4,4,3,4,3,3] we take a3,a5,a6a3,a5,a6 and perform an operation on them plus one, as a result we get a=[4,4,4,4,4,4]a=[4,4,4,4,4,4].

There are other sequences of 33 operations, after the application of which all elements become equal.

Second test case:

  • a=[1000,1002,998]a=[1000,1002,998] 2 times we take a1,a3a1,a3 and perform an operation plus one on them, as a result we get a=[1002,1002,1000]a=[1002,1002,1000].
  • a=[1002,1002,1000]a=[1002,1002,1000] also take a3a3 2 times and perform an operation plus one on it, as a result we get a=[1002,1002,1002]a=[1002,1002,1002].

Third test case:

  • a=[12,11]a=[12,11] take a2a2 and perform an operation plus one on it, as a result we get a=[12,12]a=[12,12].

这题,大胆地猜一猜,答案是不是就是一组数据里最大值和最小值的差值呢?

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	for(int i = 0;i < T;i ++)
	{
		int n;
		cin >> n;
		ll a[n + 1] = {0};
		ll min = 1e9 + 7;
		ll max = 0;
		
		for(int j = 0;j < n;j ++)
		{
			cin >> a[j];
			if(a[j] > max) max = a[j];
			if(a[j] < min) min = a[j];
		}
		
		cout << max - min << endl;
	}
	return 0;
}

然后它就AC了,嗯。


Polycarp has 33 positive integers aa, bb and cc. He can perform the following operation exactly once.

  • Choose a positive integer mm and multiply exactly one of the integers aa, bb or cc by mm.

Can Polycarp make it so that after performing the operation, the sequence of three numbers aa, bb, cc (in this order) forms an arithmetic progression? Note that you cannot change the order of aa, bb and cc.

Formally, a sequence x1,x2,…,xnx1,x2,…,xn is called an arithmetic progression (AP) if there exists a number dd (called "common difference") such that xi+1=xi+dxi+1=xi+d for all ii from 11 to n−1n−1. In this problem, n=3n=3.

For example, the following sequences are AP: [5,10,15][5,10,15], [3,2,1][3,2,1], [1,1,1][1,1,1], and [13,10,7][13,10,7]. The following sequences are not AP: [1,2,4][1,2,4], [0,1,0][0,1,0] and [1,3,2][1,3,2].

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 33 integers aa, bb, cc (1≤a,b,c≤1081≤a,b,c≤108).

Output

For each test case print "YES" (without quotes) if Polycarp can choose a positive integer mm and multiply exactly one of the integers aa, bb or cc by mm to make [a,b,c][a,b,c] be an arithmetic progression. Print "NO" (without quotes) otherwise.

You can print YES and NO in any (upper or lower) case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer).

Example

input

Copy

11
10 5 30
30 5 10
1 2 3
1 6 3
2 6 3
1 1 1
1 1 2
1 1 3
1 100000000 1
2 1 1
1 2 2

output

Copy

YES
YES
YES
YES
NO
YES
NO
YES
YES
NO
YES

Note

In the first and second test cases, you can choose the number m=4m=4 and multiply the second number (b=5b=5) by 44.

In the first test case the resulting sequence will be [10,20,30][10,20,30]. This is an AP with a difference d=10d=10.

In the second test case the resulting sequence will be [30,20,10][30,20,10]. This is an AP with a difference d=−10d=−10.

In the third test case, you can choose m=1m=1 and multiply any number by 11. The resulting sequence will be [1,2,3][1,2,3]. This is an AP with a difference d=1d=1.

In the fourth test case, you can choose m=9m=9 and multiply the first number (a=1a=1) by 99. The resulting sequence will be [9,6,3][9,6,3]. This is an AP with a difference d=−3d=−3.

In the fifth test case, it is impossible to make an AP.

这道题,就是看能不能通过乘数制造一个只有三个元素的等差数列。因为是只有3个元素嘛,把各种情况列出来,我们就可以得到2个不同的公差,然后各种情况枚举一下就可以得出答案了。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

bool check(ll *a);

int main()
{
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	ll a[3] = {0};
	for(int i = 0;i < T;i ++)
	{
		for(int j = 0;j < 3;j ++)
		{
			cin >> a[j];
		}
		bool c = check(a);
		if(c) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	
	return 0;
}

bool check(ll *a)
{
	ll a1 = *a;
	ll a2 = *(a + 1);
	ll a3 = *(a + 2);
	ll pre = 0;
	ll d = 0;
	if(a3 - a2 == a2 - a1)
	{
		return true;
	}
	d = a3 - a2;
	pre = a2 - d;
	if(pre > 0 && pre % a1 == 0)
	{
//		cout << "a1: " << pre << endl;
		return true;	
	} 
	d = a2 - a1;
	pre = a2 + d;
	if(pre > 0 && pre % a3 == 0)
	{
//		cout << "a3: " << pre << endl;
		return true;	
	} 
//	if(1.0 * (a3 - a1) / 2 != (a3 - a1) / 2) return false;
	d = (a3 - a1) / 2;
	pre = a1 + d;
	if(pre > 0 && pre % a2 == 0 && pre - a1 == a3 - pre)
	{
//		cout << "a2: " << pre << endl;
		return true;	
	} 
	return false;
}

好了,这题就AC了。


You are given an array aa consisting of nn positive integers. You can perform operations on it.

In one operation you can replace any element of the array aiai with ⌊ai2⌋⌊ai2⌋, that is, by an integer part of dividing aiai by 22 (rounding down).

See if you can apply the operation some number of times (possible 00) to make the array aa become a permutation of numbers from 11 to nn —that is, so that it contains all numbers from 11 to nn, each exactly once.

For example, if a=[1,8,25,2]a=[1,8,25,2], n=4n=4, then the answer is yes. You could do the following:

  1. Replace 88 with ⌊82⌋=4⌊82⌋=4, then a=[1,4,25,2]a=[1,4,25,2].
  2. Replace 2525 with ⌊252⌋=12⌊252⌋=12, then a=[1,4,12,2]a=[1,4,12,2].
  3. Replace 1212 with ⌊122⌋=6⌊122⌋=6, then a=[1,4,6,2]a=[1,4,6,2].
  4. Replace 66 with ⌊62⌋=3⌊62⌋=3, then a=[1,4,3,2]a=[1,4,3,2].

Input

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

Each test case contains exactly two lines. The first one contains an integer nn (1≤n≤501≤n≤50), the second one contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

Output

For each test case, output on a separate line:

  • YES if you can make the array aa become a permutation of numbers from 11 to nn,
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

Example

input

Copy

6
4
1 8 25 2
2
1 1
9
9 8 3 4 2 7 1 5 6
3
8 2 1
4
24 7 16 7
5
22 6 22 4 22

output

Copy

YES
NO
YES
NO
NO
YES

Note

The first test case is explained in the text of the problem statement.

In the second test case, it is not possible to get a permutation.

这题,就是不停地把元素除以二得到它的整数部分,直到它是1~n的值,即1<=x<=n。那么我们只要把拥有的数值给标记出来,下一个数除到1~n之间时如果重复即再除以二,直到为0或等于下一个“缺席”的1~n的值。遍历完数组元素后,我们就得到了一个1~n的"在位"列表,如果都在位,那么是可以实现答案的,输出"YES",如果有"缺席"的,那么就意味着不能实现,输出"NO",下面放上AC代码(其实这题我做了很久很久)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	
	for(int i = 0;i < T;i ++)
	{
		int n;
		cin >> n;
//		ll L = 0;
//		ll R = 1e9 + 7;
		ll a[n + 1] = {0};
		int have[n + 1] = {0};
		bool can = true;
		for(int j = 0;j < n;j ++)
		{
			int pos = 0;
			cin >> a[j];
//			if(a[j] > L) L = a[j];
//			if(a[j] < R) R = a[j];
			ll tmp = a[j];
			while(1)
			{
				if(tmp == 0)
				{
					can = false;
					break;	
				} 
				if(tmp <= n && !have[tmp])
				{
					have[tmp] = 1;
					break;
				}
				tmp /= 2;
			}
		}
		for(int i = 1;i <= n;i ++)
		{
			if(!have[i])
			{
				can = false;
				break;
			}
		}
		if(can)
		{
			cout << "YES" << endl;
		}
		else
		{
			cout << "NO" << endl;
		}
	}
	return 0;
}

明天休息,好诶!可以抽空看看书啦! :D

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ISansXI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值