結果

問題 No.1900 Don't be Powers of 2
ユーザー SSRSSSRS
提出日時 2022-04-08 23:26:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,993 bytes
コンパイル時間 2,212 ms
コンパイル使用メモリ 217,336 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 02:00:18
合計ジャッジ時間 4,325 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 10 ms
4,384 KB
testcase_05 AC 9 ms
4,376 KB
testcase_06 AC 9 ms
4,380 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 21 ms
4,380 KB
testcase_09 AC 11 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 AC 28 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 8 ms
4,376 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 7 ms
4,384 KB
testcase_22 AC 7 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 32 ms
4,380 KB
testcase_29 AC 32 ms
4,380 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 111 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 20 ms
4,380 KB
testcase_41 AC 34 ms
4,384 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,384 KB
testcase_44 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
template <typename Cap>
struct ford_fulkerson{
	struct edge{
		int to, rev;
		Cap cap;
		edge(int to, int rev, Cap cap): to(to), rev(rev), cap(cap){
		}
	};
	int N;
	vector<vector<edge>> G;
	ford_fulkerson(){
	}
	ford_fulkerson(int N): N(N), G(N){
	}
	void add_edge(int from, int to, Cap cap){
		int id1 = G[from].size();
		int id2 = G[to].size();
		G[from].push_back(edge(to, id2, cap));
		G[to].push_back(edge(from, id1, 0));
	}
	Cap max_flow(int s, int t){
		Cap flow = 0;
		while (1){
			vector<Cap> m(N, INF);
			vector<int> pv(N, -1);
			vector<int> pe(N, -1);
			vector<bool> used(N, false);
			queue<int> Q;
			Q.push(s);
			used[s] = true;
			while (!Q.empty()){
				int v = Q.front();
				Q.pop();
				int cnt = G[v].size();
				for (int i = 0; i < cnt; i++){
					int w = G[v][i].to;
					if (!used[w] && G[v][i].cap > 0){
						used[w] = true;
						m[w] = min(m[v], G[v][i].cap);
						pv[w] = v;
						pe[w] = i;
						Q.push(w);
					}
				}
			}
			if (!used[t]){
				break;
			}
			Cap f = m[t];
			for (int i = t; i != s; i = pv[i]){
				G[pv[i]][pe[i]].cap -= f;
				G[i][G[pv[i]][pe[i]].rev].cap += f;
			}
			flow += f;
		}
		return flow;
	}
};
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  map<int, int> mp;
  for (int i = 0; i < N; i++){
    mp[A[i]]++;
  }
  vector<int> B, C;
  int M = 0;
  for (auto P : mp){
    M++;
    B.push_back(P.first);
    C.push_back(P.second);
  }
  ford_fulkerson<int> G(M + 2);
  for (int i = 0; i < M; i++){
    if (__builtin_parity(B[i]) == 0){
      for (int j = 0; j < M; j++){
        if (__builtin_popcount(B[i] ^ B[j]) == 1){
          G.add_edge(i, j, C[i] * C[j]);
        }
      }
    }
  }
  for (int i = 0; i < M; i++){
    if (__builtin_parity(B[i]) == 0){
      G.add_edge(M, i, C[i]);
    } else {
      G.add_edge(i, M + 1, C[i]);
    }
  }
  cout << N - G.max_flow(M, M + 1) << endl;
}
0