QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#820773#3167. ICPC Campumd-red#AC ✓3569ms136552kbJava114.0kb2024-12-19 01:22:412024-12-19 01:22:42

Judging History

你现在查看的是最新测评结果

  • [2024-12-19 01:22:42]
  • 评测
  • 测评结果:AC
  • 用时:3569ms
  • 内存:136552kb
  • [2024-12-19 01:22:41]
  • 提交

answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class G {

    // Define the Problem class with difficulty and index
    static class Problem {
        int difficulty;
        int index;

        public Problem(int difficulty, int index) {
            this.difficulty = difficulty;
            this.index = index;
        }
    }

    public static void main(String[] args) throws IOException {
        // Initialize BufferedReader for efficient input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // Read the first line and parse n, p, q, s
        String[] firstLine = br.readLine().trim().split("\\s+");
        int n = Integer.parseInt(firstLine[0]);
        int p = Integer.parseInt(firstLine[1]);
        int q = Integer.parseInt(firstLine[2]);
        int s = Integer.parseInt(firstLine[3]);

        // Read p classical problems
        Problem[] classical = new Problem[p];
        for (int i = 0; i < p; i++) {
            int difficulty = Integer.parseInt(br.readLine().trim());
            classical[i] = new Problem(difficulty, i);
        }

        // Read q creative problems
        Problem[] creative = new Problem[q];
        for (int i = 0; i < q; i++) {
            int difficulty = Integer.parseInt(br.readLine().trim());
            creative[i] = new Problem(difficulty, i);
        }

        // Sort creative problems in descending order of difficulty once before the loop
        Arrays.sort(creative, new Comparator<Problem>() {
            @Override
            public int compare(Problem a, Problem b) {
                return Integer.compare(b.difficulty, a.difficulty);
            }
        });

        // Define the comparator for the TreeSet (sorted by difficulty, then index)
        Comparator<Problem> problemComparator = new Comparator<Problem>() {
            @Override
            public int compare(Problem a, Problem b) {
                if (a.difficulty != b.difficulty) {
                    return Integer.compare(a.difficulty, b.difficulty);
                }
                return Integer.compare(a.index, b.index);
            }
        };

        // Initialize binary search boundaries
        int lower = 0;
        int upper = 1_000_000_001;

        // Binary search to find the minimal possible mid
        while (upper > lower) {
            int mid = lower + (upper - lower) / 2;

            // Initialize TreeSet with classical problems
            TreeSet<Problem> treeSet = new TreeSet<>(problemComparator);
            treeSet.addAll(Arrays.asList(classical));

            int here = 0;

            // Iterate over creative problems sorted in descending order
            for (Problem problem : creative) {
                // Calculate the limit using Math.min
                int limit = Math.min(s - problem.difficulty, problem.difficulty + mid);

                // Create a dummy problem to use with floor
                Problem key = new Problem(limit, Integer.MAX_VALUE);

                // Find the floor problem in the TreeSet
                Problem other = treeSet.floor(key);

                // Check if the found problem satisfies the difficulty constraint
                if (other != null && other.difficulty >= (problem.difficulty - mid)) {
                    treeSet.remove(other);
                    here++;
                }

                // Early exit if we've already found enough pairs
                if (here >= n) {
                    break;
                }
            }

            // Adjust binary search boundaries based on the number of pairs found
            if (here >= n) {
                upper = mid;
            } else {
                lower = mid + 1;
            }
        }

        // After binary search, check if a valid solution was found
        if (upper == 1_000_000_001) {
            System.out.println(-1);
        } else {
            System.out.println(upper);
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 64ms
memory: 52120kb

input:

3 4 5 10
3
4
4
9
0
1
5
6
6

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 74ms
memory: 51944kb

input:

4 4 4 15
1
5
10
12
1
3
10
14

output:

13

result:

ok single line: '13'

Test #3:

score: 0
Accepted
time: 56ms
memory: 55908kb

input:

4 4 4 10
1
12
5
10
1
10
3
14

output:

-1

result:

ok single line: '-1'

Test #4:

score: 0
Accepted
time: 62ms
memory: 51756kb

input:

4 4 4 30
1
5
10
12
1
3
10
14

output:

2

result:

ok single line: '2'

Test #5:

score: 0
Accepted
time: 72ms
memory: 51648kb

input:

4 5 5 10
1
3
3
4
9
0
2
5
7
8

output:

4

result:

ok single line: '4'

Test #6:

score: 0
Accepted
time: 63ms
memory: 51996kb

input:

3 3 3 4
1
1
3
3
1
0

output:

2

result:

ok single line: '2'

Test #7:

score: 0
Accepted
time: 66ms
memory: 51560kb

input:

3 3 3 1000000000
0
0
1000000000
0
1000000000
1000000000

output:

1000000000

result:

ok single line: '1000000000'

Test #8:

score: 0
Accepted
time: 64ms
memory: 53796kb

input:

1 1 1 0
1
1

output:

-1

result:

ok single line: '-1'

Test #9:

score: 0
Accepted
time: 65ms
memory: 56040kb

input:

3 5 5 1000000000
1
100
200
300
400
2
89
211
290
410

output:

10

result:

ok single line: '10'

Test #10:

score: 0
Accepted
time: 64ms
memory: 51960kb

input:

5 5 5 100
1
10
20
90
100
0
10
80
90
99

output:

100

result:

ok single line: '100'

Test #11:

score: 0
Accepted
time: 66ms
memory: 51728kb

input:

6 6 6 25
1
7
8
13
14
15
1
10
11
12
12
13

output:

5

result:

ok single line: '5'

Test #12:

score: 0
Accepted
time: 61ms
memory: 51836kb

input:

3 9 9 30
0
1
12
22
28
29
30
50
51
0
1
12
22
28
29
30
50
51

output:

0

result:

ok single line: '0'

Test #13:

score: 0
Accepted
time: 63ms
memory: 52168kb

input:

1 1 4 10
5
1
3
6
9

output:

2

result:

ok single line: '2'

Test #14:

score: 0
Accepted
time: 59ms
memory: 55872kb

input:

1 4 1 10
1
3
6
9
5

output:

2

result:

ok single line: '2'

Test #15:

score: 0
Accepted
time: 3331ms
memory: 134564kb

input:

200000 200000 200000 1000000000
107180
2049941
2059655
2054840
124091
103395
175331
184717
190908
113910
2041452
2078004
2009005
114559
2039747
112331
132171
2050441
2095717
2044077
137515
168621
2098897
2094290
169133
2005806
167333
2000616
2001240
193639
129155
2005697
2071739
192411
138391
122679...

output:

999799998

result:

ok single line: '999799998'

Test #16:

score: 0
Accepted
time: 3169ms
memory: 131688kb

input:

200000 200000 200000 1000000000
28505
999927038
999959123
999946674
999985091
999998850
64601
999938822
83467
999926212
999929514
999965309
23830
999902208
62506
28929
999980852
999978842
21242
33622
999953101
999962534
79705
999958415
94821
41799
999944987
999945039
999982939
999983899
24750
999943...

output:

1000000000

result:

ok single line: '1000000000'

Test #17:

score: 0
Accepted
time: 1896ms
memory: 126720kb

input:

100000 200000 200000 5
8
9
3
0
6
9
1
2
10
7
2
0
7
5
3
9
8
1
2
2
2
4
4
5
6
0
0
4
2
1
4
2
7
5
4
3
3
1
7
9
0
1
1
7
10
10
8
8
10
8
7
10
9
0
10
8
9
5
10
4
0
10
8
3
3
2
5
2
2
1
5
1
5
0
3
4
0
10
2
3
10
2
8
8
10
4
2
1
8
5
6
6
10
10
4
1
3
10
4
9
5
0
7
10
0
9
10
10
6
7
4
1
8
8
3
4
6
2
2
2
1
3
2
3
7
9
1
9
1
4
...

output:

5

result:

ok single line: '5'

Test #18:

score: 0
Accepted
time: 1979ms
memory: 116788kb

input:

199999 199999 200000 15
9
9
6
8
7
0
6
4
3
6
2
10
1
3
9
8
2
1
10
6
6
4
5
6
4
4
10
5
9
1
6
3
3
1
7
3
6
1
1
7
7
8
4
10
4
8
2
1
10
1
4
10
10
7
4
0
5
0
9
8
10
0
2
8
7
3
0
0
6
0
2
2
7
7
9
8
2
7
7
1
3
9
10
2
3
4
10
6
8
5
7
7
1
6
0
8
6
7
8
7
8
0
8
3
4
0
7
6
1
3
0
4
0
6
3
6
8
8
0
0
2
0
7
3
1
1
5
4
8
7
0
10
2...

output:

6

result:

ok single line: '6'

Test #19:

score: 0
Accepted
time: 1947ms
memory: 133636kb

input:

123456 199999 199998 8
6
2
6
9
3
6
4
6
2
1
6
4
2
2
10
0
0
9
0
6
6
4
0
6
1
8
2
7
10
4
7
10
6
9
9
7
8
9
5
2
6
1
5
6
8
5
0
7
7
6
10
6
4
7
0
4
2
10
2
0
3
0
0
5
9
9
8
10
8
10
6
4
4
8
8
1
6
5
9
3
2
10
0
2
1
2
9
4
7
10
5
7
10
0
4
0
2
9
3
6
5
6
3
4
9
2
9
3
1
4
7
0
7
8
6
0
2
0
2
1
5
0
10
2
5
7
4
9
3
1
10
0
5...

output:

4

result:

ok single line: '4'

Test #20:

score: 0
Accepted
time: 3373ms
memory: 133760kb

input:

50000 200000 200000 500000000
610081114
333776675
620871342
101159319
956662600
848856113
914422044
871150418
857693653
237952826
452171083
941020562
788779660
933849843
370996573
665703051
382775772
541623769
668187622
989573075
80576714
119482138
397167501
465954658
974080197
677300808
465978160
5...

output:

440033

result:

ok single line: '440033'

Test #21:

score: 0
Accepted
time: 3265ms
memory: 130864kb

input:

100000 200000 200000 1000000
299580
16063
159195
343047
944042
482948
579566
287027
451618
546265
5965
160723
633809
964578
931774
855004
275997
59319
134340
530241
252626
323189
692301
673911
212050
425338
80598
942779
807773
950882
829398
32143
600931
360293
362874
212340
423306
217524
110065
6374...

output:

3495

result:

ok single line: '3495'

Test #22:

score: 0
Accepted
time: 3403ms
memory: 119020kb

input:

100000 200000 200000 500000000
960443662
873041408
404555069
106990526
603252293
472278989
830670299
125195407
437835463
435199393
80837055
708379167
662398669
93287908
85628142
282832335
149954169
48126613
941899338
909389244
772513662
581135366
305138946
923788526
640981653
214003423
860316866
987...

output:

-1

result:

ok single line: '-1'

Test #23:

score: 0
Accepted
time: 3569ms
memory: 113432kb

input:

200000 200000 200000 1000000
470243
528561
6696
344466
119807
2446
688761
617955
272023
226518
847369
330136
831122
239795
78952
752961
240468
144078
428534
472930
301725
265335
6925
323452
644964
330293
39334
357985
34030
984809
660696
247780
780217
794610
717763
743584
139296
628181
522485
218638
...

output:

-1

result:

ok single line: '-1'

Test #24:

score: 0
Accepted
time: 3530ms
memory: 116652kb

input:

200000 200000 200000 999999997
656177006
663928089
288734542
812194455
528246170
396095199
638303668
28252340
370376338
840329146
637669185
840500136
550724800
375925403
537724438
573678960
365626221
506403797
494769518
346192348
398316477
45405373
968000839
597508258
658825271
728422488
483162327
6...

output:

999999301

result:

ok single line: '999999301'

Test #25:

score: 0
Accepted
time: 3336ms
memory: 116832kb

input:

200000 200000 200000 199999
108970
78774
113660
143317
58595
12575
64612
92095
199319
18543
174387
76672
144468
106113
56707
12424
31457
145604
186964
5915
170295
162143
191710
23614
57071
47939
41458
147518
195771
91412
20167
139954
22662
120665
105610
134015
114246
84581
172981
106691
26403
184192...

output:

199999

result:

ok single line: '199999'

Test #26:

score: 0
Accepted
time: 3492ms
memory: 117284kb

input:

200000 200000 200000 399998
41761
140955
63803
93009
146973
41259
162339
57248
191820
136655
149566
87633
196705
114044
122246
193843
159416
18630
156839
179119
49855
195715
131061
161932
197232
33824
109414
152286
35727
117651
115546
76057
50946
39112
139132
158357
157915
161314
185146
35578
185769...

output:

281

result:

ok single line: '281'

Test #27:

score: 0
Accepted
time: 3303ms
memory: 136552kb

input:

100000 200000 200000 2001493
717001
1964820
715027
502596
1985668
895754
1699895
1331591
93897
548120
1545704
1854023
158243
1074029
522643
1200949
983471
1652619
1680706
547590
1857870
1494590
574339
926985
1771994
562367
300752
169311
873211
1159740
1142537
218023
408385
24116
696752
1071968
63439...

output:

1083

result:

ok single line: '1083'

Test #28:

score: 0
Accepted
time: 3302ms
memory: 125092kb

input:

100000 200000 200000 200020155
6909443
61300203
34049524
94352407
86370208
36147229
93408915
46598105
165328377
116416482
17384349
1366231
58937409
171379303
70004596
61319228
78386651
23695282
188108171
146630069
87087059
88379483
20682128
169350047
43600478
6532310
132656207
10339658
54048141
3525...

output:

13600

result:

ok single line: '13600'

Test #29:

score: 0
Accepted
time: 3504ms
memory: 120692kb

input:

100000 200000 200000 999991471
994832549
2994795
824111195
608244455
32119530
867237100
78068620
876433090
107447894
227326957
282730686
804370382
456064468
646123413
667837039
351693944
565777492
448839676
20343890
649093416
829721156
468435291
246446112
984632737
534467633
349518735
340633294
3570...

output:

8803

result:

ok single line: '8803'

Test #30:

score: 0
Accepted
time: 3186ms
memory: 114756kb

input:

200000 200000 200000 399999
48348
26565
112390
57813
135900
144029
133963
177
154980
116351
158144
102897
161796
193782
116214
28107
20381
184357
41722
30366
123013
126479
179846
132067
83288
175120
148962
126906
83864
48624
138453
79573
88424
198591
22118
153577
71187
39411
91416
164907
199661
1634...

output:

1

result:

ok single line: '1'

Test #31:

score: 0
Accepted
time: 3119ms
memory: 128020kb

input:

100000 200000 200000 300000
145572
74218
191889
92608
18794
68887
90323
31040
132983
106213
86815
68402
63889
133765
175950
39876
10087
102965
53315
95409
163355
131849
76995
74221
49260
162362
123650
57313
30812
13399
76572
33753
42581
102172
129325
47883
166543
117149
156984
123790
128773
39410
14...

output:

0

result:

ok single line: '0'

Test #32:

score: 0
Accepted
time: 1392ms
memory: 112344kb

input:

199999 200000 200000 14
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
...

output:

0

result:

ok single line: '0'

Test #33:

score: 0
Accepted
time: 3016ms
memory: 123856kb

input:

1 200000 200000 5000000
313476209
41591220
348337424
655901727
3907213
863143495
384127805
503566331
247487203
122220730
520109785
755497744
890786361
538196014
470430548
587287424
319963662
665721721
930667327
234562055
920791943
344072045
821076819
626150954
428166094
532743833
391878205
524741820...

output:

88

result:

ok single line: '88'

Test #34:

score: 0
Accepted
time: 3377ms
memory: 122704kb

input:

43460 200000 200000 296788440
719592435
218149251
479235474
886185135
789131199
255526481
776561142
92893574
897244003
579441410
37744986
277129382
861854781
912109711
804298511
803535963
116296511
592316188
181727921
886235445
711912309
995786373
35135360
619378187
280918782
674246036
2476750
65471...

output:

136604297

result:

ok single line: '136604297'

Test #35:

score: 0
Accepted
time: 3436ms
memory: 121752kb

input:

59616 200000 200000 776637696
298627970
496714324
793506533
880268530
201384705
895000084
319849987
548836158
470024015
339278629
337298927
392732149
959241611
181959341
384143346
242837928
517041411
286155256
663955778
648013402
541535866
103769502
182110508
715705058
490196024
620707156
879646876
...

output:

8258

result:

ok single line: '8258'

Test #36:

score: 0
Accepted
time: 3238ms
memory: 122216kb

input:

75902 200000 200000 301527906
853288027
843540954
480625953
392476858
21612687
200214667
409551573
547749860
898283833
909627348
120904372
481933786
992850041
689967306
335865238
826739949
557542489
615997468
523646008
177997668
919221757
313108628
803389546
987776044
728618132
186466840
152007651
3...

output:

-1

result:

ok single line: '-1'