結果

問題 No.1900 Don't be Powers of 2
ユーザー ShirotsumeShirotsume
提出日時 2022-02-25 18:48:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 4,721 bytes
コンパイル時間 981 ms
コンパイル使用メモリ 86,984 KB
実行使用メモリ 90,068 KB
最終ジャッジ日時 2023-09-25 14:17:28
合計ジャッジ時間 8,790 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,648 KB
testcase_01 AC 94 ms
71,620 KB
testcase_02 AC 96 ms
72,136 KB
testcase_03 AC 168 ms
80,648 KB
testcase_04 AC 153 ms
80,556 KB
testcase_05 AC 156 ms
81,164 KB
testcase_06 AC 153 ms
80,672 KB
testcase_07 AC 154 ms
80,600 KB
testcase_08 AC 162 ms
81,788 KB
testcase_09 AC 150 ms
80,120 KB
testcase_10 AC 128 ms
78,412 KB
testcase_11 AC 140 ms
78,644 KB
testcase_12 AC 171 ms
82,108 KB
testcase_13 AC 103 ms
77,128 KB
testcase_14 AC 137 ms
78,776 KB
testcase_15 AC 102 ms
77,132 KB
testcase_16 AC 102 ms
76,952 KB
testcase_17 AC 156 ms
78,900 KB
testcase_18 AC 144 ms
78,876 KB
testcase_19 AC 148 ms
78,736 KB
testcase_20 AC 142 ms
78,976 KB
testcase_21 AC 143 ms
78,888 KB
testcase_22 AC 142 ms
78,684 KB
testcase_23 AC 108 ms
77,760 KB
testcase_24 AC 105 ms
77,612 KB
testcase_25 AC 111 ms
77,852 KB
testcase_26 AC 110 ms
77,612 KB
testcase_27 AC 120 ms
78,516 KB
testcase_28 AC 168 ms
82,668 KB
testcase_29 AC 176 ms
82,016 KB
testcase_30 AC 134 ms
77,516 KB
testcase_31 AC 94 ms
71,344 KB
testcase_32 AC 94 ms
71,844 KB
testcase_33 AC 108 ms
77,556 KB
testcase_34 AC 107 ms
77,540 KB
testcase_35 AC 111 ms
77,568 KB
testcase_36 AC 109 ms
77,992 KB
testcase_37 AC 227 ms
90,068 KB
testcase_38 AC 104 ms
77,324 KB
testcase_39 AC 103 ms
75,920 KB
testcase_40 AC 189 ms
81,660 KB
testcase_41 AC 188 ms
81,932 KB
testcase_42 AC 95 ms
71,636 KB
testcase_43 AC 94 ms
71,632 KB
testcase_44 AC 94 ms
71,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
class mf_graph:
    n=1
    g=[[] for i in range(1)]
    pos=[]
    def __init__(self,N):
        self.n=N
        self.g=[[] for i in range(N)]
        self.pos=[]
    def add_edge(self,From,To,cap):
        assert 0<=From and From<self.n
        assert 0<=To and To<self.n
        assert 0<=cap
        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})
        self.g[To].append({"to":From,"rev":len(self.g[From])-1,"cap":0})
        return m
    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"]}
    def edges(self):
        m=len(self.pos)
        result=[]
        for i in range(m):
            result.append(self.get_edge(i))
        return result
    def change_edge(self,i,new_cap,new_flow):
        m=len(self.pos)
        assert 0<=i and i<m
        assert 0<=new_flow and new_flow<=new_cap
        _e=self.g[self.pos[i][0]][self.pos[i][1]]
        _re=self.g[_e["to"]][_e["rev"]]
        _e["cap"]=new_cap-new_flow
        _re["cap"]=new_flow
    def flow(self,s,t,flow_limit=(1<<63)-1):
        assert 0<=s and s<self.n
        assert 0<=t and t<self.n
        level=[0 for i in range(self.n)]
        Iter=[0 for i in range(self.n)]
        que=deque([])
        def bfs():
            for i in range(self.n):level[i]=-1
            level[s]=0
            que=deque([])
            que.append(s)
            while(len(que)>0):
                v=que.popleft()
                for e in self.g[v]:
                    if e["cap"]==0 or level[e["to"]]>=0:continue
                    level[e["to"]]=level[v]+1
                    if e["to"]==t:return
                    que.append(e["to"])
        def dfs(func,v,up):
            if (v==s):return up
            res=0
            level_v=level[v]
            for i in range(Iter[v],len(self.g[v])):
                e=self.g[v][i]
                if (level_v<=level[e["to"]] or self.g[e["to"]][e["rev"]]["cap"]==0):continue
                d=func(func,e["to"],min(up-res,self.g[e["to"]][e["rev"]]["cap"]))
                if d<=0:continue
                self.g[v][i]["cap"]+=d
                self.g[e["to"]][e["rev"]]["cap"]-=d
                res+=d
                if res==up:return res
            level[v]=self.n
            return res
        flow=0
        while(flow<flow_limit):
            bfs()
            if level[t]==-1:break
            for i in range(self.n):Iter[i]=0
            while(flow<flow_limit):
                f=dfs(dfs,t,flow_limit-flow)
                if not(f):break
                flow+=f
        return flow
    def min_cut(self,s):
        visited=[False for i in range(self.n)]
        que=deque([])
        que.append(s)
        while(len(que)>0):
            p=que.popleft()
            visited[p]=True
            for e in self.g[p]:
                if e["cap"] and not(visited[e["to"]]):
                    visited[e["to"]]=True
                    que.append(e["to"])
        return visited
 
 



#https://qiita.com/zawawahoge/items/8bbd4c2319e7f7746266
def Popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f
    
N = int(input())

A = list(map(int,input().split()))
from collections import Counter
even = Counter()
odd = Counter()
cntodd = 0
cnteven = 0
for i in range(N):
    if Popcount(A[i]) % 2 and odd[A[i]] == 0:
        cntodd += 1
        odd[A[i]] = cntodd
    elif Popcount(A[i]) % 2 == 0 and even[A[i]] == 0:
        cnteven += 1
        even[A[i]] = cnteven
n = len(odd.keys())
m = len(even.keys())
G = mf_graph(n + m + 2)
l = list(odd.keys())

A = Counter(A)
for i in range(n):
    G.add_edge(0, i + 1, A[l[i]])
l = list(even.keys())
for i in range(m):
    G.add_edge(n + i + 1, n + m + 1, A[l[i]])
for v1, c1 in odd.items():
    for v2, c2 in even.items():
        if Popcount(v1 ^ v2) == 1:
            G.add_edge(c1, n + c2, min(A[v1], A[v2]))
f = G.flow(0, n + m + 1)
print(N - f)




0