2021.1.20寒假打卡Day16

A - Wizard of Orz

There are n digital panels placed in a straight line. Each panel can show any digit from 0 to 9. Initially, all panels show 0.

Every second, the digit shown by each panel increases by 1. In other words, at the end of every second, a panel that showed 9 would now show 0, a panel that showed 0 would now show 1, a panel that showed 1 would now show 2, and so on.

When a panel is paused, the digit displayed on the panel does not change in the subsequent seconds.

You must pause exactly one of these panels, at any second you wish. Then, the panels adjacent to it get paused one second later, the panels adjacent to those get paused 2 seconds later, and so on. In other words, if you pause panel x, panel y (for all valid y) would be paused exactly |x−y| seconds later.

For example, suppose there are 4 panels, and the 3-rd panel is paused when the digit 9 is on it.

The panel 1 pauses 2 seconds later, so it has the digit 1;
the panel 2 pauses 1 second later, so it has the digit 0;
the panel 4 pauses 1 second later, so it has the digit 0.
The resulting 4-digit number is 1090. Note that this example is not optimal for n=4.

Once all panels have been paused, you write the digits displayed on them from left to right, to form an n digit number (it can consist of leading zeros). What is the largest possible number you can get? Initially, all panels show 0.

Input
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. Each test case consists of a single line containing a single integer n (1≤n≤2e5).

It is guaranteed that the sum of n over all test cases does not exceed 2e5.

Output
For each test case, print the largest number you can achieve, if you pause one panel optimally.

Example
Input

2
1
2

Output

9
98

Note
In the first test case, it is optimal to pause the first panel when the number 9 is displayed on it.

In the second test case, it is optimal to pause the second panel when the number 8 is displayed on it.

最大的情况其实是确定的,第一位必须是9,那么第二位最大是8,第三位最大是9,第四位只能是0,接下来是1234567890循环……

#include <iostream>
using namespace std;
int a[13]={9,8,9,0,1,2,3,4,5,6,7,8,9};

int main(){
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		int k=0;
		while(n--){
			if(k>12) k=3;
			cout<<a[k];
			++k;
		}
		cout<<endl;
	} 
	return 0;
}

B - Replacing Elements

You have an array a1,a2,…,an. All ai are positive integers.

In one step you can choose three distinct indices i, j, and k (i≠j; i≠k; j≠k) and assign the sum of aj and ak to ai, i. e. make ai=aj+ak.

Can you make all ai lower or equal to d using the operation above any number of times (possibly, zero)?

Input
The first line contains a single integer t (1≤t≤2000) — the number of test cases.

The first line of each test case contains two integers n and d (3≤n≤100; 1≤d≤100) — the number of elements in the array a and the value d.

The second line contains n integers a1,a2,…,an (1≤ai≤100) — the array a.

Output
For each test case, print YES, if it’s possible to make all elements ai less or equal than d using the operation above. Otherwise, print NO.

You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).

Example
Input

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

Output

NO
YES
YES

Note
In the first test case, we can prove that we can’t make all ai≤3.

In the second test case, all ai are already less or equal than d=4.

In the third test case, we can, for example, choose i=5, j=1, k=2 and make a5=a1+a2=2+1=3. Array a will become [2,1,5,3,3].

After that we can make a3=a5+a2=3+1=4. Array will become [2,1,4,3,3] and all elements are less or equal than d=4.

如果有比d大的数,只要最小的那两个比d小即可

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
	int t;cin>>t;
	while(t--){
		int n,d;
		int a[105];
		int flag=1;//标记是否需要操作 
		cin>>n>>d;
		for(int i=0;i<n;i++){
			cin>>a[i];
			if(a[n-1]<=d) flag=0;
		}
		if(flag){
			sort(a,a+n+1);
			if(a[n-1]+a[n-2]>d) cout<<"NO"<<endl;
			else cout<<"YES"<<endl;
		}
		else cout<<"YES"<<endl;
	} 
	return 0;
}

C - String LCM

Let’s define a multiplication operation between a string a and a positive integer x: a⋅x is the string that is a result of writing x copies of a one after another. For example, “abc” ⋅ 2 = “abcabc”, “a” ⋅ 5 = “aaaaa”.

A string a is divisible by another string b if there exists an integer x such that b⋅x=a. For example, “abababab” is divisible by “ab”, but is not divisible by “ababab” or “aa”.

LCM of two strings s and t (defined as LCM(s,t)) is the shortest non-empty string that is divisible by both s and t.

You are given two strings s and t. Find LCM(s,t) or report that it does not exist. It can be shown that if LCM(s,t) exists, it is unique.

Input
The first line contains one integer q (1≤q≤2000) — the number of test cases.

