Description

One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. When one-dimensional country got the Internet, it was decided to place the main server in the largest city, and to connect any other city j to the city k that has bigger population than j and is the closest to it (if there are many such cities, the largest one should be chosen). City k is called a parent of city j in this case.

Unfortunately, the Ministry of Communications got stuck in determining from where and to where the Internet cables should be laid, and the population of the country is suffering. So you should solve the problem. For every city, find its parent city.

Input

The first line contains a single integer n(1 ≤ n ≤ 200000) — the number of cities.

Each of the next n lines contains two space-separated integers xi and pi(1 ≤ xi,  pi ≤ 109) — the coordinate and the population of thei-th city.

Output

Output n space-separated integers. The i-th number should be the parent of the i-th city, or  - 1, if the i-th city doesn't have a parent. The cities are numbered from 1 by their order in the input.

Sample Input

4
1 1000
7 10
9 1
12 100
-1 4 2 1
 
题意:
   给你n个点在x轴上的位置x和权值pos 
  对于一个第i点 他的父亲定义为 和他最近并且 权值大于p[i]的 为点
  输出每个点父亲,没有满足的作其父亲的点输出-1;
题解:
  单调栈预处理出第i个点左边的父亲,和右边的父亲,去最近,权值最大的一个
  作一遍RMQ,二分左边的父亲是谁,最优解是最靠近的点
 
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+, M = 1e6+, mod = 1e9+, inf = 2e9;
typedef long long ll; int n,ans[N];
struct ss{long long x,p;int id;}a[N],lefts[N],rights[N];
vector<ss> G;
bool cmp(ss s1,ss s2) {return s1.x<s2.x;} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].p),a[i].id = i;
sort(a+,a+n+,cmp);
a[].x=-inf, a[].p = inf; a[].id = -;
G.push_back(a[]);
for(int i=;i<=n;i++) {
while(G.back().p<=a[i].p) G.pop_back();
lefts[i]=G.back();
G.push_back(a[i]);
}
G.clear();
a[n+] = (ss) {inf,inf,-};
G.push_back(a[n+]);
for(int i=n;i>=;i--) {
while(G.back().p<=a[i].p) G.pop_back();
rights[i]=G.back();
G.push_back(a[i]);
} for(int i=;i<=n;i++) {
if(abs(lefts[i].x-a[i].x)==abs(rights[i].x-a[i].x)) {
if(lefts[i].p>rights[i].p) ans[a[i].id] = lefts[i].id;
else ans[a[i].id] = rights[i].id;
}
else if(abs(lefts[i].x-a[i].x)<abs(rights[i].x-a[i].x)) {
ans[a[i].id] = lefts[i].id;
}
else ans[a[i].id] = rights[i].id;
}
for(int i=;i<=n;i++) printf("%d ",ans[i]);
printf("\n");
}

单调栈

 
 
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+, M = 1e6+, mod = 1e9+, inf = 2e9;
typedef long long ll; int n;
ll dp[N][];
int ans[N]; struct ss{long long x,p;int id;}a[N],lefts[N],rights[N]; bool cmp(ss s1,ss s2) {return s1.x<s2.x;}
ll cal(int l,int r)
{
if(l==r) return a[l].p;
int k = (int) (log((double) r-l+) / log(2.0));
return max(dp[l][k], dp[r - (<<k) + ][k]);
}
int main() {
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].p), a[i].id=i;
sort(a+,a+n+,cmp);
a[] = (ss) {-inf,inf,-};
a[n+] = (ss){inf,inf,-};
for(int i=;i<=n+;i++) dp[i][] = a[i].p;
for(int j=;(<<j)<=n+;j++) {
for(int i=;i + (<<j) - <= n+; i++) {
if(dp[i][j-] > dp[i+(<<(j-))][j-])
dp[i][j] = dp[i][j-];
else {
dp[i][j] = dp[i+(<<(j-))][j-];
}
}
}
//cout<<cal(0,1)<<endl;
for(int i=;i<=n;i++) {
int l = , r = i-,A=;
while(l<=r) {
int mid = (l+r) >> ;
if(cal(mid,i-)> a[i].p) l = mid+,A=mid;
else r = mid-;
}
lefts[i] = a[A];
// cout<<A<<" ";
l = i+, r = n+;A = n+;
while(l<=r) {
int mid = (l+r) >> ;
if(cal(i+,mid)> a[i].p) r=mid-,A = mid;
else l = mid+;
}
rights[i] = a[A];
// cout<<A<<endl;
} for(int i=;i<=n;i++) {
if(abs(lefts[i].x-a[i].x)==abs(rights[i].x-a[i].x)) {
if(lefts[i].p>rights[i].p) ans[a[i].id] = lefts[i].id;
else ans[a[i].id] = rights[i].id;
}
else if(abs(lefts[i].x-a[i].x)<abs(rights[i].x-a[i].x)) {
ans[a[i].id] = lefts[i].id;
}
else ans[a[i].id] = rights[i].id;
}
for(int i=;i<=n;i++) printf("%d ",ans[i]);
printf("\n"); }

