← Real problems

noip10p1

easysimulationdequeStatement on DMOJ

CP Lang

12 lines0.3 spec
// noip10p1 Machine Translation: a cache keeps the m most recently added words, evicting the
// oldest. Count the lookups that miss.
let (m, n) = read<(1..=100, 1..=1_000)>()
var cache: Deque<Int> = Deque()
var misses = 0
for w in read<[n]0..=1_000>():
    if w not in cache:
        misses += 1
        cache.push(w)
        if cache.len > m:
            cache.pop_front()
print(misses)

Accepted C++

62 lines · 0.04 s on the judge
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

void solve() {
    int m, n;
    cin >> m >> n;
    int arr[n];
    for (auto& a : arr) {
        cin >> a;
    }
    int ans = 0;
    set<int> vis;
    deque<int> q;
    for (auto& a : arr) {
        if (vis.count(a)) {
            continue;
        }
        ans++;
        q.push_back(a);
        vis.insert(a);
        while (sz(q) > m) {
            vis.erase(q.front());
            q.pop_front();
        }
    }
    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    solve();
}