Each test case consists of two lines, containing strings s and t (1≤|s|,|t|≤20). Each character in each of these strings is either ‘a’ or ‘b’.

Output
For each test case, print LCM(s,t) if it exists; otherwise, print -1. It can be shown that if LCM(s,t) exists, it is unique.

Example
Input

3
baba
ba
aa
aaa
aba
ab

Output

baba
aaaaaa
-1

Note
In the first test case, “baba” = “baba” ⋅ 1 = “ba” ⋅ 2.

In the second test case, “aaaaaa” = “aa” ⋅ 3 = “aaa” ⋅ 2.

找循环单体,然后根据两个字符串单体个数的最小公倍数来生成string LCM

#include <iostream>
#include<string> 
using namespace std;

int findL(string s){			//查找循环点
	int len=s.length(),l=len;
	for(int i=0;i<len;i++){
		if(len%i!=0) continue;
		else{
			int flag=1;
			for(int j=i+1;j<len;j++)
				if(s[j]!=s[j%i-1]){flag=0;break;}
			if(flag) {l=i;break;}
		} 
	}
	return l;
}

int Gcd(int a,int b){
	int r;
	r=a%b;
	if(r==0) return b;
	else return Gcd(b,r);
}

int Lcm(int a,int b){
	return a*b/Gcd(a,b);
}

int main(){
	int c; cin>>c;
	while(c--){
		int l1,l2,lcm;			//s1,s2的循环点,循环单体数的lcm
		string s1,s2,ls1,ls2;	//s1,s2及其循环单体ls1,ls2
		cin>>s1>>s2;
		l1=findL(s1);
		ls1=s1.substr(0,l1);
		l2=findL(s2);
		if(l1==l2){
			ls2=s2.substr(0,l2);
			if(ls1==ls2){
				lcm=Lcm((int)s1.length()/(l1+1),(int)s2.length()/(l2+1));
				for(int i=1;i<=lcm;i++) 
					cout<<ls1;
			}else{
				cout<<"-1";
			}
		}else{
			cout<<"-1";
		}
		cout<<endl;
	}
	return 0;
}

D - Hills And Valleys

You are given a sequence of n integers a1, a2, …, an. Let us call an index j (2≤j≤n−1) a hill if aj>aj+1 and aj>aj−1; and let us call it a valley if aj<aj+1 and aj<aj−1.

Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve?

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

The first line of each test case contains a single integer n (1≤n≤3e5).

The second line of each test case contains n space-separated integers a1, a2, …, an (1≤ai≤109).

It is guaranteed that the sum of n over all test cases does not exceed 3e5.

Output
For each test case, print a single integer — the minimum intimidation value that you can achieve.

Example
Input

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

Output

0
0
1
0

Note
In the first test case, changing a2 to 2 results in no hills and no valleys.

In the second test case, the best answer is just to leave the array as it is.

In the third test case, changing a3 to 6 results in only one valley (at the index 5).

In the fourth test case, changing a3 to 6 results in no hills and no valleys.

有亿点难。。。

E - No More Inversions

You have a sequence a with n elements 1,2,3,…,k−1,k,k−1,k−2,…,k−(n−k) (k≤n<2k).

Let’s call as inversion in a a pair of indices i<j such that a[i]>a[j].

Suppose, you have some permutation p of size k and you build a sequence b of size n in the following manner: b[i]=p[a[i]].

Your goal is to find such permutation p that the total number of inversions in b doesn’t exceed the total number of inversions in a, and b is lexicographically maximum.

Small reminder: the sequence of k integers is called a permutation if it contains all integers from 1 to k exactly once.

Another small reminder: a sequence s is lexicographically smaller than another sequence t, if either s is a prefix of t, or for the first i such that si≠ti, si<ti holds (in the first position that these sequences are different, s has smaller number than t).

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first and only line of each test case contains two integers n and k (k≤n<2k; 1≤k≤105) — the length of the sequence a and its maximum.

It’s guaranteed that the total sum of k over test cases doesn’t exceed 105.

Output
For each test case, print k integers — the permutation p which maximizes b lexicographically without increasing the total number of inversions.

It can be proven that p exists and is unique.

Example
Input

4
1 1
2 2
3 2
4 3

Output

1 
1 2 
2 1 
1 3 2 

Note
In the first test case, the sequence a=[1], there is only one permutation p=[1].

In the second test case, the sequence a=[1,2]. There is no inversion in a, so there is only one permutation p=[1,2] which doesn’t increase the number of inversions.

In the third test case, a=[1,2,1] and has 1 inversion. If we use p=[2,1], then b=[p[a[1]],p[a[2]],p[a[3]]]=[2,1,2] and also has 1 inversion.

