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

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

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = ;
  4. const int INF = 0x3f3f3f3f;
  5. int n,m,tot,S,T,x[maxn],y[maxn],A[];
  6. struct arc {
  7. int to,next;
  8. double flow;
  9. arc(int x = ,double y = ,int z = -) {
  10. to = x;
  11. flow = y;
  12. next = z;
  13. }
  14. } e[maxn<<];
  15. int head[maxn],cur[maxn],d[maxn];
  16. void add(int u,int v,double flow) {
  17. e[tot] = arc(v,flow,head[u]);
  18. head[u] = tot++;
  19. e[tot] = arc(u,,head[v]);
  20. head[v] = tot++;
  21. }
  22. queue<int>q;
  23. bool bfs() {
  24. memset(d,-,sizeof d);
  25. while(!q.empty()) q.pop();
  26. q.push(S);
  27. d[S] = ;
  28. while(!q.empty()) {
  29. int u = q.front();
  30. q.pop();
  31. for(int i = head[u]; ~i; i = e[i].next) {
  32. if(e[i].flow > && d[e[i].to] == -) {
  33. d[e[i].to] = d[u] + ;
  34. q.push(e[i].to);
  35. }
  36. }
  37. }
  38. return d[T] > -;
  39. }
  40. double dfs(int u,double low) {
  41. if(u == T) return low;
  42. double tmp = ,a;
  43. for(int &i = cur[u]; ~i; i = e[i].next) {
  44. if(e[i].flow > && d[e[i].to] == d[u] + &&(a=dfs(e[i].to,min(e[i].flow,low)))>) {
  45. e[i].flow -= a;
  46. e[i^].flow += a;
  47. low -= a;
  48. tmp += a;
  49. if(low <= ) break;
  50. }
  51. }
  52. if(tmp <= ) d[u] = -;
  53. return tmp;
  54. }
  55. bool dinic() {
  56. double ans = m;
  57. while(bfs()) {
  58. memcpy(cur,head,sizeof head);
  59. ans -= dfs(S,INF);
  60. }
  61. return ans <= ;
  62. }
  63. void build(double delta) {
  64. memset(head,-,sizeof head);
  65. for(int i = tot = ; i < m; ++i) {
  66. add(S,i + n + ,1.0);
  67. add(i + n + ,x[i],INF);
  68. add(i + n + ,y[i],INF);
  69. }
  70. for(int i = ; i <= n; ++i) add(i,T,delta);
  71. }
  72. int main() {
  73. int cm = ,cs;
  74. scanf("%d",&cs);
  75. while(cs--) {
  76. scanf("%d",&n);
  77. m = ;
  78. for(int i = ; i <= n; ++i) {
  79. scanf("%d",A+i);
  80. for(int j = i-; j > ; --j)
  81. if(A[j] > A[i]) {
  82. x[m] = j;
  83. y[m++] = i;
  84. }
  85. }
  86. S = ;
  87. T = n + m + ;
  88. double low = ,high = m,ans = ;
  89. if(m == ) {printf("Case #%d: %.7f\n",cm++,ans);continue;}
  90. while(high - low > 1e-){
  91. double mid = (low + high)/2.0;
  92. build(mid);
  93. if(dinic()) ans = high = mid;
  94. else low = mid;
  95. }
  96. printf("Case #%d: %.7f\n",cm++,ans);
  97. }
  98. return ;
  99. }

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. ES6学习笔记(十七)Class 的基本语法

    1.简介 类的由来 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y = y; ...

  2. [JLOI2015]装备购买(线性基)

    [JLOI2015]装备购买 题目描述 脸哥最近在玩一款神奇的游戏,这个游戏里有 nn 件装备,每件装备有 \(m\) 个属性,用向量 \(\mathbf{z_i}\)=\((a_1, \ldots ...

  3. POJ——T 1470 Closest Common Ancestors

    http://poj.org/problem?id=1470 Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20830   ...

  4. svn 的使用(二)

    这篇主要介绍下 svn 钩子的使用,svn 的安装以及配置等能够查看svn 的使用(一) 我们能够在svn创建的仓库目录下看到 hooks 目录. 这里面就存放这个各种svn操作同一时候会运行的脚本文 ...

  5. Oracle拾遗

    这次学习Oracle视频.还是学到了不少东西的. 首先,这是一次系统的学习.对自己的知识体系是一次查漏补缺,曾经仅仅是简单的会用,如今看到出的问题,非常easy就能够想到是哪一部分出了问题.尤其是如今 ...

  6. C++语言笔记系列之十——静态成员

    1.静态成员 (1)由keywordstatic修饰 静态变量定义语句在编译阶段就运行,运行过程中不再运行. (2)分类:静态数据成员.静态成员函数. (3)静态成员时类的全部对象共享的成员,而不是某 ...

  7. ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】

    题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...

  8. C++ STL:vector

      不定长数组:vetor 它就像一个二维数组.仅仅是第一维的大小是固定的,可是第二维的大小不固定. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAx ...

  9. nios sgdma(Scatter-Gather dma)示例

    在 Quartus7.2之后的版本中,除了原有的基于avalon-mm总线的DMA之外,还增加了Scatter-Gather DMA这种基于avalon-ST流总线的DMA IP核,它更适合与大量数据 ...

  10. Centos7 zabbix3.4.6的安装部署 (一)

    部署zabbix主要为了监控日常主机.服务器.Web服务器.数据库.路由器.交换机等日常设备,功能强大,稳定性好 现在通过使用虚拟机VM搭建的Centos7部署zabbix服务 实现简单监控功能 本章 ...