Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

给定两个非零数字列表,任务是找到最小的正整数,该数的十进制表示至少包含一个来自第一个列表的数字和一个来自第二个列表的数字。输出这个最小的漂亮整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Search for Pretty Integers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two lists of non-zero digits.

Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer?

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 9) — the lengths of the first and the second lists, respectively.

The second line contains n distinct digits a1, a2, ..., an (1 ≤ ai ≤ 9) — the elements of the first list.

The third line contains m distinct digits b1, b2, ..., bm (1 ≤ bi ≤ 9) — the elements of the second list.

Output

Print the smallest pretty integer.

Examples
input
2 3
4 2
5 7 6
output
25
input
8 8
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
output
1
Note

In the first example 254624567 are pretty, as well as many other integers. The smallest among them is 2542 and 24 are not pretty because they don't have digits from the second list.

In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.

题意就是输出一个最小数,这个数最少要包含一个第一序列的数,和一个第二序列的数。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 0;
inline int get(){
	char c;
	while((c = getchar()) < '0' || c > '9');
	int cnt = c - '0';
	while((c = getchar()) >= '0' && c <= '9') cnt = cnt * 10 + c - '0';
	return cnt;
}
bool a[20],b[20];
int mina,minb;
int n,m,ans;

int main(){
/*	#ifdef lwy
	#else
		freopen(".in","r",stdin);
		freopen(".out","w",stdout);
	#endif*/
	memset(a,false,sizeof(a));
	memset(b,false,sizeof(b));
	n = get(); m = get();
	mina = 10; minb = 10;
	for(int i = 1; i <= n; i++){
		int x; x = get();
		mina = min(mina,x);
		a[x] = true;
	}
	for(int i = 1; i <= m; i++){
		int x; x = get();
		minb = min(minb,x);
		b[x] = true;
	}
	for(int i = 1; i <= 9; i++){
		if(a[i] && b[i]){
			ans = i;
			printf("%d",ans);
			return 0;
		}
	}
	ans = min(mina,minb) * 10 + max(mina,minb);
	printf("%d",ans);
	return 0;
}

B. Maximum of Maximums of Minimums
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?

Definitions of subsegment and array splitting are given in notes.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤  105) — the size of the array a and the number of subsegments you have to split the array to.

The second line contains n integers a1,  a2,  ...,  an ( - 109  ≤  ai ≤  109).

Output

Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.

Examples
input
5 2
1 2 3 4 5
output
5
input
5 1
-4 -5 -3 -2 -1
output
-5
Note

A subsegment [l,  r] (l ≤ r) of array a is the sequence al,  al + 1,  ...,  ar.

Splitting of array a of n elements into k subsegments [l1, r1][l2, r2], ..., [lk, rk] (l1 = 1rk = nli = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).

In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.

In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4,  - 5,  - 3,  - 2,  - 1). The only minimum is min( - 4,  - 5,  - 3,  - 2,  - 1) =  - 5. The resulting maximum is  - 5.

题意是将一个序列分成k段,要求每段的最小值最大,并输出这个最大值。

一开始以为是个简单的二分。

但再仔细一看其实只要把k分成 == 1 ,== 2 ,>= 3,3类分类讨论就行了。

然而一开始没有考虑到 == 2 与 >=3 的区别,被hacked了。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1E5;
inline int get(){
	char c;
	while((c = getchar()) < '0' || c > '9');
	int cnt = c - '0';
	while((c = getchar()) >= '0' && c <= '9') cnt = cnt * 10 + c - '0';
	return cnt;
}
int N,K;
int a[MAXN+10];
int b[MAXN+10],c[MAXN+10];
int maxa,mina,ans;
int main(){
/*	#ifdef lwy
	#else
		freopen(".in","r",stdin);
		freopen(".out","w",stdout);
	#endif*/
	N = get(); K = get();
	for(int i = 1; i <= N; i++){
		scanf("%d",&a[i]);
	}
	mina = maxa = a[1];
	for(int i = 1; i <= N; i++){
		mina = min(mina,a[i]);
		maxa = max(maxa,a[i]);
	}
	if(K == 1){
		ans = mina;
	}
	if(K >= 3){
		ans = maxa;
	}
	if(K == 2){
		ans = mina;
		for(int i = 1; i <= N; i++){
			b[i] = c[i] = a[i];
		}
		for(int i = 2; i <= N; i++){
			b[i] = min(b[i],b[i-1]);
		}
		for(int i = N-1; i >= 1; i--){
			c[i] = min(c[i],c[i+1]);
		}
		for(int i = 1; i <= N-1; i++){
			ans = max(max(b[i],c[i+1]),ans);
		}
	}
	printf("%d",ans);
	return 0;
}


C. Maximum splitting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.

An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.

Input

The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries.

q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.

Output

For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.

Examples
input
1
12
output
3
input
2
6
8
output
1
2
input
3
1
2
3
output
-1
-1
-1
Note

12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.

8 = 4 + 46 can't be split into several composite summands.

1, 2, 3 are less than any composite number, so they do not have valid splittings.

题意就是要将一个数分成几个合数之和,求最大的合数个数。

这题显然一个数分成4k+n时会最多

把这个数取4的模

分4类分别讨论就行了。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 0;
inline int get(){
	char c;
	while((c = getchar()) < '0' || c > '9');
	int cnt = c - '0';
	while((c = getchar()) >= '0' && c <= '9') cnt = cnt * 10 + c - '0';
	return cnt;
}
int q,n;
int k,t,ans;
int main(){
/*	#ifdef lwy
	#else
		freopen(".in","r",stdin);
		freopen(".out","w",stdout);
	#endif*/
	q = get();
	while(q--){
		n = get();
		k = n % 4;
		t = n / 4;
		if(k == 0) ans = t;
		if(k == 1) {
			t -= 2;
			if(t < 0) ans = -1;
			if(t == 0) ans = 1;
			if(t > 0) ans = t + 1;
		}
		if(k == 2){
			t -= 1;
			if(t < 0) ans = -1;
			if(t == 0) ans = 1;
			if(t > 0) ans = t + 1;
		}
		if(k == 3){
			t -= 3;
			if(t < 0) ans = -1;
			if(t == 0) ans = 2;
			if(t > 0) ans = t + 2;
		}
		printf("%d\n",ans);
	}
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值