0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【ABC376】AtCoder Beginner Contest 376【C++】

Posted at

コンテスト名

AtCoder Beginner Contest 376(Promotion of AtCoder Career Design DAY)

コンテストURL

開催日

2024/10/19 21:00-22:40


A: Candy Button

解法

  • 最後に飴をもらった時間を記録しておき、問題文通りに判定する
ABC376A.cpp
#include <iostream>
#include <vector>
using namespace std;

int main(){
    int n, c;
    cin >> n >> c;

    vector<int> T(n);
    for(int i=0; i<n; i++){
        cin >> T[i];
    }

    int last = T[0];
    int cnt = 1;
    for(int i=1; i<n; i++){
        if(T[i]-last>=c){
            cnt++;
            last = T[i];
        }
    }

    cout << cnt << endl;

    return 0;
}

B: Hands on Ring (Easy)

解法

  • $T_i$ と LR の位置関係によって条件分岐する
ABC376B.cpp
#include <iostream>
using namespace std;

int main(){
    int n, q;
    cin >> n >> q;

    int left = 1, right = 2;
    char h;
    int t;
    int sum = 0;
    for(int i=0; i<q; i++){
        cin >> h >> t;

        if(h=='L'){
            if(left<right){
                if(left<=t && t<right){
                    sum += t - left;
                }else if(left>t){
                    sum += left - t;
                }else if(t>right){
                    sum += n - t + left;
                }
            }else{
                if(left>=t && t>right){
                    sum += left - t;
                }else if(left<t){
                    sum += t - left;
                }else if(t<right){
                    sum += n - left + t;
                }
            }

            left = t;
        }else{
            if(right<left){
                if(right<=t && t<left){
                    sum += t - right;
                }else if(right>t){
                    sum += right - t;
                }else if(t>left){
                    sum += n - t + right;
                }
            }else{
                if(right>=t && t>left){
                    sum += right - t;
                }else if(right<t){
                    sum += t - right;
                }else if(t<left){
                    sum += n - right + t;
                }
            }

            right = t;
        }
    }

    cout << sum << endl;

    return 0;
}

C: Prepare Another Box

解法

  • $A$ と $B$ を降順にソートして、おもちゃと箱のペアを貪欲に決定していく
ABC376C.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;

    vector<int> A(n), B(n-1);
    for(int i=0; i<n; i++){
        cin >> A[i];
    }
    for(int i=0; i<n-1; i++){
        cin >> B[i];
    }

    sort(A.rbegin(), A.rend());
    sort(B.rbegin(), B.rend());

    if(A.back()>B.back()){
        cout << -1 << endl;
        return 0;
    }

    int cnt = 0;
    int ans = A.back();
    int j = 0;
    for(int i=0; i<n-1; i++){
        if(A[i]>B[j]){
            cnt++;
            if(cnt>1){
                cout << -1 << endl;
                return 0;
            }
            ans = A[i];
        }else{
            j++;
        }
    }

    cout << ans << endl;

    return 0;
}

D: Cycle

解法

  • 頂点 $1$ への辺をもつ頂点を記録しておき、まず、頂点 $1$ への辺はないものとして考える
  • 幅優先探索 (BFS) で頂点 $1$ から各頂点までの最短距離を求める
  • 頂点 $1$ への辺をもつ各頂点について、(頂点 $1$ からの最短距離 $+ 1$ )の最小値が求める答えである
ABC376D.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int main(){
    int n, m;
    cin >> n >> m;

    vector<vector<int>> G(n);
    vector<int> V;
    int a, b;
    for(int i=0; i<m; i++){
        cin >> a >> b;
        a--;
        b--;
        if(b==0){
            V.push_back(a);
        }else{
            G[a].push_back(b);
        }
    }

    if(V.size()<1){
        cout << -1 << endl;
        return 0;
    }

    vector<int> dist(n);
    queue<int> Q;
    dist[0] = 0;
    Q.push(0);
    while(!Q.empty()){
        int now = Q.front();
        Q.pop();

        for(int i=0; i<G[now].size(); i++){
            int next = G[now][i];
            if(dist[next]){
                continue;
            }
            dist[next] = dist[now] + 1;
            Q.push(next);
        }
    }

    int minv = m + 1;
    for(int i=0; i<V.size(); i++){
        if(dist[V[i]]){
            minv = min(minv, dist[V[i]]+1);
        }
    }

    if(minv>m){
        cout << -1 << endl;
    }else{
        cout << minv << endl;
    }
    
    return 0;
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?