ACdream 1216 (ASC训练1) Beautiful People(DP)
题目地址: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)的更多相关文章
- 二路单调自增子序列模型【acdream 1216】
题目:acdream 1216 Beautiful People 题意:每一个人有两个值,能力值和潜力值,然后要求一个人的这两个值都严格大于第二个人的时候,这两个人才干呆在一块儿,给出很多人的值,求最 ...
- ACdream 1216——Beautiful People——————【二维LIS,nlogn处理】
Beautiful People Special Judge Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (J ...
- Aizu 2305 Beautiful Currency DP
Beautiful Currency Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- 算法训练 最大的算式 DP
算法训练 最大的算式 时间限制:1.0s 内存限制:256.0MB 问题描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果 ...
- Codeforces 1085G(1086E) Beautiful Matrix $dp$+树状数组
题意 定义一个\(n*n\)的矩阵是\(beautiful\)的,需要满足以下三个条件: 1.每一行是一个排列. 2.上下相邻的两个元素的值不同. 再定义两个矩阵的字典序大的矩阵大(从左往右从上到下一 ...
- 「暑期训练」「基础DP」 Common Subsequence (POJ-1458)
题意与分析 很简单:求最长公共子序列. 注意子序列与子串的差别:一个不连续一个连续.一份比较好的参考资料见:https://segmentfault.com/a/1190000002641054 状态 ...
- Problem D. What a Beautiful Lake dp
Problem D. What a Beautiful Lake Description Weiming Lake, also named "Un-named Lake", is ...
- 【学习笔记&训练记录】数位DP
数位DP,即对数位进行拆分,利用数位来转移的一种DP,一般采用记忆化搜索,或者是先预处理再进行转移 一个比较大略的思想就是可以对于给定的大数,进行按数位进行固定来转移记录答案 区间类型的,可以考虑前缀 ...
- SGU 199 Beautiful People(DP+二分)
时间限制:0.25s 空间限制:4M 题意: 有n个人,每个人有两个能力值,只有一个人的两个能力都小于另一个的能力值,这两个人才能共存,求能同时共存的最大人数. Solution: 显然这是一个两个关 ...
随机推荐
- LIS【p1704】寻找最优美做题曲线
Description 洛谷OJ刷题有个有趣的评测功能,就是系统自动绘制出用户的"做题曲线".所谓做题曲线就是一条曲线,或者说是折线,是这样定义的:假设某用户在第b[i]天AC了c ...
- 队列 LinkedBlockingQueue
1 api java.util.concurrent包下的新类.LinkedBlockingQueue就是其中之一,是一个阻塞的线程安全的队列,底层采用链表实现. Li ...
- delphi 浮点数转换成十六进制字符串的方法
我们在研究封包技术时,经常会碰到将浮点数转换成十六进制形式.比如在游戏中人物的座标,经常就用浮点数来表示.怎么将浮点数转换成十六进制字符串形式呢?下面我将写出其在DELPHI中的方法. 先 ...
- Nand 的几个名词:oob,bbt,ecc
转:http://blog.csdn.net/lanmanck/article/details/4230904 例如Samsung K9F1208U0B,数据存储容量为64MB,采用块页式存储管理.8 ...
- 【转】Cvmat与IplImage的相互转换
seyvlei 原文地址 1.IplImage转Cvmat IplImage* src = cvLoadImage(); CvMat* mat=cvCreateMat(src->height,s ...
- 搭建elsticsearch集群 报错with the same id but is a different node instance解决办法
搭建elsticsearch集群 报错with the same id but is a different node instance解决办法 学习了:https://blog.csdn.net/q ...
- Java取得操作系统的临时目录
一般来说,程序员用Eclipse在Windows上编程,而会将war/ear包发布到Linux的服务器上. 涉及临时文件输出的话需要找一个临时目录,下面的语句就能帮你获得不分操作系统的临时目录 Str ...
- 将ByteArrayOutputStream类型变量中的数据存储到文件中
代码: File zipFile=new File("c:\\1.zip");ByteArrayOutputStream byteOSZip = new ByteArrayOutp ...
- Angular 学习笔记——$http
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- go语言25个关键字总结
var和const :变量和常量的声明var varName type 或者 varName : = valuepackage and import: 导入func: 用于定义函数和方法return ...