Topic links
problem DescriptionSDJPX is a powful man,he controls a big country. There is n soldiers numbered 1~n (1<=n<=3000). But there was a big problem for him. He wants soldiers sorted in increasing order. He find a by-sort,but there three rules to obey.
1.He can divides soldiers into K disjoint non-empty subarrays.
2.He can sort a subarray many times untill a subarray is sorted in increasing order.
3.He can choose just the subarrays and change thier positions between themselves.
Consider A = [1 5 4 3 2] and P = 2. A possible soldiers into K = 4 disjoint subarrays is:a1 = [1],A2 = [5],A3 = [4],A4 = [3] 2],after sorting each SUBARRAY:A1 = [1],A2 = [5],A3 = [4],A4 = [2 3],after swapping A4 and a2:a1 = [1],A2 = [2 3],a3 = [4],A4 = [5].
But he wants to know for a fixed permutation, which is the maximum number of K?
Notice:every soldier have a distinct number from 1~n.there is no more than ten cases in the input.
InputFirst line is the number of cases.
For every case:
Next line is n.
Next line was the number for the N soildiers.
OutputThe maximum number of K.
Every case a line.
Sample Input251 5 4 3 254 5 1 2 3
Sample Output42 Test Instructions: Enter a 1~n arrangement, now requires the following three actions to turn it into a monotonically ascending sequence, three actions are as follows: 1, the sequence is divided into K-segment 2, can be any one of the number of segments in order to become ascending sequence (can operate on multiple segments) 3, can be exchanged for two of the position, only one time to find the largest K value? Idea: Define F[I][J] means that the interval between I and J can be divided into the number of valid segments (the valid segment indicates that the number in the interval is a sequential sequence, f[i][j] indicates that the segment is ordered, and that the segment is maintained consecutively between segments 312 and 465, 123456 consecutive), All F[i][j] can be obtained by the interval DP, and the maximum minimum value of each interval mx[][], mn[][] is also required, after which all the sub-intervals are enumerated. For each of these sub-ranges [i,j], if f[i][j]>0, then the I-to-J row is sequential, otherwise the direct enumeration computes the next interval. When F[i][j]>0,k=mx[i][j], then the interval [i,j] should be exchanged with [t,k] interval, T is not currently determined, so it is necessary to enumerate T (j<t<=k), then must have: f[1][i-1] >0& &mn[1][i-1]=1 or I==1, mn[t][k]=i,f[k+1][n]>0 && mx[k+1][n]=n or K==n, Ans=max (ans,f[1][i-1]+1+f[j+1][ T-1]+1+f[k+1][n]).
The code is as follows:
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespacestd;Const intn=3e3+5;intF[n][n];intMx[n][n],mn[n][n],r[n];intMain () {///cout<< "Hello world!" << Endl;int T; cin>>t; while(t--) { intN scanf"%d",&N); Memset (F,0,sizeof(f)); for(intI=1; i<=n;i++) {scanf ("%d",&Mx[i][i]); Mn[i][i]=Mx[i][i]; F[i][i]=1; R[i]=i; } for(intI=1; i<=n;i++) { for(intj=i+1; j<=n;j++) {Mx[i][j]=max (mx[i][j-1],mx[j][j]); MN[I][J]=min (mn[i][j-1],mn[j][j]); } } for(intI=2; i<=n;i++) { for(intj=1; j+i-1<=n;j++) { intk=j+i-1; if(mx[j][k]-mn[j][k]+1!=i) f[j][k]=0; Else { if(Mn[j][k]!=mn[j][r[j]]) f[j][k]=1; Elsef[j][k]=f[j][r[j]]+f[r[j]+1][k]; R[J]=K; } } } intans=f[1][n]; for(intI=1; i<=n;i++) { for(intj=i;j<=n;j++) { if(!f[i][j])Continue; if(i==1|| (f[1][i-1]&&mn[1][i-1]==1)) { intk=Mx[i][j]; if(K==n | | (f[k+1][n]&&mx[k+1][n]==N)) { for(intt=j+1; t<=k;t++) { if(f[t][k]&&mn[t][k]==i) ans=max (ans,f[1][i-1]+1+f[j+1][t-1]+1+f[k+1][n]); } }}}} printf ("%d\n", ans); } return 0;}
HDU 6049---sdjpx is Happy (interval dp+ enumeration)