Codeforces Round #479 (Div. 3) ABCDEF

A. Wrong Subtraction

Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm:

  • if the last digit of the number is non-zero, she decreases the number by one;
  • if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit).

You are given an integer number nn. Tanya will subtract one from it kk times. Your task is to print the result after all kk subtractions.

It is guaranteed that the result will be positive integer number.

Input

The first line of the input contains two integer numbers nn and kk (2n1092≤n≤109, 1k501≤k≤50) — the number from which Tanya will subtract and the number of subtractions correspondingly.

Output

Print one integer number — the result of the decreasing nn by one kk times.

It is guaranteed that the result will be positive integer number.

Examples
input
Copy
512 4
output
Copy
50
input
Copy
1000000000 9
output
Copy
1
Note

The first example corresponds to the following sequence: 5125115105150512→511→510→51→50.

签到

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 int main() {
 5     int n, k;
 6     cin >> n >> k;
 7     for(int i = 0; i < k; i ++) {
 8         if(n%10) n--;
 9         else n/=10;
10     }
11     cout << n << endl;
12     return 0;
13 }

B. Two-gram

Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams.

You are given a string ss consisting of nn capital Latin letters. Your task is to find any two-gram contained in the given string as a substring(i.e. two consecutive characters of the string) maximal number of times. For example, for string ss = "BBAABBBA" the answer is two-gram "BB", which contained in ss three times. In other words, find any most frequent two-gram.

Note that occurrences of the two-gram can overlap with each other.

Input

