hdu 6049---Sdjpx Is Happy(區間DP+枚舉)

來源:互聯網
上載者:User

標籤:區間dp   str   nbsp   get   into   sub   輸入   應該   from   

題目連結

 

Problem DescriptionSdjpx is a powful man,he controls a big country.There are n soldiers numbered 1~n(1<=n<=3000).But there is a big problem for him.He wants soldiers sorted in increasing order.He find a way to 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 two 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 ,what is the the maximum number of K?
Notice: every soldier has a distinct number from 1~n.There are no more than 10 cases in the input.  InputFirst line is the number of cases.
For every case:
Next line is n.
Next line is 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  題意:輸入一個1~n的排列,現在要求通過以下三種動作將之變成一個單調上升的序列,三種動作如下:          1、將這個序列分成K段          2、可以將任意一個段中的數進行排序,使之變成上升的序列(可以對多個段進行操作)     3、可以對其中的兩個段交換位置,只能交換一次          求最大的K值? 思路:定義f[i][j]表示 i 到 j 之間的區間能分成多少個合法的段(合法的段表示這個區間中的數排序後是一個連續的數列,f[i][j]表示段內排好序後,段與段之間也是保持連續的如段312 和 465 ,123456連續), 通過區間DP可以求出所有 f[i][j],還需要求出每個區間的最大值最小值 mx[][] , mn[][] , 之後枚舉所有的子區間。         對於其中每一個子區間[i,j] , 如果f[i][j]>0,那麼就表示i到j排完序後是連續的,否則直接枚舉計算下一個區間。當 f[i][j]>0,k=mx[i][j] , 那麼區間[i,j]應該和[t,k]區間進行交換位置,t目前還不確定,所以需要枚舉t (j<t<=k), 那麼必須有: f[1][i-1] >0&&mn[1][i-1]=1 或者i==1  ,mn[t][k]=i,f[k+1][n]>0 && mx[k+1][n]=n 或者 k==n ,ans=max(ans,f[1][i-1]+1+f[j+1][t-1]+1+f[k+1][n]) 。

代碼如下:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int N=3e3+5;int f[N][N];int mx[N][N],mn[N][N],R[N];int main(){    ///cout << "Hello world!" << endl;    int T; cin>>T;    while(T--)    {       int n; scanf("%d",&n);       memset(f,0,sizeof(f));       for(int i=1;i<=n;i++)       {          scanf("%d",&mx[i][i]);          mn[i][i]=mx[i][i];          f[i][i]=1;          R[i]=i;       }       for(int i=1;i<=n;i++)       {           for(int j=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(int i=2;i<=n;i++)       {           for(int j=1;j+i-1<=n;j++)           {               int k=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;                  else f[j][k]=f[j][R[j]]+f[R[j]+1][k];                  R[j]=k;               }           }       }       int ans=f[1][n];       for(int i=1;i<=n;i++)       {           for(int j=i;j<=n;j++)           {               if(!f[i][j]) continue;               if(i==1 || (f[1][i-1]&&mn[1][i-1]==1))               {                   int k=mx[i][j];                   if(k==n || (f[k+1][n]&&mx[k+1][n]==n))                   {                       for(int t=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(區間DP+枚舉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.