Hdu-6049 Sdjpx Is Happy(贪心)

185 篇文章 0 订阅
116 篇文章 0 订阅
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 =  15432 15432 and P = 2. A possible soldiers into K = 4 disjoint subarrays is:A1 =  1 1,A2 =  5 5,A3 =  4 4,A4 =  32 32,After Sorting Each Subarray:A1 =  1 1,A2 =  5 5,A3 =  4 4,A4 =  23 23,After swapping A4 and A2:A1 =  1 1,A2 =  23 23,A3 =  4 4,A4 =  5 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
分析:我们考虑对于一个最优解,它最后交换的部分要么是acb的形式要么是ab的形式,如果是acb的形式,那么
Min(a) = Max(c) + 1,Max(b) = Min(c) - 1,我们可以事先把整个序列直接贪心拆分,acb 或 ab一定是完整的一块,直接暴力枚举c就行了,总复杂度O(n^2).
f[i][j] 表示 i 到 j最多拆成几块,枚举左端点i,然后从左往右扫,遇到恰好连续的计数器就+1并且把值赋给f[i][j],如果最小值变化了计数器就清零,这样就可以n^2的预处理出来f[i][j].

 
 
#include <bits/stdc++.h>
#define N 3005
using namespace std;
typedef long long ll;
int T,n,p[N],fi[N][N],fa[N][N],block[N],l[N],r[N],f[N][N];
bool check2(int l,int r)
{
    return ((fa[l][r] - fi[l][r]) == r - l);
}
bool check1(int l,int r)
{
    return (check2(l,r) && fi[l][r] == l);
}
int main()
{
    cin.sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        memset(l,0,sizeof(l));
        memset(f,0,sizeof(f));
        memset(fi,0,sizeof(fi));
        memset(fa,0x3f,sizeof(fa));
        memset(block,0,sizeof(block));
        cin>>n;
        for(int i = 1;i <= n;i++) cin>>p[i];
        for(int i = 1;i <= n;i++)
        {
            fi[i][i] = fa[i][i] = p[i];
            for(int j = i+1;j <= n;j++) fi[i][j] = min(fi[i][j-1],p[j]),fa[i][j] = max(fa[i][j-1],p[j]);
        }
        int now = 1;
        for(int i = 1;i <= n;i++)
        {
            if(!l[now]) l[now] = i;
            r[now] = i;
            block[i] = now;
            if(check1(1,i)) now++;
        }
        for(int i = 1;i <= n;i++)
        {
            int cnt = 1;
            f[i][i] = 1;
            for(int j = i+1;j <= n;j++)
            {
                if(fi[i][j-1] != fi[i][j]) cnt = 0;
                if(check2(i,j)) f[i][j] = ++cnt;
            }
        }
        int ans = block[n];
        for(int i = 1;i <= n;i++)
         for(int j = i;j <= n;j++)
          if(block[i] == block[j] && check2(i,j))
          {
              int lab = block[i],L = l[lab],R = r[lab];
              if(i == L || j == R) continue;
              if(!check2(L,i-1) || !check2(j+1,R)) continue;
              if(fi[L][i-1] != fa[i][j] + 1 || fa[j+1][R] != fi[i][j] - 1) continue;
              ans = max(ans,block[n] + 1 + f[i][j]);
          }
        for(int i = 1;i <= n;i++)
        {
            int lab = block[i],L = l[lab],R = r[lab];
            if(!check2(L,i) || L == R  || i == R || fa[L][i] != fa[L][R]) continue;
            ans = max(ans,block[n]+1);
        }
        cout<<ans<<endl;
    }
}









评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值