結果

問題 No.1743 Permutation Code
ユーザー tnodinotnodino
提出日時 2022-04-23 17:21:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 5,515 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 13,312 KB
実行使用メモリ 102,176 KB
最終ジャッジ日時 2024-06-25 04:30:47
合計ジャッジ時間 34,081 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
class mcf_graph():
    n=1
    pos=[]
    g=[[]]
    def __init__(self,N):
        self.n=N
        self.pos=[]
        self.g=[[] for i in range(N)]
    def add_edge(self,From,To,cap,cost):
        assert 0<=From and From<self.n
        assert 0<=To and To<self.n
        m=len(self.pos)
        self.pos.append((From,len(self.g[From])))
        self.g[From].append({"to":To,"rev":len(self.g[To]),"cap":cap,"cost":cost})
        self.g[To].append({"to":From,"rev":len(self.g[From])-1,"cap":0,"cost":-cost})
    def get_edge(self,i):
        m=len(self.pos)
        assert 0<=i and i<m
        _e=self.g[self.pos[i][0]][self.pos[i][1]]
        _re=self.g[_e["to"]][_e["rev"]]
        return {"from":self.pos[i][0],"to":_e["to"],"cap":_e["cap"]+_re["cap"],
        "flow":_re["cap"],"cost":_e["cost"]}
    def edges(self):
        m=len(self.pos)
        result=[{} for i in range(m)]
        for i in range(m):
            tmp=self.get_edge(i)
            result[i]["from"]=tmp["from"]
            result[i]["to"]=tmp["to"]
            result[i]["cap"]=tmp["cap"]
            result[i]["flow"]=tmp["flow"]
            result[i]["cost"]=tmp["cost"]
        return result
    def flow(self,s,t,flow_limit=(1<<63)-1):
        return self.slope(s,t,flow_limit)[-1]
    def slope(self,s,t,flow_limit=(1<<63)-1):
        assert 0<=s and s<self.n
        assert 0<=t and t<self.n
        assert s!=t
        '''
         variants (C = maxcost):
         -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0
         reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge
        '''
        dual=[0 for i in range(self.n)]
        dist=[0 for i in range(self.n)]
        pv=[0 for i in range(self.n)]
        pe=[0 for i in range(self.n)]
        vis=[False for i in range(self.n)]
        def dual_ref():
            for i in range(self.n):
                dist[i]=(1<<63)-1
                pv[i]=-1
                pe[i]=-1
                vis[i]=False
            que=[]
            heapq.heappush(que,(0,s))
            dist[s]=0
            while(que):
                v=heapq.heappop(que)[1]
                if vis[v]:continue
                vis[v]=True
                if v==t:break
                '''
                 dist[v] = shortest(s, v) + dual[s] - dual[v]
                 dist[v] >= 0 (all reduced cost are positive)
                 dist[v] <= (n-1)C
                '''
                for i in range(len(self.g[v])):
                    e=self.g[v][i]
                    if vis[e["to"]] or (not(e["cap"])):continue
                    '''
                     |-dual[e.to]+dual[v]| <= (n-1)C
                     cost <= C - -(n-1)C + 0 = nC
                    '''
                    cost=e["cost"]-dual[e["to"]]+dual[v]
                    if dist[e["to"]]-dist[v]>cost:
                        dist[e["to"]]=dist[v]+cost
                        pv[e["to"]]=v
                        pe[e["to"]]=i
                        heapq.heappush(que,(dist[e["to"]],e["to"]))
            if not(vis[t]):
                return False
            for v in range(self.n):
                if not(vis[v]):continue
                dual[v]-=dist[t]-dist[v]
            return True
        flow=0
        cost=0
        prev_cost=-1
        result=[(flow,cost)]
        while(flow<flow_limit):
            if not(dual_ref()):
                break
            c=flow_limit-flow
            v=t
            while(v!=s):
                c=min(c,self.g[pv[v]][pe[v]]["cap"])
                v=pv[v]
            v=t
            while(v!=s):
                self.g[pv[v]][pe[v]]["cap"]-=c
                self.g[v][self.g[pv[v]][pe[v]]["rev"]]["cap"]+=c
                v=pv[v]
            d=-dual[s]
            flow+=c
            cost+=c*d
            if(prev_cost==d):
                result.pop()
            result.append((flow,cost))
            prev_cost=cost
        return result

from sys import setrecursionlimit
setrecursionlimit(10**6)

def operator1(C):
    M = len(C)
    cnt = 0
    for b in range(1,1<<16):
        cnt += len(bin(b)) - 2
        if cnt == M:
            return b, M, len(bin(b)) - 2

def operator2(C, M):
    Run = []
    cnt = 1
    for i in range(1,M):
        if C[i] == '0':
            cnt += 1
        else:
            Run.append(cnt)
            cnt = 1
    Run.append(cnt)
    return Run

def operator3(N):
    Num = [[] for _ in range(N+1)]
    for i in range(1,N+1):
        B = bin(i)[2:]
        cnt = 1
        for b in B[1:]:
            if b == '0':
                cnt += 1
            else:
                Num[i].append(cnt)
                cnt = 1
        Num[i].append(cnt)
    return Num

C = input()
N,M,B = operator1(C)
Run = operator2(C, M)
M = len(Run)
Num = operator3(N)
G = mcf_graph(N+M+2)
for i in range(M):
    G.add_edge(0, i+1, 1, 1)
for i in range(N):
    G.add_edge(M+i+1, N+M+1, 1, 1)
for i in range(1,N+1):
    num = Num[i]
    L = len(num)
    for j in range(M-L+1):
        Flg2 = 0
        for k in range(L):
            if num[k] != Run[j+k]:
                Flg2 = 1
        if Flg2:
            continue
        for k in range(L):
            G.add_edge(j+k+1, M+i, 1, 1<<(N-i))
G.flow(0, N+M+1, N)
Edges = G.edges()
ans = [0] * (N+1)
for e in Edges:
    From = e['from']
    To = e['to']
    Flow = e['flow']
    if From == 0 or To == N + M + 1 or Flow == 0:
        continue
    ans[To-M] = From
for i in range(N+1):
    ans[i] = [ans[i], i]
ans = ans[1:]
ans.sort()
for i in range(N):
    ans[i] = ans[i][1]
print(*ans)
0