题目地址:http://acdream.info/problem?

pid=1216

这题一開始用的是线段树。后来发现查询的时候还须要DP处理。挺麻烦。。也就不了了之了。。后来想到,这题事实上就是一个二维的最长上升子序列。

要先排序,先按左边的数为第一keyword进行升序排序。再按右边的数为第二keyword进行降序排序。这种话,第一keyword同样的的肯定不在一个同一个上升子序列中。然后仅仅对第二keyword进行复杂度为O(n*logn)的DP,找出最长上升序列,然后处理前驱,并输出就可以。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL long long
struct node
{
int x, y, num;
}fei[110000];
int cmp(node x, node y)
{
if(x.x==y.x)
return x.y>y.y;
return x.x<y.x;
}
int a[110000], d[110000], pre[110000], len, b[110000];
int bin_seach(int x)
{
int low=0, high=len, mid, ans;
while(low<=high)
{
mid=low+high>>1;
if(a[mid]>=x)
{
high=mid-1;
ans=mid;
}
else
{
low=mid+1;
}
}
return ans;
}
int main()
{
int n, i, j, pos, cnt;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d%d",&fei[i].x,&fei[i].y);
fei[i].num=i+1;
}
sort(fei,fei+n,cmp);
len=1;
a[1]=fei[0].y;
d[0]=-1;
d[1]=0;
memset(pre,-1,sizeof(pre));
for(i=1;i<n;i++)
{
if(fei[i].y>a[len])
{
a[++len]=fei[i].y;
pre[i]=d[len-1];
d[len]=i;
}
else
{
pos=bin_seach(fei[i].y);
a[pos]=fei[i].y;
pre[i]=d[pos-1];
d[pos]=i;
}
}
printf("%d\n",len);
cnt=0;
/*for(i=0;i<n;i++)
{
printf("%d ",fei[i].num);
}
puts("");*/
for(i=d[len];i!=-1;i=pre[i])
{
b[cnt++]=fei[i].num;
//printf("%d\n",i);
}
for(i=0;i<cnt-1;i++)
printf("%d ",b[i]);
printf("%d\n",b[cnt-1]);
}
return 0;
}

ACdream 1216 (ASC训练1) Beautiful People(DP)的更多相关文章

  1. 二路单调自增子序列模型【acdream 1216】

    题目:acdream 1216 Beautiful People 题意:每一个人有两个值,能力值和潜力值,然后要求一个人的这两个值都严格大于第二个人的时候,这两个人才干呆在一块儿,给出很多人的值,求最 ...

  2. ACdream 1216——Beautiful People——————【二维LIS,nlogn处理】

    Beautiful People Special Judge Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (J ...

  3. Aizu 2305 Beautiful Currency DP

    Beautiful Currency Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  4. 算法训练 最大的算式 DP

    算法训练 最大的算式 时间限制:1.0s   内存限制:256.0MB     问题描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果 ...

  5. Codeforces 1085G(1086E) Beautiful Matrix $dp$+树状数组

    题意 定义一个\(n*n\)的矩阵是\(beautiful\)的,需要满足以下三个条件: 1.每一行是一个排列. 2.上下相邻的两个元素的值不同. 再定义两个矩阵的字典序大的矩阵大(从左往右从上到下一 ...

  6. 「暑期训练」「基础DP」 Common Subsequence (POJ-1458)

    题意与分析 很简单:求最长公共子序列. 注意子序列与子串的差别:一个不连续一个连续.一份比较好的参考资料见:https://segmentfault.com/a/1190000002641054 状态 ...

  7. Problem D. What a Beautiful Lake dp

    Problem D. What a Beautiful Lake Description Weiming Lake, also named "Un-named Lake", is ...

  8. 【学习笔记&训练记录】数位DP

    数位DP,即对数位进行拆分,利用数位来转移的一种DP,一般采用记忆化搜索,或者是先预处理再进行转移 一个比较大略的思想就是可以对于给定的大数,进行按数位进行固定来转移记录答案 区间类型的,可以考虑前缀 ...

  9. SGU 199 Beautiful People(DP+二分)

    时间限制:0.25s 空间限制:4M 题意: 有n个人,每个人有两个能力值,只有一个人的两个能力都小于另一个的能力值,这两个人才能共存,求能同时共存的最大人数. Solution: 显然这是一个两个关 ...

随机推荐

  1. Jmeter进行webSocket接口测试

    一.运行Jmeter (1) 去官方网站下载jmeter(版本为3.3)并解压.点击bin/jmeter.bat启动jmeter (2)  新建线程组. (3) 在线程组中新建WebSocket Sa ...

  2. [BZOJ2142]礼物(扩展Lucas)

    2142: 礼物 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2286  Solved: 1009[Submit][Status][Discuss] ...

  3. Java this关键字详解

    this 关键字用来表示当前对象本身,或当前类的一个实例,通过 this 可以调用本对象的所有方法和属性.例如: public class Demo{ public int x = 10; publi ...

  4. 动态路由协议(3)--ospf

    1.设置pc ip 网关 192.168.1.1 192.168.1.254 192.168.4.1 192.168.4.254 2.设置路由器 (1)设置接口ip Router(config-/ R ...

  5. WPF--TextBox的验证

    WPF--TextBox的验证  

  6. sql server 老外博客

    Aaron Bertrand Grant Fritchey Brent Ozar Thomas LaRock Pinal Dave Phil Factor SQL Skills w/ Paul Ran ...

  7. hash算法散列算法

    Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是 ...

  8. Using xcodebuild To Export a .ipa From an Archive

    Xcode 6 changes how you export a .ipa from an archive for adhoc distribution. It used to be that you ...

  9. 利用Python,四步掌握机器学习

    为了理解和应用机器学习技术,你需要学习 Python 或者 R.这两者都是与 C.Java.PHP 相类似的编程语言.但是,因为 Python 与 R 都比较年轻,而且更加“远离”CPU,所以它们显得 ...

  10. 智能选择器和语义化的CSS

    本文由白牙根据Heydon Pickering的<Semantic CSS With Intelligent Selectors>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之 ...