結果

問題 No.1437 01 Sort
ユーザー tyawanmusi
提出日時 2021-02-20 15:35:27
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 4,483 bytes
コンパイル時間 111 ms
コンパイル使用メモリ 13,440 KB
実行使用メモリ 29,360 KB
最終ジャッジ日時 2024-09-19 01:14:01
合計ジャッジ時間 4,202 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 TLE * 1 -- * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

# O(N log^2 N)
from bisect import bisect_left,bisect_right
def solve4(n,s):
ans=10**10
zero=s.count("0")
one=n-zero
if zero==0 or zero==n:
return 0
se=set()
if s[0]=="1":
se.add(0)
for i in range(n-1,-1,-1):
if s[i]=="1":
se.add(i)
else:break
po=[]
for i in range(n):
if s[i]=="1":po+=[i,i+n,i+n+n,i+n+n+n]
po.sort()
def f(l,ll):
sp_l=(l+one)%n
sp_r=n
if sp_l==0:sp_l=n
ansl=0
ansr=0
flag=True
flagg=False
if po[ll]<=l+n:
if sp_l<=((po[ll]-1)%n+1)<=sp_r and (po[ll]%n not in se):
ansl=l+n-po[ll]-1
else:
ansl=l+n-po[ll]
if l+n+one-1<po[ll+one-1]:
flag=True
flagg=False
ng=-1
ok=one
while ng+1!=ok:
mid=(ng+ok)//2
if l+n+mid<po[ll+mid]:ok=mid
else:ng=mid
x=l+n+ok
if x%n:x+=n-(x%n)
if x<=l+n+one-1:
if l+n+ok<=po[ll+ok]<=x:
flagg=True
x+=n
if l+n+ok<=po[ll+ok]<=x:
flag=False
ansr=one-ok
if ansr:ansr+=flag
ansr-=flagg
return max(ansl,ansr)
for l in range(n):
anss=(-l-one)%n
sp_l=(l+one)%n
sp_r=n
if sp_l==0:sp_l=n
ng=-1
ok=n
while ng+1!=ok:
mid=(ng+ok)//2
x=l+n-mid-1
if not(sp_l<=((x-1)%n+1)<=sp_r and (x%n not in se)):x+=1
ll=bisect_left(po,x)
if f(l,ll)<=mid:ok=mid
else:ng=mid
ans=min(ans,anss+ok*n)
return ans
def solve3(n,s):
ans=10**10
zero=s.count("0")
one=n-zero
if zero==0 or zero==n:
return 0
se=set()
if s[0]=="1":
se.add(0)
for i in range(n-1,-1,-1):
if s[i]=="1":
se.add(i)
else:break
po=[]
for i in range(n):
if s[i]=="1":po+=[i,i+n,i+n+n,i+n+n+n]
po.sort()
for l in range(n):
a=list(range(l+n,l+one+n))
sp_l=(l+one)%n
sp_r=n
if sp_l==0:sp_l=n
for ll in range(one*3):
ansl=0
ansr=0
flag=True
flagg=False
if po[ll]<=a[0]:
if sp_l<=((po[ll]-1)%n+1)<=sp_r and (po[ll]%n not in se):
ansl=a[0]-po[ll]-1
else:
ansl=a[0]-po[ll]
if a[one-1]<po[ll+one-1]:
flag=True
flagg=False
ng=-1
ok=one
while ng+1!=ok:
mid=(ng+ok)//2
if a[mid]<po[ll+mid]:ok=mid
else:ng=mid
x=a[ok]
if x%n:x+=n-(x%n)
if x<=a[one-1]:
if a[ok]<=po[ll+ok]<=x:
flagg=True
x+=n
if a[ok]<=po[ll+ok]<=x:
flag=False
ansr=one-ok
if ansr:ansr+=flag
ansr-=flagg
print(l,ll,max(ansl,ansr)*n+(-l-one)%n)
ans=min(ans,max(ansl,ansr)*n+(-l-one)%n)
return ans
def solve2(n,s):
ans=10**10
zero=s.count("0")
one=n-zero
if zero==0 or zero==n:
return 0
se=set()
if s[0]=="1":
se.add(0)
for i in range(n-1,-1,-1):
if s[i]=="1":
se.add(i)
else:break
for l in range(n):
a=list(range(l+n,l+one+n))
sp_l=(l+one)%n
sp_r=n
if sp_l==0:sp_l=n
for ll in range(n*3):
b=[]
for i in range(ll,ll+n):
if s[i%n]=="1":b.append(i)
ansl=0
ansr=0
flag=True
flagg=False
if b[0]<=a[0]:
if sp_l<=((b[0]-1)%n+1)<=sp_r and (b[0]%n not in se):
ansl=a[0]-b[0]-1
else:
ansl=a[0]-b[0]
for i in range(one):
if b[i]>a[i]:
ansr+=1
x=a[i]
if x%n:x+=n-(x%n)
if x<=a[-1]:
if a[i]<=b[i]<=x:
flagg=True
x+=n
if a[i]<=b[i]<=x:
flag=False
if ansr:ansr+=flag
ansr-=flagg
ans=min(ans,max(ansl,ansr)*n+(-l-one)%n)
return ans
from collections import defaultdict, deque
def solve1(n,s):
cost = defaultdict(int)
cost[s] = 0
goal = "".join(sorted(list(s)))
que = deque([s])
while que:
t = que.popleft()
if t == goal:
return cost[t]
t1 = t[-1] + t[:-1]
t2 = t[0] + t[-1] + t[1:-1]
if t1 not in cost:
cost[t1] = cost[t] + 1
que.append(t1)
if t2 not in cost:
cost[t2] = cost[t] + 1
que.append(t2)
from random import randint
t="01"
while 0:
n=randint(50,100)
s="".join([t[randint(0,1)]for i in range(n)])
ans3=solve3(n,s)
ans4=solve4(n,s)
if ans4!=ans3:
print("ERROR!!")
print(n)
print(s)
print("ans3 =",ans3)
print("ans4 =",ans4)
exit()
print("ok")
n=int(input())
s=input()
#print(solve3(n,s))
print(solve4(n,s))
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0