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.

ABC187 C - 1-SAT から学んだ

Last updated at Posted at 2021-09-13

abc187_1.png

んー、何とかなりそうかも。

abc187_2.png

サクッと書いたが見事に TLE

SAT_rev1.py
from sys import exit
n = int(input())
lis = []
_lis = []
 
for _ in range(n):
    s = input()
    if s[0] == "!":
        _lis.append(s)
    else:
        lis.append(s)
 
for x in lis:        # len(lis) = 10**5
    if "!"+x in _lis:# len(_lis) = 10**5 の場合、10**10 となり TLE
        print(x)
        exit()
print("satisfiable")

※リスト x in s の計算量 O(n) を参照

じゃあ、試しに辞書にしてみよう。
以下で通った。

SAT_rev2.py
from sys import exit
n = int(input())
lis = []
dic = {}
 
for _ in range(n):
    s = input()
    if s[0] == "!":
        if s not in dic:
            dic[s] = 0
        dic[s] += 1
    else:
        lis.append(s)
 
for x in lis:       #計算量 O(N)
    if "!"+x in dic:#計算量 O(1) 
        print(x)
        exit()
print("satisfiable")

神曰く、辞書、set は in演算子は O(1) で済む。
勉強になった。

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?