Problem C.   The Problem Needs 3D Arrays

  Time Limit: 6000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Description

A permutation is a sequence of integers p1,p2,...,pn, consisting of n distinct positive integers and each of them does not exceed n. Assume that r(S) of sequence S denotes the number of inversions in sequence S (if i < j and Si > Sj, then the pair of (i,j) is called an inversion of S), l(S) of sequence S denotes the length of sequence S. Given a permutation P of length n, it’s your task to find a subsequence S of P with maximum. A subsequence of P is a sequence (pi1,pi2,...,pit) which satisfies that 0 < i1 < i2 < ... < it n.

Input

The first line of the input gives the number of test cases, T. T test cases follow.

For each test case, the first line contains an integer n (1 ≤ n ≤ 100), the length of the permutation P. The second line contains n integers p1,p2,...,pn, which represents the permutation P.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximum.

Your answer will be considered correct if it is within an absolute error of 10−6 of the correct answer.

Samples

Sample Input

Sample Output

1

5

3 4 2 5 1

Case #1: 1.250000000000

解题:最大密度子图转为最大权闭合图

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int n,m,tot,S,T,x[maxn],y[maxn],A[];
struct arc {
int to,next;
double flow;
arc(int x = ,double y = ,int z = -) {
to = x;
flow = y;
next = z;
}
} e[maxn<<];
int head[maxn],cur[maxn],d[maxn];
void add(int u,int v,double flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
queue<int>q;
bool bfs() {
memset(d,-,sizeof d);
while(!q.empty()) q.pop();
q.push(S);
d[S] = ;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow > && d[e[i].to] == -) {
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
double dfs(int u,double low) {
if(u == T) return low;
double tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].flow > && d[e[i].to] == d[u] + &&(a=dfs(e[i].to,min(e[i].flow,low)))>) {
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(low <= ) break;
}
}
if(tmp <= ) d[u] = -;
return tmp;
}
bool dinic() {
double ans = m;
while(bfs()) {
memcpy(cur,head,sizeof head);
ans -= dfs(S,INF);
}
return ans <= ;
}
void build(double delta) {
memset(head,-,sizeof head);
for(int i = tot = ; i < m; ++i) {
add(S,i + n + ,1.0);
add(i + n + ,x[i],INF);
add(i + n + ,y[i],INF);
}
for(int i = ; i <= n; ++i) add(i,T,delta);
}
int main() {
int cm = ,cs;
scanf("%d",&cs);
while(cs--) {
scanf("%d",&n);
m = ;
for(int i = ; i <= n; ++i) {
scanf("%d",A+i);
for(int j = i-; j > ; --j)
if(A[j] > A[i]) {
x[m] = j;
y[m++] = i;
}
}
S = ;
T = n + m + ;
double low = ,high = m,ans = ;
if(m == ) {printf("Case #%d: %.7f\n",cm++,ans);continue;}
while(high - low > 1e-){
double mid = (low + high)/2.0;
build(mid);
if(dinic()) ans = high = mid;
else low = mid;
}
printf("Case #%d: %.7f\n",cm++,ans);
}
return ;
}