In the fourth test case, a=[1,2,3,2], and since p=[1,3,2] then b=[1,3,2,3]. Both a and b have 1 inversion and b is the lexicographically maximum.

大佬的题解,看不懂 ,但是答案很简单

  • 日后再来研究
#include <iostream>
using namespace std;

int main(){
	int T;
	cin>>T;
	while(T--){
		int n,k;
		cin>>n>>k;
		for(int i=1;i<k-(n-k);i++)
			cout<<i<<' ';
		for(int i=k;i>=k-(n-k);i--)
			cout<<i<<' ';
		cout<<endl;
	}
	return 0;
}

F - Three Bags

You are given three bags. Each bag contains a non-empty multiset of numbers. You can perform a number of operations on these bags. In one operation, you can choose any two non-empty bags, and choose one number from each of the bags. Let’s say that you choose number a from the first bag and number b from the second bag. Then, you remove b from the second bag and replace a with a−b in the first bag. Note that if there are multiple occurrences of these numbers, then you shall only remove/replace exactly one occurrence.

You have to perform these operations in such a way that you have exactly one number remaining in exactly one of the bags (the other two bags being empty). It can be shown that you can always apply these operations to receive such a configuration in the end. Among all these configurations, find the one which has the maximum number left in the end.

Input
The first line of the input contains three space-separated integers n1, n2 and n3 (1≤n1,n2,n3≤3e5, 1≤n1+n2+n3≤3e5) — the number of numbers in the three bags.

The i-th of the next three lines contain ni space-separated integers ai,1, ai,2, …, ai,ni (1≤ai,j≤109) — the numbers in the i-th bag.

Output
Print a single integer — the maximum number which you can achieve in the end.

Examples
Input

2 4 1
1 2
6 3 4 5
5

Output

20

Input

3 2 2
7 5 4
2 9
7 1

Output

29

Note
In the first example input, let us perform the following operations:
[1,2],[6,3,4,5],[5]
(Applying an operation to (1,6))
[−5,2],[3,4,5],[5]
(Applying an operation to (−5,5))
[−10,2],[3,4],[5]
(Applying an operation to (5,−10))
[2],[3,4],[15]
(Applying an operation to (2,3))
[−1],[4],[15]
(Applying an operation to (−1,4))
[−5],[],[15]
(Applying an operation to (15,−5))
[],[],[20]

You can verify that you cannot achieve a bigger number. Hence, the answer is 20.

理解一下题意
有A,B,C三个集合,可以做的操作只有从一个集合里取一个数出来,乘以 -1 ,放到另一个集合和其中一个数相加。目标是操作到只剩下一个数,求这个最大的数。
思路: 发现当一个数被操作偶数次不影响其原来的正负性——如,A 中取 x 到 B 与 y 做差得到 y-x,接着取 y-x 到 C 与 z 做差得到 z+x ,最后的效果是 y的符号变为负,那么目标是尽可能让 y 小和少
假设最后一个数在A中,那么可以保留A中最大的数,其余的数并入B中最小的数,B中保留最小的数(用来和A合并),其余的数与C中最小的数合并,而C中其余的数与B中最小的数再合并,最后将两个合并体再全部移回A;最后的答案就是总和减去最小的两个数。

到这里为止WA QAQ

仔细考虑发现,有可能B最小的数很,那么B不能有中间数,那就要牺牲全部的C保全B;这种情况答案是A+B-C,与另一种比较答案谁大即可。

#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 3e5 + 5;
long long a[maxn], b[maxn], c[maxn];

int main()
{
	int n1, n2, n3;
	scanf("%d%d%d", &n1, &n2, &n3);
	
	long long sum[3] = {};
	long long ma, mb, mc;
	ma = mb = mc = 1e9;
	for(int i = 0; i < n1; i ++)
	{
		scanf("%d", &a[i]);
		ma = min(ma, a[i]);
		sum[0] += a[i];
	}
	for(int i = 0; i < n2; i ++)
	{
		scanf("%d", &b[i]);
		mb = min(mb, b[i]);
		sum[1] += b[i];
	}
	for(int i = 0; i < n3; i ++)
	{
		scanf("%d", &c[i]);
		mc = min(mc, c[i]);
		sum[2] += c[i];
	}
	
	long long tmp1 = min(ma + mb, min(mb + mc, mc + ma));
	long long tmp2 = min(sum[0], min(sum[1], sum[2]));
	long long ans = sum[0] + sum[1] + sum[2] - 2 * min(tmp1, tmp2);
	printf("%lld\n", ans);
	return 0;
}

哦呼~ 这题肝了我好久好久啊

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页