53472023-04-26 10:17:43Valaki2Színes szobák (50 pont)cpp14Accepted 50/5039ms7724 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 1e5;

int n, k, q;
int color[1 + maxn + 1];
vector<pii > steps;

bool stays(int idx, bool dir) {
    for(pii s : steps) {
        if(color[idx] == s.fi) {
            idx += s.se;
        }
        if((idx > n) && !dir) {
            return true; // wrong direction
        }
        if((idx > n) && dir) {
            return false;
        }
        if((idx < 1) && !dir) {
            return false; // correct way
        }
        if((idx < 1) && dir) {
            return true;
        }
    }
    return true; // actually stays
}

void solve() {
    cin >> n >> k >> q;
    for(int i = 1; i <= n; i++) {
        cin >> color[i];
    }
    for(int i = 1; i <= q; i++) {
        int a, b;
        cin >> a >> b;
        steps.pb(mp(a, b));
    }
    int l = 0, r = n + 1;
    while(l < r - 1) {
        int mid = (l + r) / 2;
        if(stays(mid, false)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    int ans = 0;
    ans += l;
    l = 0, r = n + 1;
    while(l < r - 1) {
        int mid = (l + r) / 2;
        if(stays(mid, true)) {
            l = mid;
        } else {
            r = mid;
        }
    }
    ans += n - l;
    cout << ans << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1892 KiB
2Accepted0/039ms5024 KiB
3Accepted1/13ms2460 KiB
4Accepted1/13ms2776 KiB
5Accepted1/13ms2992 KiB
6Accepted1/13ms3196 KiB
7Accepted1/13ms3408 KiB
8Accepted1/13ms3508 KiB
9Accepted1/13ms3584 KiB
10Accepted1/13ms3608 KiB
11Accepted1/13ms3840 KiB
12Accepted1/13ms3700 KiB
13Accepted1/14ms4108 KiB
14Accepted1/14ms4028 KiB
15Accepted1/14ms3992 KiB
16Accepted1/14ms4216 KiB
17Accepted1/16ms4340 KiB
18Accepted1/16ms4292 KiB
19Accepted1/121ms6136 KiB
20Accepted1/137ms7176 KiB
21Accepted1/128ms6480 KiB
22Accepted1/129ms7292 KiB
23Accepted1/135ms7304 KiB
24Accepted1/139ms7688 KiB
25Accepted1/137ms7724 KiB
26Accepted1/137ms7604 KiB
27Accepted3/337ms7608 KiB
28Accepted3/337ms7640 KiB
29Accepted3/337ms7552 KiB
30Accepted3/337ms7552 KiB
31Accepted3/337ms7552 KiB
32Accepted3/337ms7564 KiB
33Accepted4/419ms6212 KiB
34Accepted4/432ms7368 KiB