Gym - 100548C The Problem Needs 3D Arrays的更多相关文章

  1. Gym - 100548C The Problem Needs 3D Arrays (最大密度子图)

    TK在大多数 Unix平台.Windows平台和Macintosh系统都是预装好的,TKinter 模块是 Tk GUI 套件的标准Python接口.可实现Python的GUI编程. Tkinter模 ...

  2. 2014 西安 The Problem Needs 3D Arrays

    The Problem Needs 3D Arrays 题意:给你n个数, 然后1-n的数, 然后要求按顺序选出m个数, 求 逆序数/m 个数的 最大值是多少. 题解:裸的最大密度子图.逆序的2个数建 ...

  3. 14西安区域赛C - The Problem Needs 3D Arrays

    最大密度子图裸题,详情请见胡博涛论文: https://wenku.baidu.com/view/986baf00b52acfc789ebc9a9.html 不加当前弧优化t到死= = //#prag ...

  4. Uvalive 7037 The Problem Needs 3D Arrays(最大密度子图)

    题意:给一段子序列,定义密度:子序列中的逆序对数/子序列的长度 求这个序列的对大密度. 分析:将序列中的每个位置视作点,逆序对\(<i,j>\)之间表示点i与点j之间有一条无向边.所以就转 ...

  5. UVALive 7037:The Problem Needs 3D Arrays(最大密度子图)

    题目链接 题意 给出n个点,每个点有一个值,现在要选择一些点的集合,使得(选择的点生成的逆序对数目)/(选择的点的数量)的比率最大. 思路 点与点之间生成一个逆序对可以看做是得到一个边,那么就是分数规 ...

  6. Codeforces Gym 100338B Geometry Problem 计算几何

    Problem B. Geometry ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  7. Gym - 100548G The Problem to Slow Down You

    依然是回文树. 我们只需要吧siz[]改成统计两边的siz[][0/1],然后把两个字符中间随便加一个不会出现的字符拼起来,做一遍回文树统计一下就OJBK了 #include<bits/stdc ...

  8. Gym - 100548H The Problem to Make You Happy 2014-2015 ACM-ICPC, Asia Xian Regional Contest (BFS+博弈)

    题意:Bob和Alice在一张有向无环图上移动,给定二者的起点,Bob先手.Bob的失败条件是不能移动或者与Alice相遇.两个人都采取最优策略,求Bob是否会赢 分析:银牌题.先确定所有的失败状态, ...

  9. The Basics of 3D Printing in 2015 - from someone with 16 WHOLE HOURS' experience

    全文转载自 Scott Hanselman的博文. I bought a 3D printer on Friday, specifically a Printrbot Simple Metal fro ...

随机推荐

  1. SPFA的小优化

    标签:闲扯 SPFA的小优化 1. 向队尾加入元素时,如果它比对首还优,就把把它直接和队首交换. 拿一个双端队列来实现 (手写 , head ,tail   STLdeque亲测及其慢) 这个小优化其 ...

  2. HTML标签和文档结构

    HTML标签与文档结构 HTML作为一门标记语言,是通过各种各样的标签来标记网页内容的.我们学习HTML主要就是学习的HTML标签. 那什么是标签呢? #1.在HTML中规定标签使用英文的的尖括号即` ...

  3. [转载]-win7启动本地MongoDB的四种方式

    2016年04月07日 09:52:34 cherry__cheng 阅读数:19451 标签: win7启动本地MongoDB的四种方式快速启动本地mongodb 更多 个人分类: mongodb& ...

  4. 洛谷 P1273 有线电视网 && caioj 1109 树形动态规划(TreeDP)4:比赛转播(树上分组背包总结)

    从这篇博客往前到二叉苹果树都可以用分组背包做 这依赖性的问题,都可以用于这道题类似的方法来做 表示以i为根的树中取j个节点所能得的最大价值 那么每一个子树可以看成一个组,每个组里面取一个节点,两个节点 ...

  5. 一种提高单片机i/o口驱动能力的方法

    一.简述问题 当你用单片驱动发光二极管的时,你还感觉不到P0.P1口的差别.(10-20mA之间,当中P0驱动能力最强,但对于驱动直流电机依旧非常弱.其结果就是电机不转).那么有什么办法提高驱动能力吗 ...

  6. POJ2411 状态压缩dp

    POJ2411 http://poj.org/problem?id=2411

  7. flex 光标(CursorManager)

    flex 光标(CursorManager)  CursorManager相关属性   getInstance():ICursorManager AIR 应用程序中的每个 mx.core.Window ...

  8. 原生js实现多组图片切换

    这几天一直在练习原生js写效果,需要理清自己的逻辑,做了一个切换多组图片的效果: css样式: * { margin: 0; padding: 0; } body { background: #303 ...

  9. 对比《动手学深度学习》 PDF代码+《神经网络与深度学习 》PDF

    随着AlphaGo与李世石大战的落幕,人工智能成为话题焦点.AlphaGo背后的工作原理"深度学习"也跳入大众的视野.什么是深度学习,什么是神经网络,为何一段程序在精密的围棋大赛中 ...

  10. 紫书 例题 9-6 UVa 11400 (线性结构上的动态规划)

    这道题的下标从1开始比较方便,一方面前缀和算的方便一些,一方面涉及到前j 个灯泡,那么如果从0开始,前3个灯泡就是第0, 1, 2, 3个,非常奇怪. 所以灵活换下标. 然后这道题的动规有点暴力枚举的 ...