HDU 6049-SDJPX is Happy | Multi-university Training Contest 2

Source: Internet
Author: User

Thought comes from FXXL--

A strange place is the third step can not do, that is, ans at least 1, heard that there are questions in the field, and then admin said can not do-(WA I upset)

/*hdu 6049-SDJPX is Happy [enumeration, pruning] | Multi-university Training Contest 2 Test instructions: n-length arrangement n <= 3000 Sort by three steps: 1. The original array is divided into disjoint K-segment 2. Each segment is sorted independently of 3. Select the two segment of the swap ask the number of steps can be successfully sorted by K can be taken to the maximum amount of analysis: pre-processing any segment of the minimum and maximum value of any [L,r] segment can be divided into the maximum number Using f[i,j] to indicate that the so-called effective segment first satisfies max[i,j]-min[i,j] = j-i again satisfies each paragraph in accordance with this increment, that is, the maximum value of the previous paragraph = = after the minimum of a period of [I, J] after the first paragraph for [k, T] enumeration i,j, t = max[i] [j], re-enumeration K, update the answer although the enumeration is very complex, but can be pruned such as requirements: F[i,j] > 0 && i if not 1 min[1,i-1] = 1, max[1, i-1] = i-1 similar to K, t pruning */#include <bits/stdc++.h>using namespace Std;const int N = 3005;int Max[n][n], min[n][n];int f[n][n];int T, N;int A[N], Last[N    ];void init () {memset (f, 0, sizeof (f));    int I, j, K;    for (i = 1; I <= n; i++) max[i][i] = min[i][i] = A[i];            for (k = 2, k <= N; k++) for (i = 1; i+k-1 <= N; i++) {j = i+k-1;            MAX[I][J] = max (Max[i+1][j], max[i][i]);        Min[i][j] = min (Min[i+1][j], min[i][i]);    } for (i = 1; I <= n; i++) F[i][i] = 1, last[i] = i;    for (k = 2; k <= N; k++)    for (i = 1; i+k-1 <= N; i++) {j = i+k-1;            if (Max[i][j]-min[i][j]! = j-i) Continue;            if (min[i][last[i]! = Min[i][j]) f[i][j] = 1;            else f[i][j] = F[i][last[i]] + 1;        Last[i] = j;    }}int ans;void Solve () {ans = f[1][n]; for (int i = 1; I <= n; i++) {if (I! = 1 && (!f[1][i-1] | |        MAX[1][I-1]! = i-1)) continue;            for (int j = i; J <= N; j + +) {if (!f[i][j]) continue;            int t = max[i][j]; if (t! = N && (!f[t+1][n] | | Min[t+1][n]! = T+1 | |            MAX[T+1][N]! = N)) continue; for (int k = t; k > J; k--) {if (!f[k][t] | |                Min[k][t] = i) continue; if (k > j+1) {if (!f[j+1][k-1] | | MAX[K][T]! = Min[j+1][k-1]-1 | |                MIN[I][J]! = max[j+1][k-1]+1) Continue; } else {if (max[k][t]! = min[i][j]-1)Continue            } ans = max (ans, f[1][i-1] + 2 + f[j+1][k-1] + f[t+1][n]);    }}}}int Main () {scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        for (int i = 1; I <= n; i++) scanf ("%d", a+i);        Init ();        Solve ();    printf ("%d\n", ans); }}

  

HDU 6049-SDJPX is Happy | Multi-university Training Contest 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.