ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Codeforces Round #479 (Div. 3)
    코드포스(CodeForce) 2018. 6. 30. 11:43
    반응형


    1. 문제 (http://codeforces.com/contest/977/problem/A)

    A. Wrong Subtraction
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 n. Tanya will subtract one from it k times. Your task is to print the result after all k subtractions.

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

    Input

    The first line of the input contains two integer numbers n and k (2n1091k50) — the number from which Tanya will subtract and the number of subtractions correspondingly.

    Output

    Print one integer number — the result of the decreasing n by one k times.
    It is guaranteed that the result will be positive integer number.

    2. 알고리즘

    키워드 - 구현


    3. 코드


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    #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-gram
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 s consisting of n 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 s = "BBAABBBA" the answer is two-gram "BB", which contained in s 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 n (2n100) — the length of string s. The second line of the input contains the string s consisting of n capital Latin letters.

    Output

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


    2. 알고리즘

    키워드 - 구현, 문자열 처리


    3. 코드


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    #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 + > 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 Equal
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

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

    Note that the sequence can contain equal elements.

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

    Input

    The first line of the input contains integer numbers n and k (1n21050kn). The second line of the input contains n integer numbers a1,a2,,an (1ai109) — the sequence itself.

    Output

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

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


    2. 알고리즘

    키워드 - 정렬


    3. 코드

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    #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 || !(<= ans && ans <= 1000 * 1000 * 1000)) {
            cout << -<< CENDL;
            return 0;
        }
     
        cout << ans << CENDL;
     
        return 0;
    }
    cs


    1. 문제 (http://codeforces.com/contest/977/problem/D)


    2. 알고리즘

    키워드 - 정렬, 정수론


    3. 코드


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    #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 % == 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 Components
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given an undirected graph consisting of n vertices and m 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 a is connected with a vertex b, a vertex b is also connected with a vertex a). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.

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

    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 6 connected components, 2 of them are cycles: [7,10,16] and [5,11,9,15].
    Input

    The first line contains two integer numbers n and m (1n21050m2105) — number of vertices and edges.

    The following m lines contains edges: edge i is given as a pair of vertices viui (1vi,uinuivi). There is no multiple edges in the given graph, i.e. for each pair (vi,ui) there no other pairs (vi,ui) and (ui,vi) in the list of edges.

    Output

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

    2. 알고리즘

    키워드 - DFS, GRAPHS

    3. 코드


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    #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

    반응형

    댓글

Designed by Tistory.