njust sequence(二分查找)

题意

Time Limit: 1000MS
Memory Limit: 65536KB

Description

将一个给定的数列,拆分成K个不降序列,每个数出现且只出现一次,且在各序列中各个数相对于原数列的相对顺序不变。

如7 6 9 8 10可以拆成 7 9 10和6 8。求最小的K值。

Input

第一行输入一个整数T(1 <= T <= 100),表示接下来T组测试数据,

每组两行,第一行为n,代表数列长度(1<=n<=10000)

接下来一行有n个数,空格分隔(每个数<=50000)。

Output

对每组数据输出一个最小的K值。

Sample Input

2
5
7 6 9 8 10
5
5 4 3 2 1

Sample Output

2
5

思路

维护一个数组,保存多个非递减序列的最后一个值。

显然,这个数组一定是递减的。

例如上面样例中的 7 6 9 8 10

初始时,我们令a[0] = 7,表示目前有一个序列,最后一个值为7

接下来到6了,因为6小于7,所以只能使用一个新的序列,令a[1] = 6

到9,我们发现9既可以放在7后面,也可以放在6后面,那我们当然选择在7后面放,相比放在6后面会更好。

也就是说,假如当前需要放入x,那就在数组中找小于等于x的最大值,更新它为x。

因为数组是递减的,所以寻找的这个过程可以使用二分查找。

代码

#include<iostream>
#include<algorithm>
#include<list>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stack>
#include<queue>
using namespace std;
#define LL long long

int d[50009];

int find(int l, int r, int x)
{
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(x < d[mid])
            l = mid+1;
        else if(x > d[mid])
            r = mid-1;
        else
            return mid;
    }
    return l;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        int len = 0;
        d[len] = 0;
        for(int i=0; i<n; i++)
        {
            int t;
            scanf("%d", &t);
            int z = find(0, len, t);
            if(z > len)
                d[++len] = t;
            else
                d[z] = t;
        }
        printf("%d\n", len+1);
    }
    return 0;
}
时间: 2024-10-14 20:14:24

njust sequence(二分查找)的相关文章

1085. Perfect Sequence (25)【二分查找】——PAT (Advanced Level) Practise

题目信息 1085. Perfect Sequence (25) 时间限制300 ms 内存限制65536 kB 代码长度限制16000 B Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M <= m * p where M and m are the maximum and minim

HDU 5288 OO&#39;s sequence (2015多校第一场 二分查找)

OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 955    Accepted Submission(s): 358 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the nu

JAVA源码走读(二)二分查找与Arrays类(未完)

给数组赋值:通过fill方法. 对数组排序:通过sort方法,按升序.比较数组:通过equals方法比较数组中元素值是否相等.查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找法操作. 使用如下: int[] array = new int[5]; //填充数组 Arrays.fill(array, 5); System.out.println("填充数组:Arrays.fill(array, 5):"); test.output(array); //将数组的第

HDU 3763 CD【二分查找】

解题思路:给出两个数列an,bn,求an和bn中相同元素的个数因为注意到n的取值是0到1000000,所以可以用二分查找来做,因为题目中给出的an,bn,已经是单调递增的,所以不用排序了,对于输入的每一个b[i],查找它在an数列中是否存在.存在返回值为1,不存在返回值为0 CD Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 627

POJ——3061Subsequence(尺取法或二分查找)

Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11224   Accepted: 4660 Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) ar

Can you find it?——[二分查找]

Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. Input There are many cases. Every data case is descr

Python 实现二分查找算法

最近在学习python,由于在面试中,二分查找算法面试率极高,所以使用python做了一个实现. def search1(sequence, number): lower = 0 upper = len(sequence) - 1 while lower <= upper: mid = (lower + upper) // 2 if number > sequence[mid]: lower = mid + 1 elif number < sequence[mid]: upper = m

hdu 2141 Can you find it?(二分查找变例)

Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. Input There are many cases. Every data case

hdu2993之斜率dp+二分查找

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5825    Accepted Submission(s): 1446 Problem Description Consider a simple sequence which only contains positive integers as