题目链接
题意
给定一个序列,从里面选出x个数,其中的逆序对数为y,使y / x最大。
思路
将这些序列中的数看做点,其中存在逆序关系的(a[i], a[j])看做一条边,比如3 4 2 5 1。就可以建成如下的一张图:
那么问题就转化成了在图中选出若干个点,使这些点包含的边/这些点数最大。即最大稠密子图
最大稠密子图的求解参见:http://wenku.baidu.com/view/986baf00b52acfc789ebc9a9.html?from=search
其思路就是将最大稠密子图转化为最大权闭合图问题的求解。最大权闭合图求解步骤:
1. 新建源S和汇T,其中权值为正的结点,连一条S->该点的边,容量为该点权值。权值为负的结点,新建一条从该点->T的边,容量为该点权值的绝对值。
2. 对于原来的结点的边的关系,容量全部设为INF
3. 求该图的最小割
4. res = 权值为正的点的权值和 - 最小割
细节
二分的时候M也记得初始化,不然有可能根本没有进入循环,M也没有定义= =
代码
#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)