针对如下样例进行说明:
1
4
3 8 1 10
7 5 9 2
4 6 8 5
10 1 7 3
9 4 10 6
2 8 5 1
7 3 9 4
6 10 2 8 我们将每一个可能决策看成平面上的一个点,其横坐标为 且其纵坐标为 就得到了下图:

其中下凸壳上的点用线段连上了。很容易发现最后答案一定是下凸壳左半部分上的点(横纵坐标之积最小的点一定在凸壳上)。如果一个点不在下凸壳左半部分上,那他横纵坐标之积一定大于左下凸壳上某一点横纵坐标之积,那他一定就是不优的,我们就不必考虑了,所以我们的决策集就只剩下了左下凸壳。
考虑如何维护这个凸壳。我们会Andrew!,Andrew其实比暴力还劣,因为他要用暴力求出所有点的坐标,还要排序加扫描。发现我们很容易找到左下凸壳的左端点和右端点(即最小平凡度和最小违和度),考虑如何在这个基础上继续找点。我们令分治点为,胡乱猜应该让离当前左右端点距离最远,开始推式子:
好像啥也没推因为我不会KaTeX化到最后的话这个决策所有平凡度会有一个的权,所有违和度会有一个的权,我们以带权后的两个度的和为边的费用跑一遍mcmf,最后我们就会根据边的剩余流量得到这个决策的具体方案(无剩余流量即为选择,而最小费用保证了上述值最小即离左右端点的直线最远),然后递归分治即可。
思路应该已经比较完备了吧。这里给出我的代码作为参考实现:
#include<bits/stdc++.h>
using namespace std;
struct node{
int x, y;
};
struct edge{
int to, val, cost;
}ma[12007];
int head[1207], tot = 1, nxt[12007];
int T, n, s, t, ans_cost, ans_f, ans;
int a[75][75], b[75][75], p[75];
int cur[12007], dis[12007];
int vis[12007];
void add(const int &x, const int &y, const int &val, const int &cost){
ma[++tot].to = y;
ma[tot].val = val;
ma[tot].cost = cost;
nxt[tot] = head[x];
head[x] = tot;
}
int cross(const node& x_from, const node& x_to, const node& y_from, const node& y_to){
return (x_to.x - x_from.x) * (y_to.y - y_from.y) - (y_to.x - y_from.x) * (x_to.y - x_from.y);
}
int spfa(){
for(int i = 0;i <= n * 2 + 1;++i){
dis[i] = 1e9;
vis[i] = 0;
}
deque<int> q;
dis[s] = 0;
vis[s] = 1;
q.push_back(s);
while(!q.empty()){
int u = q.front();
q.pop_front();
vis[u] = 0;
for(int i = head[u];i;i = nxt[i]){
int v = ma[i].to;
if(ma[i].val){
if(dis[v] > dis[u] + ma[i].cost){
dis[v] = dis[u] + ma[i].cost;
if(!vis[v]){
vis[v] = 1;
if(q.empty()){
q.push_front(v);
continue;
}
if(dis[v] > dis[q.front()]){
q.push_back(v);
}else{
q.push_front(v);
}
}
}
}
}
}
return dis[t] != (1e9);
}
int dfs(const int &u, int lim){
if(u == t || !lim){
return lim;
}
vis[u] = 1;
int a = 0;
for(int i = cur[u];i;i = nxt[i]){
cur[u] = i;
int v = ma[i].to;
if(!vis[v] && ma[i].val && dis[v] == dis[u] + ma[i].cost){
int f = dfs(v, min(lim, ma[i].val));
if(f){
if(u <= n && v != s){
p[u] = v - n;
}
ma[i].val -= f;
ma[i ^ 1].val += f;
ans_cost += f * ma[i].cost;
lim -= f;
a += f;
if(!lim){
break;
}
}else{
dis[v] = 0;
}
}
}
vis[u] = 0;
return a;
}
node mcmf(const int &k_x, const int &k_y){
ans_f = ans_cost = 0;
tot = 0;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
tot += 2;
ma[tot].cost = k_x * a[i][j] + k_y * b[i][j];
ma[tot ^ 1].cost = -ma[tot].cost;
}
}
while(spfa()){
for(int i = 0;i <= n * 2 + 1;++i){
cur[i] = head[i];
}
ans_f += dfs(s, 1e9);
}
int sum_a = 0, sum_b = 0;
for(int i = 1;i <= n;++i){
sum_a += a[i][p[i]];
sum_b += b[i][p[i]];
int now = (i - 1) * n * 2 + p[i] * 2;
ma[now].val = 1;
ma[now ^ 1].val = 0;
}
for(int i = 1;i <= 2 * n;++i){
tot += 2;
ma[tot].val = 1;
ma[tot ^ 1].val = 0;
}
ans = min(ans, sum_a * sum_b);
return {sum_a, sum_b};
}
void made(const node &a, const node &b){
node c = mcmf(a.y - b.y, b.x - a.x);
if(cross(a, b, a, c) < 0){
made(a, c);
made(c, b);
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> T;
while(T--){
ans = 1e9;
tot = 1;
memset(head, 0, sizeof(head));
cin >> n;
t = n * 2 + 1;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
cin >> a[i][j];
add(i, j + n, 1, 0);
add(j + n, i, 0, 0);
}
}
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
cin >> b[i][j];
}
}
for(int i = 1;i <= n;++i){
add(s, i, 1, 0);
add(i, s, 0, 0);
add(i + n, t, 1, 0);
add(t, i + n, 0, 0);
}
node a, b;
a = mcmf(1, 0);
b = mcmf(0, 1);
made(a, b);
cout << ans << endl;
}
return 0;
}几句闲话:注意到我的当前弧没有写memcpy,而是for修改,为什么呢?考虑到如下情况:
int a[200001], b[200001], c[400001];
memcpy(a, c, sizeof(c));很明显我们要将c复制到a,就这一行语句我们应该写不炸吧?炸了。发现c数组的大小是a数组的二倍,而memcpy忠诚的执行了覆盖,但他并没有报RE而是报了WA,为什么呢?c++为了保证效率不会对你的任何操作进行内存检查,只会在无法继续执行的情况下返回RE,所以他继续复制c数组中的内容。复制到哪呢?应该不会是b数组吧,好难猜啊。这种情况下你连样例都不一定能过。
那我们就可以写出一些不合语法、不合情理、能正常运行但确实有病的写法:
int a[100], b = 10;
a[b] = 998244353;
cout << b[a];这份代码确实能过编,也确实会输出998244353,但要是在正常代码中处处混杂这种写法然后让别人帮你查错的话你大概会被砍成998244353份。珍爱生命,远离内存操作,远离奇技淫巧!