b966: 第 3 題 線段覆蓋長度

編輯歷史

時間 作者 版本
2017-07-19 14:10 – 14:10 (unknown) r0 – r1
顯示 diff
+ b966: 第 3 題 線段覆蓋長度
+ https://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
+ 評分結果 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
+ *但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%