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.
The first line of the input contains two integer numbers nn and kk (2≤n≤1092≤n≤109, 1≤k≤501≤k≤50) — the number from which Tanya will subtract and the number of subtractions correspondingly.
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.
512 4
50
1000000000 9
1
The first example corresponds to the following sequence: 512→511→510→51→50512→511→510→51→50.
签到
1 #include2 #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.
The first line of the input contains integer number nn (2≤n≤1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.
Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string ss as a substring (i.e. two consecutive characters of the string) maximal number of times.
7
ABACABA
AB
5
ZZZAA
ZZ
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 #include2 #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. 1≤x≤1091≤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).
The first line of the input contains integer numbers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 0≤k≤n0≤k≤n). The second line of the input contains nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the sequence itself.
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).
7 4
3 7 5 1 10 3 20
6
7 2
3 7 5 1 10 3 20
-1
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 #include2 #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 n−1n−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.
The first line of the input contatins an integer number nn (2≤n≤1002≤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 (1≤ai≤3⋅10181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
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.
6
4 8 6 3 12 9
9 3 6 12 4 8
4
42 28 84 126
126 42 84 28
2
1000000000000000000 3000000000000000000
3000000000000000000 1000000000000000000
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 #include2 #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.
The first line contains two integer numbers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 0≤m≤2⋅1050≤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 (1≤vi,ui≤n1≤vi,ui≤n, ui≠viui≠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.
Print one integer — the number of connected components which are also cycles.
5 4
1 2
3 4
5 4
3 5
1
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
2
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 #include2 #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 }