Submission #170009

#TimeUsernameProblemLanguageResultExecution timeMemory
170009WLZWatching (JOI13_watching)C++14
100 / 100
653 ms16308 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;

int n, p, q;
vector<int> a;

int check(int w) {
  vector< vector<int> > dp(n + 1, vector<int>(min(n, p) + 1, INF));
  dp[0][0] = 0;
  int x = 0, y = 0;
  for (int i = 0; i < n; i++) {
    while (x < n && a[x] - a[i] + 1 <= w) {
      x++;
    }
    while (y < n && a[y] - a[i] + 1 <= 2 * w) {
      y++;
    }
    for (int j = 0; j < min(n, p); j++) {
      dp[x][j + 1] = min(dp[x][j + 1], dp[i][j]);
      dp[y][j] = min(dp[y][j], dp[i][j] + 1);
    }
    dp[y][min(n, p)] = min(dp[y][min(n, p)], dp[i][min(n, p)] + 1);
  }
  for (int i = 0; i <= min(n, p); i++) {
    if (dp[n][i] <= q) {
      return 1;
    }
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> p >> q;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  int lo = 1, hi = (int) 1e9, ans;
  while (lo <= hi) {
    int mid = lo + (hi - lo) / 2;
    if (check(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans << '\n';
  return 0;
}

Compilation message (stderr)

watching.cpp: In function 'int main()':
watching.cpp:55:18: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   cout << ans << '\n';
                  ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...