赞
踩
目录
前段时间刚去参加了ccf考试,感受就是:ccf的题目比较扣细节,如果有一个细节没有照顾到,就不能全部AC。而且ccf考试是不能到看你哪里出错了的,你只能知道自己的第几条测试没有AC过。如果你觉得自己的思路是正确的,自己测试的结果也是正确的,建议再仔细读一遍题目,看看有哪些点是你漏看或者没有考虑到的。
这道题我刚看的时候有点慌,我以为这道题选定区域的时候是有交集的。我当时没想出怎么计算重合的区域,就先假设不会重合,写了一份代码,没想到竟然AC了。是我把这道题想复杂了。以下是我的代码:
- #田地丈量
- for i in range(n):
- x1, y1, x2, y2 = map(int, input().split())
- if x1 >= a or y1 >= b or x2 <= 0 or y2 <= 0:
- continue
- if 0 <= x1 <= a:
- stx = x1
- elif x1 < 0:
- stx = 0
- if 0 <= y1 <= b:
- sty = y1
- elif y1 < 0:
- sty = 0
- if 0 < x2 <= a:
- enx = x2
- elif x2 > a:
- enx = a
- if 0 < y2 <= b:
- eny = y2
- elif y2 > b:
- eny = b
- sum_ += (enx-stx)*(eny-sty)
- print(sum_)

这道题如果通过暴力的方式,拿70分还是很简单的,但是后面的测试会超时。
如果想要拿100分,就要思考如何优化代码。以下是我考试的时候AC过的代码:
- from collections import Counter
-
- n, m, k = map(int, input().split())
-
- blocks = Counter()
- min_t = float('inf')
- max_t = 0
- for _ in range(n):
- t, c = map(int,input().split())
- if t < min_t:
- min_t = t
- if t > max_t:
- max_t = t
- blocks[t] += c
- #print(blocks)
-
- cost = 0
- temp = 0
- for i in range(max_t,k-1,-1):
- if m - cost < blocks[i]:
- print(i)
- break
- if i in blocks:
- temp += blocks[i]
- cost += (blocks[i] + temp)
- else:
- print(k)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。