RMQ+二分

Gym 100971D Laying Cables 单调栈的更多相关文章

  1. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  2. Gym 100971D Laying Cables 二分 || 单调栈

    要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...

  3. Codeforces gym 100971 D. Laying Cables 单调栈

    D. Laying Cables time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. Gym - 101102D Rectangles (单调栈)

    Given an R×C grid with each cell containing an integer, find the number of subrectangles in this gri ...

  5. D - Laying Cables Gym - 100971D (单调栈)

    题目链接:https://cn.vjudge.net/problem/Gym-100971D 题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...

  6. Gym 100971D 单调栈

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  7. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  8. Gym - 101334F 单调栈

    当时我的第一想法也是用单调栈,但是被我写炸了:我也不知道错在哪里: 看了大神的写法,用数组模拟的: 记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置. 当有数据要出栈的时候,说明栈里的数据 ...

  9. Gym - 102028H Can You Solve the Harder Problem? (后缀数组+RMQ+单调栈)

    题意:求一个序列中本质不同的连续子序列的最大值之和. 由于要求“本质不同”,所以后缀数组就派上用场了,可以从小到大枚举每个后缀,对于每个sa[i],从sa[i]+ht[i]开始枚举(ht[0]=0), ...

随机推荐

  1. Windows部署WordPress

    感谢原文作者,还是给个快速通道: http://www.cnblogs.com/huangcong/archive/2010/03/31/1701052.html 另:安装之后,运行速度会很慢,以下是 ...

  2. iOS学习笔记—ViewController/生命周期

    ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图.iOS的SDK中提供很多原生ViewController ...

  3. 如何修改ubuntu系统的电脑名(主机名)

    在按照ubuntu系统时,会提示你给电脑填写一个名字,可能当时你没有想好,就随便填写了一个,可是以后就又有新的想法,想重新更换一个名字,该怎么办呢? 其实很简单.按照下面的步骤即可. 进去后,修改完, ...

  4. [OpenJudge 3063]罪犯问题

    [OpenJudge 3063]罪犯问题 试题描述 一天,警官抓获了N个嫌犯,审问N个罪犯的途中,作为警长助手的你突然发现其中被确定为罪犯的K号人是你曾经出生入死的兄弟,你不能眼睁睁看着他被抓进牢里. ...

  5. ZeroMQ(java)之Router/Dealer模式

    本教程转自:http://blog.csdn.net/kobejayandy/article/details/20163527 在开始之前先把guid里面提到的几个ZeroMQ的特性列一下吧: (1) ...

  6. TCP同步与异步及阻塞模式,多线程+阻塞模式,非阻塞模式简单介绍

    首先我简单介绍一下同步TCP编程 与异步TCP编程. 在服务端我们通常用一个TcpListener来监听一个IP和端口.客户端来一个请求的连接,在服务端可以用同步的方式来接收,也可以用异步的方式去接收 ...

  7. Linux MySQL差异备份技巧

    MSSQL差异备份使用技巧 15 Apr 2013 所谓的差异备份,就是只备份最近一次备份之后到此次备份之前所增加的那一部分数据.打个比方我第N次备份后数据库存放的内容是ABCD,然后我第N+1次 备 ...

  8. 【UGUI】Canvas和Rect Transform

    Canvas 1.所有的UI元件都需要放在Canvas里 2.UI元件的绘制顺序,与在 Hierarchy的顺序相同,在上面的元素会先被绘制,位于后续绘制元素的下面 3.可以选择3种不同的渲染模式: ...

  9. Android Fragment间对象传递

    由于Activity相对较为笨重,因而在日常的开发中很多时候会用到Fragment.然而Activity之间是通过Intent进行数据的传递,那Fragment是通过什么来进行传递的呢?Fragmen ...

  10. [MAC] mac系统如何截图

    mac自带截图工具,因此不需要安装任何第三方软件,便可以实现屏幕截图,截图的方法有若干种,下面介绍最简单的方法:通过快捷键进行截图: 全屏截图: 同时按住键盘左下方的  command   和   s ...