0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ABC181 C - Collinearity を解いた。おまけ B 問題付き

Posted at

abc181_1.png
abc181_2.png
abc181_3.png

100C3 : 全探索しても ザックリ O(300000)。
間に合う。

abc181c.py
N = int(input())
lis = []
for _ in range(N):
    x,y = map(int,input().split())
    lis.append([x,y])
 
from itertools import combinations
for a,b,c in combinations(range(N),3):
    if (lis[a][0]-lis[b][0])*(lis[a][1]-lis[c][1]) == (lis[a][0]-lis[c][0])*(lis[a][1]-lis[b][1]):
        print("Yes")
        exit()
print("No")

サクッと書いて TLE。理由もコメントで入れました。

abc181b_ng.py
N = int(input())
memo = []
 
for _ in range(N):#O(10^5)
    a,b = map(int,input().split())
    memo.extend(range(a,b+1))#worst O(10^6)
print(sum(memo))
## maximum O(10^11) => TLE

対策はこれ。

abc181b_ok.py
N = int(input())
memo = []
 
def cal(n):#n が偶数、奇数の何れでも必ず 2 で割り切れる
    return (n*(1+n))//2
 
for _ in range(N):
    a,b = map(int,input().split())
    memo.append(cal(b)-cal(a-1))
print(sum(memo))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?