[COCI 2024/2025 #3] 红蓝牌 / Karte 题解
E_firework · · 题解
法一
前置知识:位运算。
因为
无优化的代码:
#include<bits/stdc++.h>
#define LL long long
#define mes(s, x) memset(s, x, sizeof s)
#define Maxn 25
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
char c[Maxn][Maxn];
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int n = read(), m = read(), x = read(), y = read(), ans = 0, tmp, s;
for(int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
for(int i = 0; i < 1 << n; i++){//枚举子集
tmp = 0;
for(int j = 1; j <= n; j++) if((i >> (j - 1)) & 1) tmp -= x;
for(int k = 1; k <= m; k++){
s = 0;
for(int j = 1; j <= n; j++) if(((i >> (j - 1)) & 1) && c[j][k] == '1') s++;
if(s > y) tmp += s - y;
}
ans = max(tmp, ans);
}
printf("%d\n", ans);
return 0;
}
有优化的代码:
#include<bits/stdc++.h>
#define LL long long
#define mes(s, x) memset(s, x, sizeof s)
#define Maxn 25
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
char c[Maxn][Maxn];
int a[Maxn];//与一张蓝牌组成好对的红牌集合
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int n = read(), m = read(), x = read(), y = read(), ans = 0, tmp, s;
for(int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
if(c[j][i] == '1') a[i] |= 1 << (j - 1);
}
}
for(int i = 0; i < 1 << n; i++){//枚举子集
tmp = 0;
for(int j = 1; j <= n; j++) if((i >> (j - 1)) & 1) tmp -= x;
for(int j = 1; j <= m; j++){
s = __builtin_popcount(i & a[j]);
if(s > y) tmp += s - y;
}
ans = max(tmp, ans);
}
printf("%d\n", ans);
return 0;
}
法二
前置知识:最小割。
需要注意,网络流并不属于 NOI 大纲划定的入门级或提高级考察范围。
我们可以使用最小割算法解决这个问题,我们需要构造一个图使得其最小割等于为方案中未包含的好对数与选取的牌的代价之和的最小值(选红牌的代价为
我们把每张牌当成一个节点,再建立一个源点
在构造未包含的好对数时可以用二元关系最小割的方法分析。
考虑一个好对
解得
注意到这一做法可以扩展到好对不是一红一蓝的情况,因为分析中并没有用到二分图的性质。
如果使用 dinic 算法求解最小割,那么时间复杂度不会超过
代码:
#include<bits/stdc++.h>
#define LL long long
#define mes(s, x) memset(s, x, sizeof s)
#define Maxn 50
#define Maxm 2005
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
int hd[Maxn], nxt[Maxm], v[Maxm], f[Maxm], cnt;
void addedge0(int x, int y, int z){
nxt[++cnt] = hd[x];
hd[x] = cnt;
v[cnt] = y;
f[cnt] = z;
}
void addedge(int x, int y, int z){
// printf("%d %d %d\n", x, y, z);
addedge0(x, y, z);
addedge0(y, x, 0);
}
void clear(){
mes(hd, -1);
cnt = -1;
}
int d[Maxn], cur[Maxn], lid, rid, s, t;
bool bfs(){
mes(d, -1);
d[s] = 0;
queue<int> q;
q.push(s);
int x, y;
while(q.size()){
x = q.front();
q.pop();
for(int j = hd[x]; j != -1; j = nxt[j]){
y = v[j];
if(f[j] && d[y] == -1){
d[y] = d[x] + 1;
q.push(y);
}
}
}
return d[t] != -1;
}
LL dfs(int i, LL a){
if(a == 0 || i == t) return a;
LL s = 0, x;
int j, y;
while(cur[i] != -1){
j = cur[i];
y = v[j];
if(d[i] + 1 == d[y]){
x = dfs(y, min(a, 1ll * f[j]));
a -= x;
s += x;
f[j] -= x;
f[j ^ 1] += x;
if(a == 0) break;
}
cur[i] = nxt[j];
}
return s;
}
LL dinic(){
LL sum = 0;
while(bfs()){
for(int i = lid; i <= rid; i++) cur[i] = hd[i];
sum += dfs(s, inf);
}
return sum;
}
char c[25][25];
int a[25];
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
clear();
int n = read(), m = read(), x = read(), y = read(), d = 0;
lid = s = 0, rid = t = n + m + 1;
for(int i = 1; i <= n; i++){
scanf("%s", c[i] + 1);
for(int j = 1; j <= m; j++){
if(c[i][j] == '1'){
a[i]++;
d++;
addedge(i, n + j, 1);
}
}
}
for(int i = 1; i <= n; i++){
addedge(s, i, a[i]);
addedge(i, t, x);
}
for(int i = 1; i <= m; i++){
addedge(n + i, t, y);
}
printf("%d\n", d - dinic());
return 0;
}