SGU 521 North-East ( 二维LIS 线段树优化 )
521. "North-East"
Memory limit: 262144 kilobytes
output: standard
The popular music band of international fame "North-East" is coming to Berland! This news has spread all over the country, so numerous fans are now ready to rush and buy all the tickets!
At present the fans still don't know in which cities the band plans to give concerts. The only thing is known at the moment is that the band will visit several cities, and as their name says, they will strictly move north and east when going to the next city. In other words when the band moves from city i to city j, city j is always located northward and eastward of the city i.
It's also known that the tour is planned in such a way that the maximum possible number of cities will be visited. The musicians refuse to reveal other details. As you know, fans always get ready for the arrival of their idols, so they would appreciate any single detail about possible movements of their favorite musicians.
Your task is to help the fans and find two lists of cities — A and B. The first list A should contain the cities, which the band might visit during the tour. The second list B should contain the cities, which the band will have to visit for sure during the tour.
The first line of input contains a single integer n (1 ≤ n ≤ 105) — amount of cities in the country. The following n lines contain coordinates of the cities. Each line contains a pair of integersxi, yi (-106 ≤ xi, yi ≤ 106) — the coordinates of the i-th city. Ox axis is directed west-to-east, and Oy axis — south-to-north. No two given cities will be located at the same point.
Print the required list A to the first line of output and B to the second line. Each list should start with the amount of cities in it, followed by the indices of cities in increasing order. Cities are numbered from 1 to n.
sample input |
sample output |
5 |
5 1 2 3 4 5 |
sample input |
sample output |
5 |
4 1 2 3 5 |
题意是可以从某个点出发,走向 x , y 都严格增大的点走 , 问那些点可能在最长路径上 ,那些点必定会经过
用 dp[0][i] 表示点i 作为右端点的最长路 , dp[1][i] 表示点i 作为左端点的最长路 。
再判一下 dp[0][i] + dp[1][i] 是否跟最大值相等 , 就可知道它是否第一类点。
而第二类点直接用一个cnt数组记录一下数字的出现次数是否唯一即可判断 ~
#include <bits/stdc++.h>
using namespace std ;
const int N = ; int n ; struct SegTree {
#define root 1,n+10,1
#define lr rt<<1
#define rr rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int mx[N<<] ; void Up( int rt ) {
mx[rt] = max( mx[lr] , mx[rr] ) ;
} void Build( int l , int r , int rt ) {
mx[rt] = ;
if( l == r ) return ;
int mid = ( l+r ) >> ;
Build(lson),Build(rson) ;
} void Update( int l , int r , int rt , int x , int v ) {
if( l == r ) { mx[rt] = v ; return ; }
int mid = (l+r)>>;
if( x <= mid ) Update( lson , x , v );
else Update( rson , x , v);
Up(rt);
} int Query( int l , int r , int rt , int L , int R ) {
if( l == L && r == R ) return mx[rt] ;
int mid = (l+r) >> ;
if( R <= mid ) return Query( lson , L , R ) ;
else if( L > mid ) return Query( rson , L , R ) ;
else return max( Query( lson , L , mid ) , Query( rson , mid + , R ) ) ;
}
} T; struct node { int x , y , id ; } e[N] ; inline bool cmp1( const node &a , const node &b ) {
if( a.x != b.x ) return a.x < b.x ;
else return a.y < b.y ;
} inline bool cmp2( const node &a , const node &b ) {
if( a.x != b.x ) return a.x > b.x ;
else return a.y > b.y ;
} map<int,int>idx;
vector<int>a;
int S[N] , top ; vector<int> ans1 , ans2 ;
int cnt[N] , dp[][N] ; void Doit( int k , int p ) {
for( int i = top - ; i >= ; --i ) {
int id = idx[ e[p].y ] ;
dp[k][ e[p].id ] = S[i] ;
T.Update( root , id , S[i] );
p-- ;
}
} void Gao() { memset( dp , , sizeof dp ) ;
memset( cnt , , sizeof cnt ) ; sort( e + , e + n + , cmp1 ) ;
T.Build( root ) ; top = ;
for( int i = ; i <= n ; ++i ) {
int id = idx[ e[i].y ] ;
if( i == n || e[i].x != e[i+].x ) {
S[top++] = T.Query( root , , id - ) + ;
Doit( , i );
top = ;
} else {
S[top++] = T.Query( root , , id - ) + ;
}
}
sort( e + , e + n + , cmp2 ) ;
T.Build( root ) ; top = ;
for( int i = ; i <= n ; ++i ) {
int id = idx[ e[i].y ] ;
if( i == n || e[i].x != e[i+].x ) {
S[top++] = T.Query( root , id + , n + ) + ;
Doit( , i );
top = ;
} else {
S[top++] = T.Query( root , id + , n + ) + ;
}
} // for( int i = 1 ; i <= n ; ++i ) cout << e[i].id << ' ' << dp[0][ e[i].id ] << ' ' << dp[1][ e[i].id ] << endl ; ans1.clear() , ans2.clear() ;
int len = ;
for( int i = ; i <= n ; ++i ) len = max( len , dp[][i] + dp[][i] ) ;
for( int i = ; i <= n ; ++i ) if( dp[][ e[i].id ] + dp[][ e[i].id ] == len ) ans1.push_back( e[i].id ) , cnt[ dp[][ e[i].id ] ]++ ;
for( int i = ; i <= n ; ++i ) if( dp[][ e[i].id ] + dp[][ e[i].id ] == len && cnt[ dp[][e[i].id] ] == ) ans2.push_back( e[i].id ) ;
} void Output() {
sort( ans1.begin() , ans1.end() ) , sort( ans2.begin() , ans2.end() ) ;
printf("%d",ans1.size()); for( int i = ; i < ans1.size() ; ++i ) printf(" %d",ans1[i]);puts("");
printf("%d",ans2.size()); for( int i = ; i < ans2.size() ; ++i ) printf(" %d",ans2[i]);puts("");
} int main() {
while( ~scanf("%d",&n) ) {
idx.clear(); a.clear();
for( int i = ; i <= n ; ++i ) {
scanf("%d%d",&e[i].x,&e[i].y);
e[i].id = i ;
a.push_back( e[i].y );
}
sort( a.begin() , a.end() ) ;
int tot = ;
for( int i = ; i < a.size() ; ++i ) {
if( idx.find( a[i] ) == idx.end() ) {
idx[ a[i] ] = tot++ ;
}
}
Gao(); Output();
}
return ;
}
SGU 521 North-East ( 二维LIS 线段树优化 )的更多相关文章
- 二维$MLE$线段树
关于二维线段树,ta死了 先来看看两种二维线段树的打法 1.四叉树 然而ta死了,ta是$\Theta (n)$的,加上线段树的常数,$T$飞稳 2.线段树套线段树 我尽量画出来... 图中每个方块是 ...
- D. Babaei and Birthday Cake---cf629D(LIS线段树优化)
题目链接:http://codeforces.com/problemset/problem/629/D 题意就是现有n个蛋糕,蛋糕的形状是圆柱体,每个蛋糕的体积就是圆柱体的体积,每个蛋糕的编号是1-- ...
- 二维LIS(CDQ分治)
题目描述 给定一个长度为N的序列S,S的每个元素pi是一个二元组(xi,yi),定义pi<pj当且仅当xi<xj并且yi<yj,求S的最长上升子序列长度 输入格式 第一行一个N,表示 ...
- HDU 1160 FatMouse's Speed(要记录路径的二维LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ2244: [SDOI2011]拦截导弹(CDQ分治,二维LIS,计数)
Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度.并且能够拦截任意速度的导弹,但是以后每一发炮弹都不能高 ...
- (转载)Android项目实战(二十八):Zxing二维码实现及优化
Android项目实战(二十八):Zxing二维码实现及优化 前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中 ...
- 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp
题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a varian ...
- hdu 3698 UVA1490 Let the light guide us 线段树优化DP
题目链接 and 题目大意 hdu3698 但是 hdu的数据比较弱,所以在这luogu提交吧UVA1490 Let the light guide us 有一个\(n*m\)的平原,要求每行选一个点 ...
- UVA-1322 Minimizing Maximizer (DP+线段树优化)
题目大意:给一个长度为n的区间,m条线段序列,找出这个序列的一个最短子序列,使得区间完全被覆盖. 题目分析:这道题不难想,定义状态dp(i)表示用前 i 条线段覆盖区间1~第 i 线段的右端点需要的最 ...
随机推荐
- python1--比较三个数值的大小
num1 = input("num1:")num2 = input("num2:")num3 = input("num3:")num = m ...
- vue学习-day03(动画,组件)
目录: 1.品牌列表-从数据库获取列表 2.品牌列表-完成添加功能 3.品牌列表-完成删除功能 4.品牌列表-全局配置数据接口的根域名 5.品牌列表-全局配置emulateJS ...
- Bootstap
Bootstrap框架 Bootstrap框架 Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序 ...
- StringTokenizer工具类的使用
package stringtokenizer.java; import java.util.StringTokenizer; public class stringtokenizer { publi ...
- cin.clear()、cin.sync()
看机器学习时,发现之前学的C++代码忘了,cin.clear().cin.sync() cin.clear():将流中的所有状态值都重设为有效值 cin.sync():清空流 这个很有意思,如果没有c ...
- (一)SQL -- 基础知识
SQL是一个标准的数据库语言,是面向集合的描述性非过程化语言. 优点:功能强.效率高.简单易学易维护. 缺点:非过程化语言,大多数语言都是独立执行,与上下文无关,而大多数 应用都是一个完整的过程,显然 ...
- 关于spotlight_on_oracle的配置及操作
Spotlight是一个强有力的Oracle数据库实时性能诊断工具,提供了一个直观的.可视化的数据库活动展现.Spotlight可视化展现性能瓶颈,一旦某个指标超出可接受的阀值的话.而且,通过下钻功能 ...
- rtmp协议分析
最近需要做一个rtmp服务器,着手分析一下rtmp协议,开干. rtmp握手 这个推荐一篇文章讲解得比较透彻http://blog.sina.com.cn/s/blog_676e11660102v8b ...
- 【flask-Email】邮件发送
使用依赖: flask_mail 安装方式: pip3 install flask-mail 代码示例: from flask import Flask from flask_mail import ...
- win7旗舰版C盘无写入权限别拒绝怎么办? 精选
win7旗舰版C盘无写入权限别拒绝怎么办? 精选 https://zhidao.baidu.com/question/366277826663554972.html 浏览 42 次 1个回答 [热点话 ...