제출 #1347481

#제출 시각아이디문제언어결과실행 시간메모리
1347481DangKhoizzzz Martian DNA (BOI18_dna)C++20
100 / 100
16 ms4636 KiB
#include <bits/stdc++.h>
#define int long long
#define arr3 array <int , 3>
#define pii pair <int , int>
#define fi first
#define se second
#define BIT(x , k) ((x >> k)&1)
#define MASK(x) (1 << x)

using namespace std;

const int maxn = 2e6 + 7;
const int INF = 1e18;

int n , a[maxn] , cnt[maxn] , no = 0 , k , r , req[maxn];

void solve()
{
    cin >> n >> k >> r;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= r; i++)
    {
        int b , q; cin >> b >> q;
        if(req[b] == 0) no++;
        req[b] = max(req[b] , q);
    }
    int j = 1;
    int ans = INF;
    for(int i = 1; i <= n; i++)
    {
        no -= (cnt[a[i]] < req[a[i]]);
        cnt[a[i]]++;
        no += (cnt[a[i]] < req[a[i]]);
        if(no > 0) continue;

        while(cnt[a[j]] > req[a[j]])
        {
            
            cnt[a[j]]--;
            j++;
        }
        if(no == 0) ans = min(ans , i - j + 1);
    }
    if(ans == INF) cout << "impossible" << '\n';
    else cout << ans << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    //freopen("inp.txt" , "r" , stdin); freopen("out.txt" , "w", stdout);
    //int tc; cin >> tc; while(tc--) solve();
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...