The first line of the input contains integer number nn (2n1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.

Output

Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string sas a substring (i.e. two consecutive characters of the string) maximal number of times.

Examples
input
Copy
7
ABACABA
output
Copy
AB
input
Copy
5
ZZZAA
output
Copy
ZZ
Note

In the first example "BA" is also valid answer.

In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times.

求出现最多的连续两个字母

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 map<string,int> mp;
 5 int main() {
 6     int n;
 7     string s;
 8     cin >> n >> s;
 9     for(int i = 0; i < n-1; i ++) {
10         string ss = s.substr(i,2);
11         mp[ss]++;
12     }
13     n = 0;
14     map<string,int> ::iterator it = mp.begin();
15     for(; it!=mp.end(); it++) {
16         if((*it).second > n) {
17             n = (*it).second;
18             s = (*it).first;
19         }
20     }
21     cout << s<< endl;
22     return 0;
23 }

C. Less or Equal

You are given a sequence of integers of length nn and integer number kk. You should print any integer number xx in the range of [1;109][1;109] (i.e. 1x1091≤x≤109) such that exactly kk elements of given sequence are less than or equal to xx.

Note that the sequence can contain equal elements.

If there is no such xx, print "-1" (without quotes).

Input

The first line of the input contains integer numbers nn and kk (1n21051≤n≤2⋅105, 0kn0≤k≤n). The second line of the input contains nn integer numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the sequence itself.

Output

Print any integer number xx from range [1;109][1;109] such that exactly kk elements of given sequence is less or equal to xx.

If there is no such xx, print "-1" (without quotes).

Examples
input
Copy
7 4
3 7 5 1 10 3 20
output
Copy
6
input
Copy
7 2
3 7 5 1 10 3 20
output
Copy
-1
Note

In the first example 55 is also a valid answer because the elements with indices [1,3,4,6][1,3,4,6] is less than or equal to 55 and obviously less than or equal to 66.

In the second example you cannot choose any number that only 22 elements of the given sequence will be less than or equal to this number because 33 elements of the given sequence will be also less than or equal to this number.

求是否存在一个数x,使的数组中存在k的个小于等于x。

k = 0非常特殊, 一直错在这里。k = 0时,要看第一个数是否等于1,等于1的话不存在这个的x,因为x有个范围。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 2e5+10;
 5 int a[N];
 6 int main() {
 7     int n, k;
 8     cin >> n >> k;
 9     for(int i = 0; i < n; i ++) cin >> a[i];
10     sort(a,a+n);
11     if(k == 0) {
12         if(a[0] == 1) printf("-1\n");
13         else printf("1\n");
14         return 0;
15     }
16     if(a[k-1] == a[k]) printf("-1\n");
17     else printf("%d\n",a[k-1]);
18     return 0;
19 }

D. Divide by three, multiply by two

Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n1n−1operations of the two kinds:

  • divide the number xx by 33 (xx must be divisible by 33);
  • multiply the number xx by 22.

After each operation, Polycarp writes down the result on the board and replaces xx by the result. So there will be nn numbers on the board after all.

You are given a sequence of length nn — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.

Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.

It is guaranteed that the answer exists.

Input

The first line of the input contatins an integer number nn (2n1002≤n≤100) — the number of the elements in the sequence. The second line of the input contains nn integer numbers a1,a2,,ana1,a2,…,an (1ai310181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output

Print nn integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.

It is guaranteed that the answer exists.

Examples
input
Copy
6
4 8 6 3 12 9
output
Copy
9 3 6 12 4 8 
input
Copy
4
42 28 84 126
output
Copy
126 42 84 28 
input
Copy
2
1000000000000000000 3000000000000000000
output
Copy
3000000000000000000 1000000000000000000 
Note

In the first example the given sequence can be rearranged in the following way: [9,3,6,12,4,8][9,3,6,12,4,8]. It can match possible Polycarp's game which started with x=9x=9.

模拟题。一个数肯定存在一个数在它前面或者在它后面,都存在的话就在中间,否则在两端。

pre[i] 表示第i个是的前面是第j个数,nex[i] 表示第i个数的后面是第j个数

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 ll a[110], nex[110], pre[110];
 5 int main() {
 6     int n;
 7     cin >> n;
 8     for(int i = 1; i <= n; i ++) nex[i] = pre[i] = i;
 9     for(int i = 1; i <= n; i ++) cin >> a[i];
10     for(int i = 1; i <= n; i ++) {
11         for(int j = 1; j <= n; j ++) {
12             if(a[i] > a[j]) {
13                 if(a[i]%3==0 && a[i]/3 == a[j]) {
14                     nex[i] = j;
15                     pre[j] = i;
16                 } else if(a[i]%2==0&&a[i]/2==a[j]) {
17                     nex[j] = i;
18                     pre[i] = j;
19                 }
20             } else if(a[j] > a[i]){
21                 if(a[j]%3==0 && a[j]/3 == a[i]) {
22                     nex[j] = i;
23                     pre[i] = j;
24                 } else if(a[j]%2==0&&a[j]/2==a[i]) {
25                     nex[i] = j;
26                     pre[j] = i;
27                 }
28             }
29         }
30     }
31     int id = 0;
32     for(int i = 1; i <= n; i ++) {
33         if(pre[i] == i) {
34             id = i;
35             break;
36         }
37     }
38     while(nex[id] != id) {
39         printf("%lld ",a[id]);
40         id = nex[id];
41     }
42     printf("%lld\n",a[id]);
43     return 0;
44 }

E. Cyclic Components

You are given an undirected graph consisting of nn vertices and mm edges. Your task is to find the number of connected components which are cycles.

Here are some definitions of graph theory.

An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex aa is connected with a vertex bb, a vertex bb is also connected with a vertex aa). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.

Two vertices uu and vv belong to the same connected component if and only if there is at least one path along edges connecting uu and vv.

A connected component is a cycle if and only if its vertices can be reordered in such a way that:

  • the first vertex is connected with the second vertex by an edge,
  • the second vertex is connected with the third vertex by an edge,
  • ...
  • the last vertex is connected with the first vertex by an edge,
  • all the described edges of a cycle are distinct.

A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices.

There are 66 connected components, 22 of them are cycles: [7,10,16][7,10,16] and [5,11,9,15][5,11,9,15].
Input

The first line contains two integer numbers nn and mm (1n21051≤n≤2⋅105, 0m21050≤m≤2⋅105) — number of vertices and edges.

The following mm lines contains edges: edge ii is given as a pair of vertices vivi, uiui (1vi,uin1≤vi,ui≤n, uiviui≠vi). There is no multiple edges in the given graph, i.e. for each pair (vi,uivi,ui) there no other pairs (vi,uivi,ui) and (ui,viui,vi) in the list of edges.

Output

Print one integer — the number of connected components which are also cycles.

Examples
input
Copy
5 4
1 2
3 4
5 4
3 5
output
Copy
1
input
Copy
17 15
1 8
1 12
5 11
11 9
9 15
15 5
4 13
3 13
4 3
10 16
7 10
16 7
14 3
14 4
17 6
output
Copy
2
Note

In the first example only component [3,4,5][3,4,5] is also a cycle.

The illustration above corresponds to the second example.

判断有多少个圈。构造圈的话,圈中的每个节点都有两个节点,dfs遍历。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 2e5+10;
 5 vector<int> vs[N];
 6 bool vis[N], flag;
 7 int root;
 8 void dfs(int x, int f, int de) {
 9     vis[x] = 1;
10     if(vs[x].size() != 2) return;
11     for(int i = 0; i < vs[x].size(); i ++) {
12         int v = vs[x][i];
13         if(!vis[v] && v != f) dfs(v, x,de+1);
14         if(v == root && de != 1) {
15             flag = true;
16             return;
17         }
18     }
19 }
20 int main() {
21     int n, m;
22     cin >> n >> m;
23     for(int i = 0; i < m; i ++) {
24         int u, v;
25         cin >> u >> v;
26         vs[v].push_back(u);
27         vs[u].push_back(v);
28     }
29     int ans = 0;
30     for(int i = 1; i <= n; i ++) {
31         if(!vis[i]) {
32             flag = 0;
33             root = i;
34             dfs(i,-1,0);
35             if(flag) ans++;
36         }
37     }
38     cout << ans << endl;
39     return 0;
40 }

F. Consecutive Subsequence

You are given an integer array of length nn.

You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to [x,x+1,,x+k1][x,x+1,…,x+k−1] for some value xx and length kk.

Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array [5,3,1,2,4][5,3,1,2,4] the following arrays are subsequences: [3][3], [5,3,1,2,4][5,3,1,2,4], [5,1,4][5,1,4], but the array [1,3][1,3] is not.

Input

The first line of the input containing integer number nn (1n21051≤n≤2⋅105) — the length of the array. The second line of the input containing nn integer numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the array itself.

Output

On the first line print kk — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.

On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.

Examples
input
Copy
7
3 3 4 7 5 6 8
output
Copy
4
2 3 5 6
input
Copy
6
1 3 5 2 4 6
output
Copy
2
1 4
input
Copy
4
10 9 8 7
output
Copy
1
1
input
Copy
9
6 7 8 3 4 5 9 10 11
output
Copy
6
1 2 3 7 8 9
Note

All valid answers for the first example (as sequences of indices):

  • [1,3,5,6][1,3,5,6]
  • [2,3,5,6][2,3,5,6]

All valid answers for the second example:

  • [1,4][1,4]
  • [2,5][2,5]
  • [3,6][3,6]

All valid answers for the third example:

  • [1][1]
  • [2][2]
  • [3][3]
  • [4][4]

All valid answers for the fourth example:

  • [1,2,3,7,8,9][1,2,3,7,8,9]

 判断最大的单调递增子序列,只能增1.

每输入一个数x,就判断x-1在之前是否存在,存在的话使用x的最长增系列长度是mp[x-1] +1 ,否则让mp[x] = 1。

这样mp[a[i]] 的最大值就是最长增序列了,然后输出下标就行。

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 #define ll long long
 4 using namespace std;
 5 const int N = 2e5+10;
 6 map<int,int> mp;
 7 int a[N];
 8 int main() {
 9     int n, x;
10     cin >> n;
11     for(int i = 1; i <= n; i ++) {
12         cin >> a[i];
13         if(mp[a[i]-1]) {
14             mp[a[i]] = mp[a[i]-1]+1;
15         } else mp[a[i]] = 1;
16     }
17     map<int,int> ::iterator it = mp.begin();
18     int MAX = 0, id;
19     for(;it != mp.end(); ++it) {
20         if(MAX < (*it).second){
21             MAX = (*it).second;
22             id = (*it).first;
23         }
24     }
25     cout << MAX << endl;
26     int MIN = id - MAX + 1;
27     for(int i = 1; i <= n; i ++) {
28         if(a[i] == MIN) {
29             printf("%d ",i);
30             MIN++;
31         }
32         if(MIN > id) break;
33     }
34     return 0;
35 }

猜你喜欢

转载自www.cnblogs.com/xingkongyihao/p/9028373.html