QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#286586#5592. Family Visitsmfshi03TL 17ms9140kbPython31.3kb2023-12-18 05:18:462023-12-18 05:18:47

Judging History

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

  • [2023-12-18 05:18:47]
  • 评测
  • 测评结果:TL
  • 用时:17ms
  • 内存:9140kb
  • [2023-12-18 05:18:46]
  • 提交

answer

# This solution is wrong the smallest solution on kattis is around 700 lines idk?
import sys
sys.setrecursionlimit(1000000)

n, d = map(int, input().split())

mess = [0 for i in range(n)]
times = [0 for i in range(n)]
prefix = [0 for i in range(n + 1)]

for i in range(n):
    m, c = map(int, input().split())
    mess[i] = m
    prefix[i+1] = prefix[i] + m
    times[i] = c

cache = dict()

def recur(start, i, target, count):
    if target <= 0:
        return count 

    if i < start:
        return float("inf")

    if (i, target, count) in cache:
        return cache[(i, target, count)]


    removed = min(times[i], prefix[i+1] - prefix[start])
    
    cache[(i, target, count)] = min(recur(start, i - 1, target - removed, count + 1), recur(start, i - 1, target, count))

    return cache[(i, target, count)]
    

cdays = 0
start = 0
np = False
days = []

for i in range(d):
    days.append(int(input()))
    
for day in days:

    if times[day - 1] < mess[day - 1]:
        np = True
        break

    clean_days = recur(start, day - 1, prefix[day] - prefix[start], 0)
    #print(clean_days)
    #print()

    if clean_days == float("inf"):
        np = True
        break

    cdays += clean_days
    start = day

if np:
    print(-1)
else:
    print(cdays)








Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 13ms
memory: 8972kb

input:

6 2
1 2
2 1
1 4
3 2
3 6
2 3
3
6

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 12ms
memory: 9132kb

input:

10 5
12 10
0 2
7 1
1 8
3 4
3 4
2 3
1 2
10 1
7 5
2
4
5
6
8

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 5ms
memory: 9032kb

input:

10 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
10

output:

10

result:

ok single line: '10'

Test #4:

score: 0
Accepted
time: 11ms
memory: 9068kb

input:

3 1
10 10
10 10
10 0
2

output:

2

result:

ok single line: '2'

Test #5:

score: 0
Accepted
time: 17ms
memory: 9036kb

input:

12 1
1 1
1 1
1 1
0 4
1 1
1 1
1 1
0 6
1 1
1 1
1 1
0 4
12

output:

2

result:

ok single line: '2'

Test #6:

score: 0
Accepted
time: 10ms
memory: 9136kb

input:

6 1
1 0
1 0
1 2
1 0
1 0
1 2
6

output:

-1

result:

ok single line: '-1'

Test #7:

score: 0
Accepted
time: 16ms
memory: 9140kb

input:

9 1
1 0
1 0
1 9
1 0
1 0
1 2
1 0
1 0
1 3
9

output:

-1

result:

ok single line: '-1'

Test #8:

score: 0
Accepted
time: 16ms
memory: 9040kb

input:

6 1
1 0
1 0
1 2
1 0
1 0
1 4
6

output:

2

result:

ok single line: '2'

Test #9:

score: 0
Accepted
time: 5ms
memory: 9016kb

input:

6 1
0 0
0 1
0 2
0 0
0 1
0 2
6

output:

0

result:

ok single line: '0'

Test #10:

score: -100
Time Limit Exceeded

input:

999 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 53...

output:


result: