BZOJ 1852 最长不下降序列

数据过水。此非正解。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100500
using namespace std;
int n,ret=0;
struct pnt
{
    int a,b;
}p[maxn];
bool cmp1(pnt x,pnt y)
{
    if ((x.b>=y.a) && (x.a>y.b)) return false;
    else if ((x.b>=y.a) && (x.a<=y.b)) return x.b<y.b;
    else if ((x.b<y.a) && (x.a>y.b)) return x.a<y.a;
    else return true;
}
bool cmp2(pnt x,pnt y)
{
    if (x.a==y.a) return x.b>y.b;
    return x.a>y.a;
}
inline int read()
{
    int data=0;char ch;
    while (ch<‘0‘ || ch>‘9‘) ch=getchar();
    while (ch>=‘0‘ && ch<=‘9‘)
    {
        data=data*10+ch-‘0‘;
        ch=getchar();
    }
    return data;
}
int main()
{
    n=read();
    for (register int i=1;i<=n;i++) p[i].a=read(),p[i].b=read();
    int ans=0,mx=0;
    sort(p+1,p+n+1,cmp1);
    for (register int i=1;i<=n;i++)
    {
        if (p[i].a>mx)
        {
            ans++;
            mx=max(mx,p[i].b);
        }
    }
    ret=max(ret,ans);
    sort(p+1,p+n+1,cmp2);
    ans=1;mx=p[1].a;
    for (register int i=2;i<=n;i++)
    {
        if (mx>p[i].b)
        {
            ans++;
            mx=p[i].a;
        }
    }
    ret=max(ret,ans);
    printf("%d\n",ret);
    return 0;
}
时间: 2024-08-29 19:00:27

BZOJ 1852 最长不下降序列的相关文章

算法复习——求最长不下降序列长度(dp算法)

题目: 题目背景 161114-练习-DAY1-AHSDFZ T2 题目描述 有 N 辆列车,标记为 1,2,3,-,N.它们按照一定的次序进站,站台共有 K 个轨道,轨道遵从先进先出的原则.列车进入站台内的轨道后可以等待任意时间后出站,且所有列车不可后退.现在要使出站的顺序变为 N,N-1,N-2,-,1,询问 K 的最小值是多少. 例如上图中进站的顺序为 1,3,2,4,8,6,9,5,7,则出站的顺序变为 9,8,7,6,5,4,3,2,1. 输入格式 输入共 2 行.第 1 行包含 1 

求最长不下降序列个数

求最长不下降序列个数(jdoj-1946) 题目大意:给你一个序列,求所有最长不下降序列的个数. 注释:n(总序列长度)<10000. 想法:维护两个数组,分别表示包含这个数的最长子序列长度和达到这个长度的方案数,最后统计答案,跑两次dp即可. 最后,附上丑陋的代码....... 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int dp[10010]; 5 int a[10010]; 6

【模板】求最长不下降序列 [动态规划 LIs]

求最长不下降序列 看不出来哪里还错了..... d[i]以i为结尾的最长上升子序列的长度     g[i]表示d值为i的最小状态的编号即长度为i的上升子序列的最小末尾值(d[j]=i的j值最小) liurujia's for(int i=1;i<=n;++i) g[i]=inf; for(int i=1;i<=n;++i){ int k=lower_buond(g+1,g+1+n,a[i])-g; d[i]=k; g[k]=a[i]; } 二昏好难啊..... 贴上90昏代码.... #inc

Laoj P1194 [hnoi97]最长不下降序列

问题背景 动态规划入门-第13题 试题描述 设有整数序列b1,b2,b3,-,bm,若存在i1<i2<i3<-<in,且bi1<bi2<bi3<-<bin,则称 b1,b2,b3,-,bm中有长度为n的不下降序列bi1,bi2,bi3,-,bin.求序列b1,b2,b3,-,bm中所有长度(n)最大不下降子序列.例如3,18,7,14,10,12,23,41,16,24,其中3,18,23,24就是一个长度为4的不下降序列,同时也有3,7,10,12,16,

1259:【例9.3】求最长不下降序列

传送门:http://ybt.ssoier.cn:8088/problem_show.php?pid=1259 [题目描述] 设有由n(1≤n≤200) 个不相同的整数组成的数列,记为:b(1).b(2).…….b(n)且b(i)≠b(j)(i≠j),若存在i1<i2<i3<…<ie 且有b(i1)<b(i2)<…<b(ie) 则称为长度为e的不下降序列.程序要求,当原数列出之后,求出最长的不下降序列. 例如13,7,9,16,38,24,37,18,44,19,

BZOJ 1852:[MexicoOI06]最长不下降序列(贪心+DP+线段树+离散化)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1852 [题目大意] 给你N对数A1,B1……An,Bn.要求你从中找出最多的对, 把它们按照一种方式排列,重新标号1,2,..,k.能满足对于每一对i<j,都有Ai>Bj. [题解] 对于排序的问题,如果i必须要在j前面, 那么有A[i]>B[j],且B[i]>=A[j],相加得A[i]+B[i]>A[j]+B[j], 因此按A+B从大到小排序后最优, 我们先将A

最长不下降序列解决算法

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include "stdafx.h" #include<iostream> #include<cstdio> #include<cstring> using namespace std

第八十课 最长不下降序列

原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9734927.html

最长不下降子序列的长度

试题描述 求最长不下降子序列的长度. 设有由n个不相同的整数组成的数列,记为:a[1].a[2].…….a[n].例如:3,18,7,14,10,12,23,41,16,24.若存在0<i1<i2<i3< … < ie 且有a[i1]<=a[i2]<= … <=a[ie]则称为长度为e的不下降序列.如上例中3,18,23,24就是一个长度为4的不下降序列,同时也有3,7,10,12,16,24长度为6的不下降序列. 输入 第一行为n,表示序列中整数的个数,第