답안 #485478

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485478 2021-11-08T01:06:06 Z sam571128 Martian DNA (BOI18_dna) C++17
100 / 100
38 ms 6724 KB
#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 2e5+5, MOD = 1e9+7;
int val[N], cnt[N], arr[N];
int ans = 0;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int fastpow(int n, int p){
    int res = 1;
    while(p){
        if(p&1) res = res * n % MOD;
        n = n * n % MOD;
        p >>= 1;
    }
    return res;
}

int hsh(int x){
    if(cnt[x] <= 0) return 0;
    return cnt[x] * val[x];
}

bool check(int x){
    int tmp = ans;
    ans = ((ans-hsh(x))%MOD+MOD)%MOD;
    cnt[x]--;
    ans = ((ans+hsh(x))%MOD+MOD)%MOD;
    bool ok = (ans!=0);
    cnt[x]++;
    ans = tmp;
    return ok;
}

signed main(){
    fastio
    int n,k,R;
    cin >> n >> k >> R;
    for(int i = 0;i < n;i++){
        cin >> arr[i];
    }   

    for(int i = 0;i < R;i++){
        int x;
        cin >> x;
        val[x] = uniform_int_distribution<int>(0,1e9)(rng);
        cin >> cnt[x];
        ans += hsh(x);
    }

    int l = 0, r = -1, res = 1e18;
    while(l < n){
        while(r + 1 < n && check(arr[r+1])){
            ans = ((ans-hsh(arr[r+1]))%MOD+MOD)%MOD;
            cnt[arr[r+1]]--;
            ans = ((ans+hsh(arr[r+1]))%MOD+MOD)%MOD;
            r++;         
        }
        if(r+1 < n&&!check(arr[r+1])){
            res = min(res,r-l+2);
        }

        ans = ((ans-hsh(arr[l]))%MOD+MOD)%MOD;
        cnt[arr[l]]++;
        ans = ((ans+hsh(arr[l]))%MOD+MOD)%MOD;
        l++;
    }

    if(res==1e18) cout << "impossible\n";
    else cout << res << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 320 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 2252 KB Output is correct
2 Correct 16 ms 2252 KB Output is correct
3 Correct 17 ms 2252 KB Output is correct
4 Correct 20 ms 2268 KB Output is correct
5 Correct 20 ms 3680 KB Output is correct
6 Correct 15 ms 2252 KB Output is correct
7 Correct 15 ms 2396 KB Output is correct
8 Correct 22 ms 4672 KB Output is correct
9 Correct 18 ms 2892 KB Output is correct
10 Correct 14 ms 2252 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 0 ms 320 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 4852 KB Output is correct
2 Correct 28 ms 4376 KB Output is correct
3 Correct 23 ms 4044 KB Output is correct
4 Correct 16 ms 2252 KB Output is correct
5 Correct 37 ms 5488 KB Output is correct
6 Correct 38 ms 6724 KB Output is correct
7 Correct 19 ms 3020 KB Output is correct
8 Correct 21 ms 3392 KB Output is correct
9 Correct 14 ms 2288 KB Output is correct
10 Correct 16 ms 2296 KB Output is correct
11 Correct 15 ms 2324 KB Output is correct
12 Correct 21 ms 2272 KB Output is correct
13 Correct 20 ms 3656 KB Output is correct
14 Correct 18 ms 2260 KB Output is correct
15 Correct 23 ms 2508 KB Output is correct
16 Correct 22 ms 4704 KB Output is correct
17 Correct 18 ms 2892 KB Output is correct
18 Correct 15 ms 2292 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct