Submission #468447

# Submission time Handle Problem Language Result Execution time Memory
468447 2021-08-28T10:39:06 Z sumit_kk10 Watching (JOI13_watching) C++14
100 / 100
592 ms 16220 KB
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
#define ld long double
using namespace std;
const int N = 2e3 + 5;
const int MOD = 1e9 + 7;
int n, p, q, a[N], dp[N][N], top[N], toq[N];

int go(int i, int small, ll mid){
    if(small > p) return INT_MAX;
    if(i == n + 1) return 0;
    if(dp[i][small] != -1)
        return dp[i][small];
    int req = INT_MAX;
    req = min(req, go(top[i], small + 1, mid));
    req = min(req, go(toq[i], small, mid) + 1);
    return dp[i][small] = req;
}

bool check(ll mid){
    // let dp[i][j] denote the min no. of large cameras needed when we place atmost j small cameras
    // in the prefix of i.
    memset(dp, -1, sizeof dp);
    for(int i = 1; i <= n; ++i){
        int low = i + 1, high = n, res = n + 1;
        while(low <= high){
            int mid1 = (low + high) / 2;
            if(a[mid1] > a[i] + mid - 1){
                res = mid1;
                high = mid1 - 1;
            }
            else
                low = mid1 + 1;
        }
        top[i] = res;
        low = i + 1, high = n, res = n + 1;
        while(low <= high){
            int mid1 = (low + high) / 2;
            if(a[mid1] > a[i] + 2*mid - 1){
                res = mid1;
                high = mid1 - 1;
            }
            else
                low = mid1 + 1;
        }
        toq[i] = res;
    }
    return (go(1, 0, mid) <= q);
}

void solve(){
    cin >> n >> p >> q;
    for(int i = 1; i <= n; ++i)
        cin >> a[i];
    if(p + q >= n){
        cout << "1\n";
        return;
    }
    sort(a + 1, a + n + 1);
    ll low = 1, high = 1e12, ans;
    while(low <= high){
        ll mid = low + (high - low) / 2;
        if(check(mid)){
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
    cout << ans << "\n";
}

int main(){
    fast;
    int t = 1;
    // cin >> t;
    while(t--)
        solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 83 ms 15948 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 81 ms 15948 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 84 ms 15948 KB Output is correct
8 Correct 79 ms 16044 KB Output is correct
9 Correct 81 ms 15980 KB Output is correct
10 Correct 80 ms 16068 KB Output is correct
11 Correct 82 ms 16052 KB Output is correct
12 Correct 88 ms 16040 KB Output is correct
13 Correct 81 ms 16060 KB Output is correct
14 Correct 81 ms 16048 KB Output is correct
15 Correct 83 ms 15948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 15948 KB Output is correct
2 Correct 1 ms 204 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 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 88 ms 15996 KB Output is correct
8 Correct 134 ms 16068 KB Output is correct
9 Correct 249 ms 16060 KB Output is correct
10 Correct 592 ms 16172 KB Output is correct
11 Correct 163 ms 16220 KB Output is correct
12 Correct 480 ms 16084 KB Output is correct
13 Correct 85 ms 16076 KB Output is correct
14 Correct 83 ms 16076 KB Output is correct
15 Correct 91 ms 16076 KB Output is correct