2
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?

【ABC413】AtCoder Beginner Contest 413【C++】

Posted at

コンテスト名

デンソークリエイトプログラミングコンテスト2025(AtCoder Beginner Contest 413)

コンテストURL

開催日

2025/07/05 21:00–22:40


A: Content Too Large

解法

  • 問題文通りに判定する
ABC413A.cpp
#include <iostream>
using namespace std;

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

    int sum = 0;
    int a;
    for(int i=0; i<n; i++){
        cin >> a;
        sum += a;
    }

    if(sum<=m){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }

    return 0;
}

B: cat 2

解法

  • set<string> で連結した結果の文字列を管理する
ABC413B.cpp
#include <iostream>
#include <vector>
#include <string>
#include <set>
using namespace std;

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

    string s;
    vector<string> V;
    for(int i=0; i<n; i++){
        cin >> s;
        V.push_back(s);
    }

    set<string> S;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(i==j){
                continue;
            }

            string t = V[i] + V[j];
            S.insert(t);
        }
    }

    cout << S.size() << endl;

    return 0;
}

C: Large Queue

解法

  • queue<pair<long long int, long long int>> で $x$ と $c$ の組を管理する
ABC413C.cpp
#include <iostream>
#include <queue>
using namespace std;

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

    queue<pair<long long int, long long int>> Q;
    long long int num, c, x, k;
    int idx = 0;
    for(int i=0; i<q; i++){
        cin >> num;

        if(num==1){
            cin >> c >> x;
            Q.emplace(x, c);
        }else if(num==2){
            cin >> k;
            long long int sum = 0;
            while(k!=0){
                auto [x, c] = Q.front();
                if(k>=c){
                    sum += x * c;
                    k -= c;
                    Q.pop();
                }else{
                    sum += x * k;
                    Q.front().second -= k;
                    k = 0;
                }
            }

            cout << sum << '\n';
        }
    }
}

D: Make Geometric Sequence

解法

  • すべての項の絶対値が等しい場合とそれ以外で場合分けする
  • 絶対値の昇順にソートする
  • 等比数列であるとき、 $B_i \times B_{i+2} = {B_{i+1}}^2$ が成り立つ
ABC413D.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

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

    while(t--){
        int n;
        cin >> n;

        vector<pair<long long int, long long int>> A;
        int pluscnt = 0, minuscnt = 0;
        set<long long int> S;
        long long int a;
        for(int i=0; i<n; i++){
            cin >> a;

            if(a<0){
                minuscnt++;
                S.insert(-a);
                A.emplace_back(-a, a);
            }else{
                pluscnt++;
                S.insert(a);
                A.emplace_back(a, a);
            }
        }

        bool flag = true;
        if(n>2){
            if(S.size()==1){
                if(pluscnt>0 && minuscnt>0 && min(pluscnt, minuscnt)!=n/2){
                    flag = false;
                }
            }else{
                sort(A.begin(), A.end());
                for(int i=0; i<n-2; i++){
                    if(A[i].second*A[i+2].second!=A[i+1].second*A[i+1].second){
                        flag = false;
                        break;
                    }
                }
            }
        }

        if(flag){
            cout << "Yes" << '\n';
        }else{
            cout << "No" << '\n'; 
        }
    }

    return 0;
}
2
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
2
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?