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. web.config 加密/解密

    (Aspnet_regiis.exe) 这样的一个工具,可以对站点的.config文件的节进行加密 方法: #> 加密:aspnet_regiis -pef "加密的web.confi ...

  2. SGU 170 Particles(规律题)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=170 解题报告:输入两个由'+'和'-'组成的字符串,让你判断第二个串能不能由第一个 ...

  3. tomcat安全配置(二)

    1. JVM 1.1. 使用 Server JRE 替代JDK. 服务器上不要安装JDK,请使用 Server JRE. 服务器上根本不需要编译器,代码应该在Release服务器上完成编译打包工作. ...

  4. 深度学习入门教程UFLDL学习实验笔记一:稀疏自编码器

    UFLDL即(unsupervised feature learning & deep learning).这是斯坦福网站上的一篇经典教程.顾名思义,你将在这篇这篇文章中学习到无监督特征学习和 ...

  5. Ubuntu上如何安装Java,Eclipse,Pydev,Python(自带,不用装),BeautifulSoup

    如何安装Java,如果出于编程的需要安装Java,需要安装的是JDK,而不仅仅是JRE,下面说说如何在Ubuntu下如何安装JDK:只有两步,1.下载并解压,2.配置环境变量1.下载并解压:下载地址: ...

  6. 【云计算】docker build如何支持参数化构建?

    docker 1.9.0版本之后,已经支持docker build参数化构建. docker 版本更新记录: github讨论: 参开资料: https://github.com/docker/doc ...

  7. ASP.NET小知识

    所有System.Web.UI.*命名空间下的内容可以称为Web From,而System.Web.*命名空间下的其他内容可以称为ASP.NET. @section用法:配合母版页中的@RenderS ...

  8. iOS7 status bar 样式问题

    在ios7中,有如下status bar 样式 typedef NS_ENUM(NSInteger, UIStatusBarStyle) { UIStatusBarStyleDefault = , / ...

  9. Java for LeetCode 066 Plus One

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  10. php5.4.3连接SQLite3

    我使用的是WAMP2.2菜单-PHP-PHP extensions勾选php_sqlite3<?php$conn = new SQLite3("c:/wamp/www/test.db& ...