码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #440 (Div. 2)(ABC)

时间:2017-10-15 21:30:23      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:minimum   his   多少   dig   integer   ber   dex   ever   mon   

A. Search for Pretty Integers

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

a、b数组如果有公共数字输出最小的否则输出两个最小数组成的数。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 map<int,int> mp;
 4 int a[11], b[11];
 5 int main() {
 6     int n, m;
 7     cin >> n >> m;
 8     for(int i = 0; i < n; i ++) cin >> a[i],mp[a[i]]++;
 9     for(int i = 0; i < m; i ++) cin >> b[i];
10     sort(a,a+n);
11     sort(b,b+m);
12     for(int i = 0; i < m; i ++) {
13         if(mp.count(b[i])) return 0*printf("%d\n",b[i]);
14     }
15     printf("%d%d",min(a[0],b[0]),max(a[0],b[0]));
16     return 0;
17 }

 

 

B. Maximum of Maximums of Minimums

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?=?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.

 

k=2的时候需要特判一下 其它的话k=1就是最小值否则最大值。

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 using namespace std;
 4 const int N = 1e5+10;
 5 int dmin[N][25], a[N], n;
 6 void init() {
 7     for(int i = 1; i <= n; i ++){
 8         dmin[i][0] = a[i];
 9     }
10     for(int j = 1; (1<<j) <= n; j ++){
11         for(int i = 1; i+(1<<j)-1<= n; i ++){
12             dmin[i][j] = min(dmin[i][j-1],dmin[(1<<(j-1))+i][j-1]);
13         }
14     }
15 }
16 int getValue(int l, int r){
17     int k = 0;
18     while(1<<(k+1) <= (r-l+1))k++;
19     return min(dmin[l][k],dmin[r-(1<<k)+1][k]);
20 }
21 int main() {
22     int k, MAX = -INF, MIN = INF;
23     cin >> n >> k;
24     for(int i = 1; i <= n; i ++) cin >> a[i], MAX = max(MAX, a[i]), MIN = min(MIN, a[i]);
25     if(k == 1) printf("%d\n",MIN);
26     else if(k == 2) {
27         if(a[1] == MAX || a[n] == MAX) printf("%d\n",MAX);
28         else {
29             init();
30             int ans = -INF;
31             for(int i = 1; i < n; i ++) {
32                 int cnt = max(getValue(1,i),getValue(i+1,n));
33                 if(cnt > ans) ans = cnt;
34             }
35             printf("%d\n",ans);
36         }
37     }else printf("%d\n",MAX);
38     return 0;
39 }

 

C. Maximum splitting

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?+?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.

 

求n最多可以由多少个非素数组成找规律。

非素数有 4 6 8 9 10 12 14 15

偶数规律就是n/4  n=2的时候特判一下

奇数时,9 9+4 9+4+4..... 15 15+4 15+4+4......其它到的就是-1

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main() {
 5     int q, n;
 6     cin >> q;
 7     while(q--) {
 8         cin >> n;
 9         if(n&1) {
10             if(n < 9) printf("-1\n");
11             else {
12                 if((n-1)%4==0) printf("%d\n",1+(n-9)/4);
13                 else if(n == 11) printf("-1\n");
14                 else printf("%d\n",2+(n-15)/4);
15             }
16         } else {
17             if(n == 2) printf("-1\n");
18             else if(n%4 == 0) printf("%d\n",n/4);
19             else printf("%d\n",(n-2)/4);
20         }
21     }
22     return 0;
23 }

 

Codeforces Round #440 (Div. 2)(ABC)

标签:minimum   his   多少   dig   integer   ber   dex   ever   mon   

原文地址:http://www.cnblogs.com/xingkongyihao/p/7672961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!