-
Codeforces Round #479 (Div. 3)코드포스(CodeForce) 2018. 6. 30. 11:43반응형
1. 문제 (http://codeforces.com/contest/977/problem/A)
A. Wrong Subtractiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLittle 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 . Tanya will subtract one from it times. Your task is to print the result after all subtractions.
It is guaranteed that the result will be positive integer number.
InputThe first line of the input contains two integer numbers and (, ) — the number from which Tanya will subtract and the number of subtractions correspondingly.
OutputPrint one integer number — the result of the decreasing by one times.
It is guaranteed that the result will be positive integer number.2. 알고리즘
키워드 - 구현
3. 코드
1234567891011121314151617181920212223242526272829#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <functional> // greater 사용 위해 필요#include <string>#include <map>#include <math.h>using namespace std;int main() {int n, t;cin >> n >> t;while(t--) {int cand = n % 10;if(cand == 0) {n = n / 10;} else {n--;}}cout << n << endl;return 0;}cs 1. 문제 (http://codeforces.com/contest/977/problem/B)
B. Two-gramtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTwo-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 consisting of 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 = "BBAABBBA" the answer is two-gram "BB", which contained in three times. In other words, find any most frequent two-gram.
Note that occurrences of the two-gram can overlap with each other.
InputThe first line of the input contains integer number () — the length of string . The second line of the input contains the string consisting of capital Latin letters.
OutputPrint the only line containing exactly two capital Latin letters — any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times.
2. 알고리즘
키워드 - 구현, 문자열 처리
3. 코드
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include <iostream>#include <sstream>#include <string>#include <algorithm>#include <functional>#include <vector>#include <list>#include <queue>#include <deque>#include <map>#include <set>#include <stack>#include <cstring>using namespace std;#define MAX_SIZE 100#define INF 0x7fffffff#define CENDL "\n"/** @memory - 1984 kb* @time - 0 ms*/int main() {cin.tie(0);std::ios::sync_with_stdio(false);int n; cin >> n;string s;cin >> s;map<string, int> m;int i=0;while(true) {string ss = s.substr(i, 2);m[ss]++;i++;if (i + 2 > s.size()) {break;}}map<string, int>::iterator iter;int sol = -1;string sss;for(iter = m.begin(); iter != m.end(); iter++){if (sol < iter->second) {sol = iter->second;sss.clear();sss.append(iter->first);}}cout << sss << endl;return 0;}cs 1. 문제 (http://codeforces.com/contest/977/problem/C)
C. Less or Equaltime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a sequence of integers of length and integer number . You should print any integer number in the range of (i.e. ) such that exactly elements of given sequence are less than or equal to .
Note that the sequence can contain equal elements.
If there is no such , print "-1" (without quotes).
InputThe first line of the input contains integer numbers and (, ). The second line of the input contains integer numbers () — the sequence itself.
OutputPrint any integer number from range such that exactly elements of given sequence is less or equal to .
If there is no such , print "-1" (without quotes).
2. 알고리즘
키워드 - 정렬
3. 코드
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758#include <iostream>#include <sstream>#include <string>#include <algorithm>#include <functional>#include <vector>#include <list>#include <queue>#include <deque>#include <map>#include <set>#include <stack>#include <cstring>using namespace std;#define MAX_SIZE 100#define INF 0x7fffffff#define CENDL "\n"/** @memory - 1984 kb* @time - 0 ms*/int main() {cin.tie(0);std::ios::sync_with_stdio(false);int n, k; cin >> n >> k;vector<int> arr(n);for (int i = 0; i < n; ++i) {cin >> arr[i];}sort(arr.begin(), arr.end());int ans;if (k == 0) {ans = arr[0] - 1;} else {ans = arr[k - 1];}int cnt = 0;for (int i = 0; i < n; ++i)if (arr[i] <= ans) ++cnt;if (cnt != k || !(1 <= ans && ans <= 1000 * 1000 * 1000)) {cout << -1 << CENDL;return 0;}cout << ans << CENDL;return 0;}cs 1. 문제 (http://codeforces.com/contest/977/problem/D)
2. 알고리즘
키워드 - 정렬, 정수론
3. 코드
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253#include <iostream>#include <sstream>#include <string>#include <algorithm>#include <functional>#include <vector>#include <list>#include <queue>#include <deque>#include <map>#include <set>#include <stack>#include <cstring>using namespace std;#define MAX_SIZE 100#define INF 0x7fffffff#define CENDL "\n"/** @memory - 1984 kb* @time - 0 ms*/typedef long long LL;int count3(LL x){int ret=0;while(x % 3 == 0){ret++;x /= 3;}return ret;}int n;vector<pair<int,LL>> v;int main() {cin.tie(0);std::ios::sync_with_stdio(false);cin>>n;v.resize(n);for(int i=0; i<n; i++){cin>>v[i].second;v[i].first=-count3(v[i].second);}sort(v.begin(), v.end());for(int i=0; i<n; i++)cout << v[i].second << " ";return 0;}cs 1. 문제 (http://codeforces.com/contest/977/problem/E)
E. Cyclic Componentstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an undirected graph consisting of vertices and 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 is connected with a vertex , a vertex is also connected with a vertex ). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.
Two vertices and belong to the same connected component if and only if there is at least one path along edges connecting and .
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 connected components, of them are cycles: and . InputThe first line contains two integer numbers and (, ) — number of vertices and edges.
The following lines contains edges: edge is given as a pair of vertices , (, ). There is no multiple edges in the given graph, i.e. for each pair () there no other pairs () and () in the list of edges.
OutputPrint one integer — the number of connected components which are also cycles.
2. 알고리즘
키워드 - DFS, GRAPHS
3. 코드
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172#include <iostream>#include <sstream>#include <string>#include <algorithm>#include <functional>#include <vector>#include <list>#include <queue>#include <deque>#include <map>#include <set>#include <stack>#include <cstring>using namespace std;#define MAX_SIZE 100#define INF 0x7fffffff#define CENDL "\n"/** @memory - 1984 kb* @time - 0 ms*/const int N = 200 * 1000 + 11;int n, m;int deg[N];bool used[N];vector<int> comp;vector<int> g[N];void dfs(int v) {used[v] = true;comp.push_back(v);for (auto to : g[v])if (!used[to])dfs(to);}int main() {cin.tie(0);std::ios::sync_with_stdio(false);cin >> n >> m;for (int i = 0; i < m; ++i) {int x, y;cin >> x >> y;--x, --y;g[x].push_back(y);g[y].push_back(x);++deg[x];++deg[y];}int ans = 0;for (int i = 0; i < n; ++i) {if (!used[i]) {comp.clear();dfs(i);bool ok = true;for (auto v : comp) ok &= deg[v] == 2;if (ok) ++ans;}}cout << ans << CENDL;return 0;}cs 반응형'코드포스(CodeForce)' 카테고리의 다른 글
Codeforces Round #428 (Div. 2) - A. Arya and Bran (0) 2018.08.17 Codeforces Round #479 (Div. 3) - B - Two-gram (0) 2018.08.17 Codeforces Round #497 (Div. 2) (0) 2018.07.15 Educational Codeforces Round 47 (Rated for Div. 2) (0) 2018.07.15 Codeforces Round #496 (Div. 3) (0) 2018.07.10