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?

競プロ日記#25/07/12、07/19

Posted at

AtCoder

APG4b

EX23 - 最頻値

  • mapの「Key」と「Value」でそれぞれ「値」と「登場回数」を管理する
  • 今回はせっかく教科書ページでunordered_mapの話が出ていたので使ってみた。
int main() {
    long long N;
    cin >> N;
    unordered_map<long long, long long> Score;
    for (long long i = 0; i < N; i++) {
        long long ScoreValue;
        cin >> ScoreValue;
        Score[ScoreValue]++;
    }
    long long MaxScore = 0;
    long long MaxKey = 0;
    for (const auto& [key, value] : Score) {
        if (value > MaxScore) {
            MaxScore = value;
            MaxKey = key;
        }
    }

    cout << MaxKey << " " << MaxScore << endl;
}

AtCoder Beginner Contest 395

C - Shortest Duplicate Subarray

  • シンプルに連想配列で出現した値をKey,インデックスをValueとして管理していく。
  • サイズが2以上の時は複数回同じ数値が出現したということで長さを求める。その最小値をminimum変数で管理する。
  • ちなみに解説では
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;

int main() {
    long long N;
    cin >> N;
    long long minimum = LLONG_MAX;
    map<long long, vector<long long>> mp;
    for (long long i = 1; i <= N; i++) {
        long long A;
        cin >> A;
        mp[A].push_back(i);
        if (mp[A].size() > 1){
            long long first = mp[A][mp[A].size() - 1];
            long long second = mp[A][mp[A].size() - 2];
            if (first - second + 1 < minimum) {
                minimum = first - second + 1;
            }
        }
    }
    if (minimum != LLONG_MAX) {
        cout << minimum << endl;
        return 0;
    }
    cout << -1 << endl;
}
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?