Problemc. The problem Needs 3D Arrays
Description
A permutation is asequence of integers p1, p2, ..., pn,consisting of n distinct positive integers and each of them does Notexceed N. Assume that R (s) of sequence s denotes the number ofinversions in sequence S (if I < J and Si > Sj,the n The pair of (I, J) is called an inversion of s), L (s) ofsequence s denotes the length of sequence s. Given a permutation p oflength N, it's your task to find a subsequence s of P with MAXIMUMR (s)/L (s). A subsequence of P is a sequence (Pi1, Pi2,..., pit) which satisfies that 0 < I1 <i2 < ... < it≤n.
Input
The first line ofthe 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 ofthe permutation P. The second line contains n integers p1,p2, ..., PN, which represents thepermutation P.
Output
For each test case,output-one line containing ' case #x: Y ', where x is the ' Test Casenumber (starting from 1) and Y-is the Maximum R (s)/L (s).
Your answer would beconsidered correct if it is within an absolute error of 10?6of the correct answer.
Samples
Sample Input |
Sample Output |
1 5 3 4 2 5 1 |
Case #1:1.250000000000 |
Knowledge Points:
Maximum density sub-graph, most powerful closed graph, minimum cut.
Main topic:
give the1~NitNAn arrangement of a positive integerP, required inPTo find a sub-sequence in theS, so that the number of reverse order contained in the subsequence is logarithmicR (S)and the length of the subsequenceL (S)The ratio is the largest. The maximum ratio of outputR (s)/L (s).
Problem Solving Ideas:
You can view each number as a point in the graph and convert the relationship in reverse order to an edge in the diagram. That makes up an no-show graph. The sample can be converted to:
What is required now is to select points in the diagram and the edges they are connected to each other to form a closed sub-graph. The ratio of the number of edges in the graph (the inverse logarithm) to the number of points (selected numbers) is the largest. This is the model of a maximum density sub-graph. The best selection scheme of the sample is to select the points and edges of the red callout in the figure, a total of 4 points 5 edges, the ratio is 1.25.
according to Hubertau's paper, "The application of the minimum cut model in the informatics competition", you can use0-1model for fractional planning. According to the score plan general steps, two points find the answer. The Guess value for an answerG, transforming the original into a networkG (V,e)the process, that is, in the original point setVon the basis of increasing the sourceSand SinksTTo replace each original non-forward edge with two capacity1has a forward edge(U,V)and the(V,u)To increase the connection source to the original image at each point of the forward edge(S,V), the capacity isUincrease the connection of the original image each pointVto SinksThas a forward edge(v,t), the capacity is(U+2g?d[v])(whereD[V]is aVthe degree of the point). Find the minimum cut (equal to the maximum flow) of the network, if(uxn-Minimum Cut)/2 >=0, The Guess valueGIt's doable, zoom in.G, continue two points;<0it is not feasible to reduceG, continue the two points. Finally, the most feasibleG. The maximum ratio can be obtained.
Reference Code:(not AC )
#include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue > #include <cmath>using namespace std;const double INF = 1 << 30;const double EPS = 1e-12;const int MAXN = 1 10;const int MAXM = 50010;struct Edge {int u, V, next; Double C; Edge () {} edge (int _u, int _v, double _c, int _next): U (_u), V (_v), C (_c), Next (_next) {}} Edge[maxm];int Head[maxn], Cnt;bool Visited[maxn];int PATH[MAXN], D[MAXN], SRC, to;int N, M, Ncase, Ccase, P[MAXN], degree[maxn];vector<pair<i NT, int> > e;void addedge (int u, int v, double c) {edge[cnt] = Edge (U, V, C, Head[u]); Head[u] = cnt++; EDGE[CNT] = Edge (V, u, 0, head[v]); HEAD[V] = cnt++;} BOOL BFs () {memset (d, 0, sizeof (d)); Queue<int> Q; Q.push (SRC); D[SRC] = 1; while (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V; if (edge[i].C &&!d[v]) {D[v] = D[u] + 1; Q.push (v); }}} return d[to];} Double dfs (int x, double pf) {if (x = = to) return PF; DOUBLE ret = 0; for (int i = head[x]; I! =-1 && pf; i = edge[i].next) {int v = EDGE[I].V; if (edge[i].c && d[v] = = D[x] + 1) {Double p = dfs (V, min (edge[i].c, pf)); EDGE[I].C-= p; edge[i ^ 1].c + = p; ret + = P; PF-= p; }} if (!ret) d[x] =-2; return ret;} Double Dinic () {double ret = 0.0; while (BFS ()) ret + = DFS (src, INF); return ret;} inline void Buildedge (double g) {memset (head,-1, sizeof (head)); CNT = 0; src = 0; to = n + 1; for (int i = 0; i < m; i++) {Addedge (E[i].first, E[i].second, 1.0); Addedge (E[i].second, E[i].first, 1.0); } for (int i = 1; I <= n; i++) {Addedge (src, I, M * 1.0); Addedge (I, To, M * 1.0 + 2.0 * G-degrEe[i]); }}void init () {e.clear (); memset (degree, 0, sizeof (degree));} void input () {scanf ("%d", &n); for (int i = 1; I <= n; i++) {scanf ("%d", &p[i]); }}inline bool Check (double g) {Buildedge (g); Double h = (1.0 * m * n-dinic ())/2.0; return h > 0;} void Solve () {for (int i = 1; I <= N, i++) {for (int j = i+1; J <= N; j + +) {if (P[i] > p[ J]) {E.push_back (Make_pair (i, j)); degree[i]++; degree[j]++; }}} m = E.size (); if (M = = 0) {printf ("Case #%d:%.12lf\n", ++ccase, 0); Return } Double L = 1.0/n-eps, r = m * 1.0 + EPS; while (R-l > EPS) {Double mid = (L + r)/2.0; if (check (mid)) {L = mid; } else {r = Mid; }} printf ("Case #%d:%.12lf\n", ++ccase, L);} int main () {scanf ("%d", &ncase); while (ncase--) {init (); InpUT (); Solve (); } return 0;}
Welcome to discuss correct.
CF GYM 100548 The problem Needs 3D Arrays (2014ACM Xi ' an field race problem C)