CF #737(div2)B. Moamen and k-subarrays 贪心

题意 :

  • 给一个长为 n 的序列(distinct),将其分为 <= k 个连续的序列,对这些段任意组合,求能否得到递增序列。

思路 :

  • 记录在原序列中每个distinct的位置,然后在结果序列中和原序列一一比对。
  • 2s,且O(2^n)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <map>
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const double pi = acos(-1);
typedef long long ll;

const int N = 1e5 + 10;

int a[N], b[N];

int main()
{
    IOS;
    
    int T;
    cin >> T;
    
    while (T -- )
    {
        int n, k;
        cin >> n >> k;
        unordered_map<int, int> ma;
        for (int i = 1; i <= n; i ++ ) cin >> a[i], b[i] = a[i], ma[a[i]] = i;
        
        sort(b + 1, b + n + 1);
        
        int cnt = 0;
        for (int i = 1; i <= n; i ++ )
        {
            cnt ++ ;
            int j = ma[b[i]];
            for ( ; j + 1 <= n; j ++ )
                if (b[i + 1] == a[j + 1]) i ++ ;
                else break;
        }
        if (cnt <= k) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值