FZU 2216 The Longest Straight 二分

0可以表示任何1到m的数,求一个最长的连续上升序列长度

因为m的范围在10w,所以以每个节点为起点 进行二分,复杂度mlogm

思路:b[i]表示到 1 到 i 有几个数没有出现,二分的时候注意加等号

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn=100000+5;
bool vis[maxn];
int b[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,sum=0;
        scanf("%d%d",&n,&m);
        memset(vis,0,sizeof(vis));
        int x;
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&x);
            if(!x)sum++;
            else vis[x]=1;
        }
        b[0]=0;
        for(int i=1; i<=m; ++i)
        {
            if(vis[i])b[i]=b[i-1];
            else b[i]=b[i-1]+1;
        }
        int ans=1;
        for(int i=1; i<m; ++i)
        {
            int l=i,r=m,mid;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(b[mid]-b[i-1]>sum)r=mid-1;
                else l=mid+1;
            }
            mid=(l+r)>>1;
            ans=max(ans,mid-i+1);
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-10-10 06:00:49

FZU 2216 The Longest Straight 二分的相关文章

FZU 2216 The Longest Straight(最长直道)

Description 题目描述 ZB is playing a card game where the goal is to make straights. Each card in the deck has a number between 1 and M(including 1 and M). A straight is a sequence of cards with consecutive values. Values do not wrap around, so 1 does not

The Longest Straight(二分,离散化)

 Problem 2216 The Longest Straight Accept: 7    Submit: 14 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description ZB is playing a card game where the goal is to make straights. Each card in the deck has a number between 1 and M(includi

The Longest Straight(FZUoj2216)

 Problem 2216 The Longest Straight Accept: 82    Submit: 203Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description ZB is playing a card game where the goal is to make straights. Each card in the deck has a number between 1 and M(includ

FZU-2216 The Longest Straight (二分枚举)

题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二分序列的终点. 代码如下; # include<iostream> # include<cstdio> # include<queue> # include<vector> # include<list> # include<map> #

Problem FZU 2232 炉石传说(二分匹配)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2232 分析:因为是要求所有的学长一次性打败所有的敌人,所以可以看做是匹配问题.构建一个图,G[i][j]=1表示学长在攻击对手后,自己的生命值大于0,对方生命值小于等于0.然后看匹配数是否为n即可. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include

[LeetCode] 687. Longest Univalue Path

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

第六届福建省大学生程序设计竞赛不完全题解

昨天自己队做了一下第六届福建省赛,感觉有些蒙,赛后补了几道题,能力有限,一共只出A了7道题 A题   Super Mobile Charger 题目链接 http://acm.fzu.edu.cn/problem.php?pid=2212 水题 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a[10005]; int b[10005]; int main(

2015福建省赛

 Problem A Super Mobile Charger Accept: 217    Submit: 402 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description While HIT ACM Group finished their contest in Shanghai and is heading back Harbin, their train was delayed due to the hea

2014 8.7 第8场个人 排位

FZU 2079  最大获利(dp) 自己很难想到,囧 dp[i]表示处理完前i 个点的最大收益.有两种情况:(1)第i 个点不投资,那么dp[i] = dp[i - 1](2)第i 个点投资,那么dp[i] = max(dp[k] + get(k + 1,i) - sigma(c[j]) (k + 1 <= j <= i))(0 < k <i)这个的意思是,[k + 1,i]点选择全部投资,所以sigma(c[j])表示每个点都投资了,get(k +1,i)的意思是,[k + 1