Submission #399756

# Submission time Handle Problem Language Result Execution time Memory
399756 2021-05-06T14:34:48 Z Sorting Martian DNA (BOI18_dna) C++17
100 / 100
39 ms 2720 KB
#include <bits/stdc++.h>

using namespace std;
template<typename T> void check_min(T &a, const T &b){ a = (a < b) ? a : b; }
template<typename T> void check_max(T &a, const T &b){ a = (a > b) ? a : b; }
typedef long long ll;
#define all(x) (x).begin(), (x).end()
int read_int();

const int N = 2e5 + 3;

int n, k, r, a[N];
int mn[N], cnt[N], cnt_ok;

void add(int x){
    ++cnt[x];
    if(cnt[x] == mn[x]) ++cnt_ok;
}

void rem(int x){
    if(cnt[x] == mn[x]) --cnt_ok;
    --cnt[x];
}

bool check(int mid){
    fill(cnt, cnt + k, 0);
    cnt_ok = k - r;
    for(int i = 0; i < mid; ++i)
        add(a[i]);

    if(cnt_ok == k) return true;

    for(int i = mid; i < n; ++i){
        add(a[i]);
        rem(a[i - mid]);
        if(cnt_ok == k) return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    n = read_int(), k = read_int(), r = read_int();
    for(int i = 0; i < n; ++i)
        a[i] = read_int();
    for(int i = 0; i < r; ++i){
        int b, q;
        b = read_int(), q = read_int();
        mn[b] = q;
    }

    int l = 1, r = n + 1;
    while(l != r){
        int mid = (l + r) >> 1;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }

    if(l == n + 1) cout << "impossible\n";
    else cout << l << "\n";
}

int ptr = N - 1;
char buff[N];

void next_char(){ if(++ptr == N) fread(buff, 1, N, stdin), ptr = 0; }

int read_int(){
    int ret = 0;
    for(; buff[ptr] < '0' || buff[ptr] > '9'; next_char());
    for(; '0' <= buff[ptr] && buff[ptr] <= '9'; next_char())
        ret = 10 * ret + buff[ptr] - '0';
    return ret;
}

Compilation message

dna.cpp: In function 'void next_char()':
dna.cpp:68:39: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   68 | void next_char(){ if(++ptr == N) fread(buff, 1, N, stdin), ptr = 0; }
      |                                  ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 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 336 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 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 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 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 336 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 1308 KB Output is correct
2 Correct 17 ms 1228 KB Output is correct
3 Correct 9 ms 1228 KB Output is correct
4 Correct 15 ms 1312 KB Output is correct
5 Correct 21 ms 1656 KB Output is correct
6 Correct 14 ms 1308 KB Output is correct
7 Correct 14 ms 1316 KB Output is correct
8 Correct 29 ms 2104 KB Output is correct
9 Correct 19 ms 1372 KB Output is correct
10 Correct 21 ms 1228 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 1 ms 332 KB Output is correct
19 Correct 1 ms 332 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 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1936 KB Output is correct
2 Correct 28 ms 1740 KB Output is correct
3 Correct 39 ms 1780 KB Output is correct
4 Correct 3 ms 1228 KB Output is correct
5 Correct 21 ms 2088 KB Output is correct
6 Correct 38 ms 2720 KB Output is correct
7 Correct 19 ms 1380 KB Output is correct
8 Correct 33 ms 1448 KB Output is correct
9 Correct 18 ms 1312 KB Output is correct
10 Correct 19 ms 1232 KB Output is correct
11 Correct 9 ms 1228 KB Output is correct
12 Correct 16 ms 1308 KB Output is correct
13 Correct 21 ms 1660 KB Output is correct
14 Correct 14 ms 1308 KB Output is correct
15 Correct 14 ms 1228 KB Output is correct
16 Correct 29 ms 2004 KB Output is correct
17 Correct 18 ms 1364 KB Output is correct
18 Correct 20 ms 1308 KB Output is correct
19 Correct 1 ms 332 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 352 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct