首页 > 代码库 > [FZU2203] 单纵大法好(二分)

[FZU2203] 单纵大法好(二分)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2203

二分答案,即二分m表示第m个不会被击中,那么被击中的炮弹为第m+1个。每次check就行了。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <cassert>
 24 #include <cstdio>
 25 #include <bitset>
 26 #include <vector>
 27 #include <deque>
 28 #include <queue>
 29 #include <stack>
 30 #include <ctime>
 31 #include <set>
 32 #include <map>
 33 #include <cmath>
 34 using namespace std;
 35 #define fr first
 36 #define sc second
 37 #define cl clear
 38 #define BUG puts("here!!!")
 39 #define W(a) while(a--)
 40 #define pb(a) push_back(a)
 41 #define Rint(a) scanf("%d", &a)
 42 #define Rll(a) scanf("%lld", &a)
 43 #define Rs(a) scanf("%s", a)
 44 #define Cin(a) cin >> a
 45 #define FRead() freopen("in", "r", stdin)
 46 #define FWrite() freopen("out", "w", stdout)
 47 #define Rep(i, len) for(int i = 0; i < (len); i++)
 48 #define For(i, a, len) for(int i = (a); i < (len); i++)
 49 #define Cls(a) memset((a), 0, sizeof(a))
 50 #define Clr(a, x) memset((a), (x), sizeof(a))
 51 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
 52 #define lrt rt << 1
 53 #define rrt rt << 1 | 1
 54 #define pi 3.14159265359
 55 #define RT return
 56 #define lowbit(x) x & (-x)
 57 #define onenum(x) __builtin_popcount(x)
 58 typedef long long LL;
 59 typedef long double LD;
 60 typedef unsigned long long ULL;
 61 typedef pair<int, int> pii;
 62 typedef pair<string, int> psi;
 63 typedef pair<LL, LL> pll;
 64 typedef map<string, int> msi;
 65 typedef vector<int> vi;
 66 typedef vector<LL> vl;
 67 typedef vector<vl> vvl;
 68 typedef vector<bool> vb;
 69 
 70 
 71 const int maxn = 200200;
 72 int n, k, a, m, x[maxn];
 73 int tmp[maxn];
 74 
 75 int ok(int m) {
 76     int tot = 0;
 77     for(int i = 1; i <= m; i++) tmp[i] = x[i];
 78     sort(tmp+1, tmp+m+1);
 79     tmp[0] = 0; tmp[m+1] = n + 1;
 80     for(int i = 0; i <= m; i++) {
 81         tot += (tmp[i+1] - tmp[i]) / (a + 1);
 82     }
 83     return tot >= k;
 84 }
 85 
 86 signed main() {
 87     // freopen("in", "r", stdin);
 88     while(~scanf("%d%d%d%d",&n,&k,&a,&m)) {
 89         for(int i = 1; i <= m; i++) scanf("%d", &x[i]);
 90         if(ok(m)) {
 91             puts("-1");
 92             continue;
 93         }
 94         int ret = 0;
 95         int lo = 1, hi = m;
 96         while(lo <= hi) {
 97             int mid = (lo + hi) >> 1;
 98             if(ok(mid)) {
 99                 ret = mid;
100                 lo = mid + 1;
101             }
102             else hi = mid - 1;
103         }
104         printf("%d\n", ret + 1);
105     }
106     return 0;
107 }

 

[FZU2203] 单纵大法好(二分)