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?

AtCoder Beginner Contest 373

Last updated at Posted at 2024-10-06

A - September

O(N)
シミュレートしましょう。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int N = 12;
    vector<string> S(N);
    rep(i, N) cin >> S[i];

    int ans = 0;
    rep(i, N){
        if(i + 1 == S[i].size()){
            ans++;
        }
    }
    cout << ans << endl;

    return 0;
} 

B - 1D Keyboard

O(N)
問題文の意味を理解するのが難しいです。
シミュレートしましょう。
AからB、BからCとindexを移動させる時、

ans += abs(i - j)

とindexの差分の絶対値を合計して出力します。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int N = 26;
    string S;
    cin >> S;

    int index = 0;
    rep(i, N){
        if('A' == S[i]) index = i;
    }

    int ans = 0; 
    rep(i, N){
        rep(j, N){
            if(S[index] + 1 == S[j]){
                ans += abs(j-index);  
                index = j;
                break;
            }
        }
    }
    cout << ans << endl;

    return 0;
} 

C - Max Ai+Bj

O(N)
Aの最も大きな要素、Bの最も大きな要素を加算して出力します。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    rep(i, N) cin >> A[i];
    rep(i, N) cin >> B[i];
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    cout << A[N-1] + B[N-1] << endl;
    return 0;
} 

D - Hidden Weights

O(N)
BFSです。
「j番目の有向辺は頂点u_jから頂点v_jに向かっており、重みw_jを持っています。
x_v_j − x_u_j = w_jが成り立つ。」
と記載されています。
uからvはコストwであり、vからuへはコスト-wです。

C++
rep(i, M){
    ll u, v, w;
    cin >> u >> v >> w;
    u--; v--;
    graph[u].push_back({v, w});
    graph[v].push_back({u, -w});
}

BFSで始点のコストを0にします。
繋がっている辺毎に一つずつ探索していきます。
一度通った頂点は探索から外します。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<pair<ll, ll>>> graph(N);
    rep(i, M){
        ll u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        graph[u].push_back({v, w});
        graph[v].push_back({u, -w});
    }

    vector<ll> cost(N, 0);
    vector<bool> used(N, false);
    for(int i=0; i<N; i++){
        if(used[i]) continue;

        used[i] = true;
        queue<int> que;
        que.push(i);

        while(que.size()){
            int p = que.front();
            que.pop();
            for(auto g:graph[p]){
                if(used[g.first] == false){
                    used[g.first] = true;
                    cost[g.first] = cost[p] + g.second;
                    que.push(g.first);
                }
            }
        }
    }

    for(int i=0; i<N; i++){
        cout << cost[i];
        if(i<N-1) cout << ' ';
        else cout << 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?