Submission #1298590

#TimeUsernameProblemLanguageResultExecution timeMemory
1298590dzhoz0 Martian DNA (BOI18_dna)C++20
100 / 100
73 ms9396 KiB
/*
    it could have been better :)
    it will next time ;)
*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define INF 1e18
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>

const int MOD = 1'000'000'000 + 7;

void setIO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
#ifdef LOCAL
    freopen("inp.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#else
    if (!name.empty())
    {
        freopen((name + ".INP").c_str(), "r", stdin);
        freopen((name + ".OUT").c_str(), "w", stdout);
    }
#endif
}

// START OF DEBUG
#define db(val) "["#val" = "<<(val)<<"] "
#define print_op(...) ostream& operator<<(ostream& out, const __VA_ARGS__& u)
// for printing std::pair
template<class U, class V> print_op(pair<U, V>) {
    return out << "(" << u.first << ", " << u.second << ")";
}
// for printing collection
template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator<<(ostream& out, const Con& con) { 
    out << "{";
    for (auto beg = con.begin(), it = beg; it != con.end(); ++it)
        out << (it == beg ? "" : ", ") << *it;
    return out << "}";
}
// for printing std::tuple
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
    if constexpr(i == tuple_size<T>::value) return out << ")"; 
    else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); 
}
template<class ...U> print_op(tuple<U...>) {
    return print_tuple_utils<0, tuple<U...>>(out, u);
}
// END OF DEBUG

const int MAXN = 2e5;
int n, k, q; 
int a[MAXN + 5];
int req[MAXN + 5];
int cnt[MAXN + 5];

void solve()
{
    cin >> n >> k >> q;

    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 0; i <= k; i++) req[i] = INF;
    for(int i = 1; i <= q; i++) {
        int b; cin >> b;
        cin >> req[b];
    }

    int l = 1, r = 1;
    set<int> s;
    while(r <= n) { 
        cnt[a[r]]++;
        if(cnt[a[r]] >= req[a[r]]) s.insert(a[r]);
        if(s.size() == q) break;
        r++;
    }

    int res = r - l + 1;

    if(r == n + 1) {
        cout << "impossible\n";
        return;
    }

    cnt[a[r]]--;
    if(cnt[a[r]] < req[a[r]]) s.erase(a[r]);

    while(r <= n) {
        cnt[a[r]]++;
        if(cnt[a[r]] >= req[a[r]]) s.insert(a[r]);
        while(l <= r) {
            cnt[a[l]]--;
            if(cnt[a[l]] < req[a[l]]) s.erase(a[l]);
            if(s.size() == q) l++;
            else {
                cnt[a[l]]++;
                if(cnt[a[l]] >= req[a[l]]) s.insert(a[l]);
                break;
            }
        }
        res = min(res, r - l + 1);

        r++;
    }

    cout << res << '\n';
}

signed main()
{
    setIO();
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
}

Compilation message (stderr)

dna.cpp: In function 'void setIO(std::string)':
dna.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen((name + ".INP").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dna.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen((name + ".OUT").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...