Codeforces Round #440 (A-C ) 题解

来源:互联网 发布:直播看电视的软件 编辑:程序博客网 时间:2024/06/05 02:51


A 题

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 34 25 7 6
output
25
input
8 81 2 3 4 5 6 7 88 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.


12


【题意】

求两行中组合的最小, 若存在 两行中都有, 的话 则直接输出


【代码】

#include <bits/stdc++.h>const int INF=0x3f3f3f3f;const int MAXN=12;typedef long long ll;using namespace std;int a[MAXN],b[MAXN];int main(){int n,m;while(~scanf("%d %d",&n,&m)){for(int i=1;i<=n;i++){scanf("%d",&a[i]);}for(int j=1;j<=m;j++){scanf("%d",&b[j]);}int ans=INF;sort(a+1,a+n+1);sort(b+1,b+m+1);int i,j;for( i=1;i<=n;i++){for( j=1;j<=m;j++){if(a[i]==b[j])ans=min(ans,a[i]);ans=min(ans,min(a[i],b[j])*10+max(a[i],b[j]));}}cout<<ans<<endl;}return 0;}



B

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 21 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.


12

【题意】

连续区间 求最小中最大 

【思路】


可以发现 当k>=3 时  我们始终可以让最大值 单独一组。 k==1 时 一定是最小值

当n==2 时    结果一定是在 第一个和 最后一个中选择;


【代码】

#include <bits/stdc++.h>#define mem(a,b) memset(a,b,sizeof(a));const int INF=0x3f3f3f3f;const int MAXN=500500;const int MAX=500050;typedef long long ll;using namespace std;struct cmp1{      bool operator ()(int &a,int &b){          return a>b;//?????      }  };  priority_queue<int,vector<int>,cmp1> Q,P;int a[MAXN];int main(){int n,k;while(~scanf("%d %d",&n,&k)){mem(a,0);int x=INF;int y=-INF;for(int i=1;i<=n;i++){scanf("%d",&a[i]);x=min(x,a[i]);y=max(y,a[i]);}if(k==1){printf("%d\n",x);}else if(k>=3){printf("%d\n",y);}else{printf("%d\n",max(a[1],a[n]));}}return 0;}


C

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
112
output
3
input
268
output
12
input
3123
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.


【题意】

问一个数  由非素数 组合 最多有多少个;

【思路】

多写几组可以发现, 1 2 3 5 7 11 时 为 -1

任何一个数 都可以由 4 6 9 组合出来, 

而且发现  当 n 为奇数时  结果为 (n-4) / 4

n 为偶数时 结果为 n / 4


【代码】

#include <bits/stdc++.h>#define mem(a,b) memset(a,b,sizeof(a));const int INF=0x3f3f3f3f;const int MAXN=500500;const int MAX=500050;typedef long long ll;using namespace std;int main(){int t;cin>>t;int n;while(t--){scanf("%d",&n);if(n<4||n==5||n==7||n==11)printf("-1\n");else{if(n&1)printf("%d\n",(n-4)/4 );elseprintf("%d\n",n/4 );}}return 0;}


1

12

原创粉丝点击