Problem Description
Sdjpx 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.
 
Input
First line is the number of cases.
For every case:
Next line is n.
Next line is the number for the n soildiers.
 
Output
the maximum number of K.
Every case a line.
 
Sample Input
2
5
1 5 4 3 2
5
4 5 1 2 3
 
Sample Output
4
2

Hint

Test1: Same as walk through in the statement. Test2: [4 5] [1 2 3] Swap the 2 blocks: [1 2 3] [4 5].

 
启发博客:http://www.cnblogs.com/FxxL/p/7253028.html
题意: 给出n,一个1~n的排列,要求进行三步操作
           1.分区(随便分)
           2.对分好的每一个区内进行从小到大的排序
           3.挑选两个区进行交换(只能挑选两个,只能交换易次),使得序列的顺序变成1-n;
          问在满足要求的情况下,最多能分成多少区
题解:第一步是分区,第二步是枚举。
          分区是开了一个f[i][j]数组用来记录,i-j区间里可以有多少满足要求的段,用到mx,mi,r来辅助,具体可见代码注释。
          枚举是枚举要交换的两个区间,每次更新答案的最大值。设左右区间分别为seg_a,seg_b。
          seg_a要满足:第一段或者之前包括1~i-1的所有数字,当然自身不能为空
                                把这个区间最大的数定义为k,根据k来枚举seg_b,k是seg_b的右端点
                                还需满足k==n或者k+1及以后的所有数字包含k+1~n
          seg_b要满足:k是seg_b的右端点,自身不为空,要保证它的最小值是i
          像上述这样来做即可,当然中间会有一些不小心造成的WA点,大家注意即可
 
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
#define MAXN 3005 int a[MAXN],res,n;
int mi[MAXN][MAXN],mx[MAXN][MAXN];
//mi[i][j]表示从i到j的最小值,mx[i][j]表示从i到j的最大值
int f[MAXN][MAXN],r[MAXN];
//f[i][j]表示从i到j可以分成的区间数,r[i]表示最近一次从i开始的区间的右端(方便更新) void init()//第一步,分块
{
memset(mi,,sizeof(mi));
memset(mx,,sizeof(mx));
memset(f,,sizeof(f));
memset(r,,sizeof(r));
for(int i=;i<=n;i++)
{
mi[i][i]=a[i];
mx[i][i]=a[i];
f[i][i]=;
r[i]=i;
}
//为mi,mx赋值
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
{
mx[i][j]=max(a[j],mx[i][j-]);
mi[i][j]=min(a[j],mi[i][j-]);
}
//为f数组赋值
for(int t=;t<=n;t++)//t在枚举区间长度
for(int i=;i+t-<=n;i++)
{
int j=i+t-;
//不是连续的一段无法分区间
if(mx[i][j]-mi[i][j]!=t-)
f[i][j]=;
else
{
//j一定大于r[i]
if(mi[i][r[i]]>mi[i][j])
f[i][j]=;
else
f[i][j]=f[i][r[i]]+f[r[i]+][j];
r[i]=j;//这个r数组很精华
}
}
} void solve()//第二步,枚举找交换区间
{
int k;
res=max(,f[][n]);//WA点,一开始写成res=1就WA了
//先枚举seg_a
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++)
{
//满足条件才能继续枚举seg_b
if(i==||(f[][i-]!=&&mi[][i-]==))
{
k=mx[i][j];
if(f[i][j]&&(k==n||(f[k+][n]!=&&mx[k+][n]==n)))
{
for(int t=j+;t<=k;t++)
{
if(f[t][k]&&mi[t][k]==i)
{
//printf("%d %d %d %d %d\n",i,j,t,k,f[1][i-1]+1+f[j+1][t-1]+1+f[k+1][n]);
res=max(res,f[][i-]++f[j+][t-]++f[k+][n]);
}
}
}
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
init();
solve();
printf("%d\n",res);
}
return ; }
             
 

HDU 6049 17多校2 Sdjpx Is Happy(思维题difficult)的更多相关文章

  1. HDU 6140 17多校8 Hybrid Crystals(思维题)

    题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...

  2. HDU 6034 17多校1 Balala Power!(思维 排序)

    Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...

  3. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

  4. HDU 6045 17多校2 Is Derek lying?

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  5. HDU 6124 17多校7 Euler theorem(简单思维题)

    Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. Bu ...

  6. HDU 3130 17多校7 Kolakoski(思维简单)

    Problem Description This is Kolakosiki sequence: 1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1……. This seq ...

  7. HDU 6038 17多校1 Function(找循环节/环)

    Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1. D ...

  8. HDU 6103 17多校6 Kirinriki(双指针维护)

    Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑i=0n− ...

  9. HDU 6098 17多校6 Inversion(思维+优化)

    Problem Description Give an array A, the index starts from 1.Now we want to know Bi=maxi∤jAj , i≥2. ...

随机推荐

  1. csu oj 1341 string and arrays

    Description 有一个N*N的字符矩阵,从上到下依次记为第1行,第2行,……,第N行,从左至右依次记为第1列,第2列,……,第N列. 对于这个矩阵会进行一系列操作,但这些操作只有两类: (1) ...

  2. 关于click的多次触发问题(冒泡事件)

    1. 问题描述: 在点击事件触发时调用接口,若用户多次点击会造成多次调用接口,有时会引起一些数据错误的问题,如支付页面,点击多次时会在后台生成多个相同订单 解决方法: (1)加flag,让点击事件只执 ...

  3. 借助python工具从word文件中抽取相关表的定义,最后组装建表语句-非常好

    借助python工具从word文件中抽取表的定义,最后组装建表语句-非常好 --如有转载请以超链接的方式注明原文章出处,谢谢大家.请尊重每一位乐于分享的原创者 1.python脚本 ## -*- co ...

  4. 9. Palindrome Number(回文数)C++

    将int转换为string,注意for循环判断条件的简化 class Solution { public: bool isPalindrome(int x) { ) return false; str ...

  5. 四、触发器(Trigger)

    一.触发器 有点类似AOP里的拦截器,触发器不能传递参数,也不能输出参数,也不能显式调用,只有当满足触发器条件的时候Oracle会自动调用. 触发器: 1.语句级别的触发器:CRUD操作 2.行级别的 ...

  6. E - Let's Go Rolling!

    题目描述:数轴上有nn个质点,第ii个质点的坐标为xixi,花费为cici,现在要选择其中一些点固定,代价为这些点的花费,固定的点不动,不固定的点会向左移动直至遇到固定的点,代价是这两点的距离,如果左 ...

  7. jQuery-全屏滚动插件【fullPage.js】API 使用方法总结

    jQuery-全屏滚动插件[fullPage.js]API 使用方法总结   jQuery-全屏滚动插件fullPage.js使用方法总结 作者github及下载地址:https://github.c ...

  8. Swagger 路径过滤 -PreSerializeFilters

    Swagger 默认显示所有api, 如果要做路径过滤,可以这样做. //过滤,只显示部分api app.UseSwagger(c=> { c.PreSerializeFilters.Add(( ...

  9. python(2)之列表

    列表的使用 names=["zhangyang","liming",["sese","popo"],"xiao ...

  10. JQuery对象和DOM对象的区别与转换

    刚开始学习JQuery,经常分不清楚哪些是JQuery对象,哪些是DOM对象,了解它们之间的关系是很有必要的. 1.DOM对象和JQuery对象的区别 1)  DOM对象 DOM是Document O ...