Codeforces 870

来源:互联网 发布:团队协作工具知乎 编辑:程序博客网 时间:2024/06/16 04:31

CodeForces 870A
Search for Pretty Integers

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

Sample Input
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
Hint
In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 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.

题意:给两个整数m,n,然后给m个数(数组a)以及n个数(数组b)。求一个数,这个数的每一位必须在数组a以及数组b里至少出现一次。求所有满足条件里当中最小的数。

解题思路:两种情况:
1.a数组与b数组全不相等,这时候找a数组最小的值以及b数组最小的值然后组成一个数(偏小的一个);
2.如果有相等的,直接输出a数组与b数组中相同的数中最小的一个。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;int a[10],b[10];int main(){    int m,n;    cin>>m>>n;    for(int i=0; i<m; i++)        cin>>a[i];    for(int j=0; j<n; j++)        cin>>b[j];    sort(a,a+m);    sort(b,b+n);    int flag=0,ans;    for(int i=0,j=0; i<m&&j<n;)//找相同的数    {        if(a[i]==b[j])        {            ans=a[i];            flag=1;            break;        }        else if(a[i]<b[j])            i++;        else if(a[i]>b[j])            j++;    }    if(flag==1)    {        cout<<ans<<endl;    }    else//全不相同    {        if(a[0]==b[0])            cout<<a[0]<<endl;        else        {            if(a[0]<b[0])                cout<<a[0]*10+b[0]<<endl;            else                cout<<b[0]*10+a[0]<<endl;        }    }}

CodeForces 870B
Maximum of Maximums of Minimums

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

Sample Input
Input
5 2
1 2 3 4 5
Output
5
Input
5 1
-4 -5 -3 -2 -1
Output
-5
Hint
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 = 1, rk = n, li = 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.

题意:给两个整数n和k,然后接下来有n个数。要求是将这n个数分成k组,然后求出每一组里最小的数,再找出这些最小数当中最大的数。

解题思路:三种情况

当k=1时,输出最小值;
当k=2时,输出边界的数中较大的那个;
当k=3时,输出所有数当中最大的。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;const int M=1e5;int a[M+50];int b[M+50];int main(){    int n,k;    cin>>n>>k;    for(int i=0; i<n; i++)        {            cin>>a[i];            b[i]=a[i];        }    int t1=a[n-1],t2=a[0];    sort(a,a+n);    if(k==1)        cout<<a[0];    else if(k==2)    {        cout<<(b[0]<b[n-1]?b[n-1]:b[0])<<endl;    }    else        cout<<a[n-1]<<endl;}

CodeForces 870C

Maximum splitting

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

Sample Input
Input
1
12
Output
3
Input
2
6
8
Output
1
2
Input
3
1
2
3
Output
-1
-1
-1
Hint
12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.

8 = 4 + 4, 6 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。

解题思路:
用给的数对4取余,判断余数的情况,举个例子

16%4=0; 16=4+4+4+4,因为全是素数,故结果为4(找不到其他表达式中素数更多的式子)
17%4=1; 17=4+4+4+4+1,因为1是非素数,所以用两个4和1组成9,结果为3
18%4=2; 18=4+4+4+4+2,因为2是非素数,所以用一个4和2组成6,结果为4
19%4=3; 19=4+4+4+4+3,因为3是非素数,3=1+2,4和2组成6,4+4+1=9,结果为3

有几个数需要特判,1 2 3 5 7 11 这几个数不能由素数组成,输出-1

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;int main(){    int T;    cin>>T;    while(T--)    {        int x;        cin>>x;        if(x==1||x==2||x==3||x==5||x==7||x==11)            cout<<-1<<endl;        else if(x%4==0)            cout<<x/4<<endl;        else if(x%4==1)            cout<<x/4-1<<endl;        else if(x%4==2)            cout<<x/4<<endl;        else if(x%4==3)            cout<<x/4-1<<endl;    }}
原创粉丝点击