神金的二分做法WA #5 & #8.玄关求条
查看原帖
神金的二分做法WA #5 & #8.玄关求条
1340182
Terminal_P楼主2025/2/4 13:27

rt, 80pts Unaccepted
Wrong Answer on #5 & #8
Wrong Answer记录(R201245748)
时间复杂度应该是O(Blog2B)O(B\log_2B)(?)

#include <bits/stdc++.h>
#define int long long
#define endl "\n"  // Line2-3 坏习惯但懒得改
using namespace std;
const int N = 1e5 + 5;
int n, k, b;
vector<int> a; // 储存坏掉路灯的编号
int search(int x) { // 找出最后一个小于等于x的元素的下标
  int idx = lower_bound(a.begin(), a.end(), x) - a.begin();
  if (a[idx] > x) {
    return idx - 1;
  } else {
    return idx;
  }
}
signed main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  // freopen("P3662.in", "r", stdin);
  // freopen("P3662.out", "w", stdout);
  cin >> n >> k >> b;
  for (int i = 1; i <= b; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  sort(a.begin(), a.end()); // 排序
  // 思路:从每个坏掉的路灯往后一个完好的路灯开始搜索肯定是较优的
  int ans = search(k); // 从第一个路灯还是搜索
  for (int i = 0; i < a.size(); i++) { // 对于每个坏掉的路灯:
    if (n - a[i] < k) { // 如果编号太大
      break; // 结束
    } else { // 否则:
      ans = min(ans, search(a[i] + k) - i); // 更新答案
      if (ans == 0) {
        break; // 如果最优为0结束搜索
      }
    }
  }
  cout << ans << endl; // 输出
  return 0;
}

dalao教教我哪里错了plz :(

2025/2/4 13:27
加载中...