b966: 第 3 題 線段覆蓋長度

最後編輯:2017-03-03 建立:2017-02-15 歷史紀錄

百千.IOhttps://zerojudge.tw/ShowProblem?problemid=b966

  • 黃柏睿while True:
  • try:
  • n=int(input())
  • lines=[]
  • for _ in range(n):
  • lines.append([int(x) for x in input().split()])
  • lines.sort()
  • length=0
  • for _ in range(n-1):
  • if lines[0][1]>=lines[1][0]:
  • lines[1]=[ lines[0][0], max(lines[0][1],lines[1][1]) ]
  • else:
  • length+=lines[0][1]-lines[0][0]
  • del lines[0]
  • length+=lines[0][1]-lines[0][0]
  • print(length)
  • except:break

百千.IO評分結果 score:70%

 

  • while True:
  • try:
  • S=set() # set()生一個空集合
  • N=int(input())
  • for _ in range(N):
  • L,R=input().split()
  • L,R=int(L),int(R)
  • for x in range(L,R):
  • S.add(x)
  • print(len(S))
  • except:
  • break

評分結果 score:70%

 

  • while True:
  • try:
  • S=set()
  • N=int(input())
  • for _ in range(N):
  • L,R=input().split()
  • L,R=int(L),int(R)
  • S=S|set(range(L,R)) # | 是聯集運算子
  • print(len(S))
  • except:
  • break

評分結果 score:70%

 

timeit.timeit( 'code' ) 回傳執行code一百萬次所花的秒數 (參數number預設是一百萬次)

  • from timeit import timeit
  • print(timeit('S=[False]*10'))
  • print(timeit('S=[False for _ in range(10)]'))
  • print(timeit('S=list(map(lambda x:False, range(10)))')) # 無名函數的參數是x 回傳值是False
    百千.IO但script中只執行一次的code是不必太計較啦

 

  • while True:
  • try:
  • S=[False]*1000000
  • N=int(input())
  • for _ in range(N):
  • L,R=input().split()
  • L,R=int(L),int(R)
  • S[L:R]=[True]*(R-L)
  • print(S.count(True))
  • except:
  • break

評分結果 score:70%

 

  • while True:
  • try:
  • S=[0]*1000000
  • N=int(input())
  • for _ in range(N):
  • L,R=input().split()
  • L,R=int(L),int(R)
  • S[L:R]=[1]*(R-L)
  • print(sum(S))
  • except:
  • break

評分結果 score:70%