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 473

0
Last updated at Posted at 2026-08-30

A - Second Half Sum

問題文

O(N)
人数/2から合計を計算しましょう。

C++
    for(int i=n/2; i<n; i++){
        ans += a[i];
    }
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);
    rep(i, n) cin >> a[i];
    int ans = 0;
    for(int i=n/2; i<n; i++){
        ans += a[i];
    }
    cout << ans << endl;
    return 0;
} 

B - Old Maid

問題文

O(不明)
1~Nまでループする。
iの要素を同じ要素がないか判定して、存在していたら2つの要素を削除する。
存在していないなら答えの変数に加算する。

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;
    map<int, int> mp;
    rep(i, n){
        int a;
        cin >> a;
        mp[a]++;
        if(mp[a]>=2) mp[a]-=2;
    }
    int ans = 0;
    for(auto [k, v]:mp){
        ans += k * v;
    }
    cout << ans << endl;
    return 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;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    int ans = 0;
    while(a.size()){
        int v = a.back();
        a.pop_back();
        int nn = a.size();
        for(int i=0; i<nn; i++){
            if(v == a[i]){
                a.erase(a.begin() + i);
                break;
            }
        }
        if(nn == a.size()){
            ans += v;
        }
    }
    cout << ans << endl;
    return 0;
} 

公式解説のこのコーディング方法が本回答だと思います。

C - Change Schools

問題文

O(NlogN)
コンテスト中は、なぜか二分探索で回答しました。
所属するクラスの人数の配列の要素_i+1より大きい要素が存在するかを判定しました。

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, k;
    cin >> n >> k;
    vector<int> a(k+1);
    rep(i, n){
        int aa;
        cin >> aa;
        a[aa]++;
    }
    multiset<int> mst;
    for(int i=1; i<=k; i++){
        mst.insert(a[i]);
    }
    int ans = 0;
    for(int i=1; i<=k; i++){
        if(mst.upper_bound(a[i]+1) == mst.end()) ans++;
    }
    cout << ans << endl;
    return 0;
} 

二分探索が余計でしたね。
O(N)
人数が最大のクラスの人数-1をmとする。
m-1以上の人数のクラスがいくつあるか探索して答えを出す。

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, k;
    cin >> n >> k;
    vector<int> a(k+1);
    rep(i, n){
        int aa;
        cin >> aa;
        a[aa]++;
    }
    int m = 0;
    for(int i=1; i<=k; i++){
        m = max(m, a[i]);
    }
    int ans = 0;
    for(int i=1; i<=k; i++){
        if(a[i] >= m-1) ans++;
    }
    cout << ans << 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?