QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#139097 | #4425. Cake | ckiseki# | ML | 0ms | 0kb | C++17 | 6.4kb | 2023-08-12 17:41:45 | 2023-08-12 17:41:48 |
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int cnt = sizeof...(T);
(..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
cerr << "\e[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
cerr << (f++ ? ", " : "") << *L;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
template<typename Cap = int64_t>
class Dinic{
private:
struct E {
int to, rev;
Cap cap;
};
int n, st, ed;
vector<vector<E>> G;
vector<int> lv, idx;
bool BFS() {
lv.assign(n, -1);
queue<int> bfs;
bfs.push(st);
lv[st] = 0;
while (not bfs.empty()) {
int u = bfs.front();
bfs.pop();
for (auto e : G[u]) {
if (e.cap <= 0 or lv[e.to] != -1)
continue;
bfs.push(e.to);
lv[e.to] = lv[u] + 1;
}
}
return lv[ed] != -1;
}
Cap DFS(int u, Cap f) {
if (u == ed)
return f;
Cap ret = 0;
for (int &i = idx[u]; i < int(G[u].size()); ++i) {
auto &e = G[u][i];
if (e.cap <= 0 or lv[e.to] != lv[u] + 1)
continue;
Cap nf = DFS(e.to, min(f, e.cap));
ret += nf;
e.cap -= nf;
f -= nf;
G[e.to][e.rev].cap += nf;
if (f == 0) return ret;
}
if (ret == 0) lv[u] = -1;
return ret;
}
public:
void init(int n_) { G.assign(n = n_, vector<E>()); }
void add_edge(int u, int v, Cap c) {
G[u].push_back({v, int(G[v].size()), c});
G[v].push_back({u, int(G[u].size()) - 1, 0});
}
Cap max_flow(int st_, int ed_) {
st = st_, ed = ed_;
Cap ret =0 ;
while (BFS()) {
idx.assign(n, 0);
Cap f = DFS(st, numeric_limits<Cap>::max());
ret += f;
if (f == 0)
break;
}
return ret;
}
};
const int maxn = 200025 * 20;
int get_lg(int k) {
int h = 0;
while ((1 << h) <= k)
++h;
return h;
}
struct Trie {
int tot;
int ch[maxn][2];
int segid[maxn];
int cur;
int height;
void init(int n, int k) {
height = get_lg(k);
for (int i = 0; i <= tot; i++) {
ch[i][0] = ch[i][1] = 0;
segid[i] = -1;
}
tot = 0;
cur = n;
segid[0] = ++cur;
}
int insert(int val) {
int now = 0;
for (int i = height; i >= 0; i--) {
int d = val >> i & 1;
if (!ch[now][d]) {
ch[now][d] = ++tot;
segid[tot] = ++cur;
}
now = ch[now][d];
}
return now;
}
void build(Dinic<> &f) {
for (int i = 0; i <= tot; i++) {
for (int j: {0, 1}) if (ch[i][j]) {
debug(i, j, segid[i], segid[ch[i][j]]);
f.add_edge(segid[i], segid[ch[i][j]], 1e9);
}
}
}
void add_edge(Dinic<> &f, int node, int val, int k) {
int now = 0;
for (int i = height; i >= 0; i--) {
int d = (val ^ k) >> i & 1;
if (~k & 1) {
if (ch[now][!d]) {
debug(node, segid[ch[now][!d]]);
f.add_edge(node, segid[ch[now][!d]], 1e9);
}
}
if (!ch[now][d]) return;
now = ch[now][d];
}
}
} tr;
int gao(const vector<int> &a, int k) {
const int h = get_lg(k);
const int n = a.size();
tr.init(n, k);
for (int i = 0; i < n; i++) {
tr.insert(a[i]);
}
Dinic<> flow;
int S = tr.cur, T = S + 1;
flow.init(T + 1);
orange(all(a));
for (int i = 0; i < n; i++) {
// debug(a[i], i, h, a[i] >> (h - 1) & 1);
if (a[i] >> (h - 1) & 1) {
// for (int j = 0; j < n; j++)
// if ((a[i] ^ a[j]) > k)
// flow.add_edge(j, i, 1);
flow.add_edge(i, T, 1);
debug(a[i], T);
} else {
// for (int j = 0; j < n; j++)
// if ((a[i] ^ a[j]) > k)
// flow.add_edge(i, j, 1);
flow.add_edge(S, i, 1);
debug(S, a[i]);
}
}
tr.build(flow);
for (int i = 0; i < n; i++) {
tr.add_edge(flow, i, a[i], k);
}
int f = flow.max_flow(S, T);
debug(f);
return n - f;
}
void solve() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
const int h = get_lg(k);
debug(k >> h & 1);
map<int, vector<int>> mp;
for (int i = 0; i < n; i++) {
debug(a[i] >> h);
mp[a[i] >> h].emplace_back(a[i] & ((1 << h) - 1));
}
int ans = 0;
for (const auto &[_, v]: mp) {
ans = max(ans, gao(v, k));
}
cout << ans << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
/*
[ all(a) ] = [ 3, 12, 9, 10, 3, 4 ]
(S, a[i]) = (24, 3)
(a[i], T) = (12, 25)
(a[i], T) = (9, 25)
(a[i], T) = (10, 25)
(S, a[i]) = (24, 3)
(S, a[i]) = (24, 4)
(i, j, segid[i], segid[ch[i][j]]) = (0, 0, 7, 8)
(i, j, segid[i], segid[ch[i][j]]) = (1, 0, 8, 9)
(i, j, segid[i], segid[ch[i][j]]) = (1, 1, 8, 13)
(i, j, segid[i], segid[ch[i][j]]) = (2, 0, 9, 10)
(i, j, segid[i], segid[ch[i][j]]) = (2, 1, 9, 22)
(i, j, segid[i], segid[ch[i][j]]) = (3, 1, 10, 11)
(i, j, segid[i], segid[ch[i][j]]) = (4, 1, 11, 12)
(i, j, segid[i], segid[ch[i][j]]) = (6, 0, 13, 17)
(i, j, segid[i], segid[ch[i][j]]) = (6, 1, 13, 14)
(i, j, segid[i], segid[ch[i][j]]) = (7, 0, 14, 15)
(i, j, segid[i], segid[ch[i][j]]) = (8, 0, 15, 16)
(i, j, segid[i], segid[ch[i][j]]) = (10, 0, 17, 18)
(i, j, segid[i], segid[ch[i][j]]) = (10, 1, 17, 20)
(i, j, segid[i], segid[ch[i][j]]) = (11, 1, 18, 19)
(i, j, segid[i], segid[ch[i][j]]) = (13, 0, 20, 21)
(i, j, segid[i], segid[ch[i][j]]) = (15, 0, 22, 23)
(i, j, segid[i], segid[ch[i][j]]) = (16, 0, 23, 24)
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
4 500000 471518156 319758862 812815356 520822448 129241996 461169933 796713727 608641317 281180101 953966756 749634941 274104949 996181952 88142916 998544672 125597509 991731126 974767231 338911715 674197249 167602044 682799026 226927279 703198907 216742488 8185420 94921423 690039818 859329736 45428...