[HDU] [4907] Task Schedule

Problem Description

Loading...

Problem Analysis

Since the data range in this problem is very small
the marking approach is easier to understand

If you use Binary Search
first collect all the free times
then use Binary Search to find
the free time that is greater than q and closest to q

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[200200],b[200200];
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=1; i<=n; i++) {
            int t;
            scanf("%d",&t);
            a[t]=1;
        }
        int cnt=0;
        for(int i=0; i<200020; i++)
            if(!a[i])
                b[cnt++]=i;
        while(m--) {
            int t;
            scanf("%d",&t);
            if(!a[t]) {
                printf("%d\n",t);
                continue;
            }
            int l=0,r=cnt;
            while(l<=r) {
                int mid=(l+r)/2;
                if(b[mid]>=t)
                    r=mid-1;
                else
                    l=mid+1;
            }
            printf("%d\n",b[l]);
        }
    }
    return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...