hdu 6049---Sdjpx Is Happy(区间DP+枚举)
2018-06-17 22:08:30来源:未知 阅读 ()
题目链接
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.
For every case:
Next line is n.
Next line is the number for the n soildiers.
Every case a line.
#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; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- CodeForces 1313D Happy New Year 2020-03-04
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash