D. Laying Cables
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input
4
1 1000
7 10
9 1
12 100
output
-1 4 2 1
input
3
1 100
2 1
3 10
output
-1 1 1
input
3
1 10
3 100
2 1
output
2 -1 2

思路:找到左边和右边比他大的值,最后比较左右;

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 100000007
#define esp 0.00000000001
const int N=2e5+,M=1e6+,inf=1e9+;
struct is
{
int x,val;
int pos;
}a[N];
int cmp(is x,is y)
{
return x.x<y.x;
}
int d[N],k;
int l[N],r[N];
int ans[N];
int check(int x,int len)
{
if(x<||x>len)
return ;
return ;
}
int main()
{
int x,y,z,i,t;
scanf("%d",&x);
for(i=;i<=x;i++)
scanf("%d%d",&a[i].x,&a[i].val),a[i].pos=i;
sort(a+,a++x,cmp);
a[].val=a[x+].val=inf;
k=;
d[++k]=;
for(i=;i<=x;i++)
{
while(a[d[k]].val<a[i].val)k--;
l[i]=d[k];
d[++k]=i;
}
k=;
d[++k]=x+;
for(i=x;i>=;i--)
{
while(a[d[k]].val<a[i].val)k--;
r[i]=d[k];
d[++k]=i;
}
for(i=;i<=x;i++)
{
int u=check(l[i],x),v=check(r[i],x);
if(u&&v)
{
if(a[i].x-a[l[i]].x>a[r[i]].x-a[i].x)
ans[a[i].pos]=a[r[i]].pos;
else if(a[i].x-a[l[i]].x<a[r[i]].x-a[i].x)
ans[a[i].pos]=a[l[i]].pos;
else
{
if(a[l[i]].val>a[r[i]].val)
ans[a[i].pos]=a[l[i]].pos;
else
ans[a[i].pos]=a[r[i]].pos;
}
}
else if(u)
ans[a[i].pos]=a[l[i]].pos;
else if(v)
ans[a[i].pos]=a[r[i]].pos;
else
ans[a[i].pos]=-;
}
for(i=;i<=x;i++)
printf("%d ",ans[i]);
return ;
}

Codeforces gym 100971 D. Laying Cables 单调栈的更多相关文章

  1. Gym 100971D Laying Cables 单调栈

    Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...

  2. Codeforces 1107G Vasya and Maximum Profit [单调栈]

    洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...

  3. Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...

  4. codeforces gym 100971 K Palindromization 思路

    题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...

  5. codeforces 817 D. Imbalanced Array(单调栈+思维)

    题目链接:http://codeforces.com/contest/817/problem/D 题意:给你n个数a[1..n]定义连续子段imbalance值为最大值和最小值的差,要你求这个数组的i ...

  6. Educational Codeforces Round 23 D. Imbalanced Array 单调栈

    D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. codeforces Gym 100971 A、B、C、F、G、K、L

    A题  直接把问号全部变为陆地如果所有陆地连通    那么再逐个把刚才变成陆地的问号变为水如果依旧连通有多种解 为什么我的代码跑不过去,和网上的题解思路一模一样!!?? #include<cst ...

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

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

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

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

随机推荐

  1. 【使用时发生的意外】HDFS 分布式写入问题 AlreadyBeingCreatedException

    进行追加文件时出现AlreadyBeingCreatedException错误 堆栈信息大致如下: org.apache.hadoop.ipc.RemoteException(org.apache.h ...

  2. VMware 虚拟机 Ubuntu 不能全屏问题

    在刚安装完ubuntu后,屏幕不能全屏显示,此时: 1.安装VMware Tools 步骤: 1.1     进入ubuntu系统后,点击虚拟机上的[虚拟机]->[安装 vmware tools ...

  3. 基于Hadoop的数据仓库Hive

    Hive是基于Hadoop的数据仓库工具,可对存储在HDFS上的文件中的数据集进行数据整理.特殊查询和分析处理,提供了类似于SQL语言的查询语言–HiveQL,可通过HQL语句实现简单的MR统计,Hi ...

  4. composer是php包管理工具

    composer是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件. MAC.L ...

  5. JVM内存分配原理

    堆栈常量池等内存分配原理详解 存储的方式: 寄存器 栈(stack) 堆(heap) 静态域 常量池 非RAM存储 JAVA寄存器 最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制.  ...

  6. iOS 个人所得税 app 基础解析实践

    前言:2019年 新个税实施在即,全国几乎所有在职员工都会下载“个人所得税”app来使用,并且 注册使用过程需要填写身份证号等相当私密重要的个人信息. 至今,各大app平台应用下载榜首仍然“无人能出其 ...

  7. Win10应用《纸书科学计算器》更新啦!

    <纸书科学计算器>在2016年8月拿了计算机设计大赛国家一等奖,现在仍记得我在答辩时还给评委们普及了一波UWP平台的知识.受此鼓励,这款应用也不会停下更新的脚步^_^.最近从1.9小幅升级 ...

  8. 【WPF】修改ComboBox样式

    修改WPF默认的ComboBox控件样式 如下图所示: 修改代码如下: <UserControl.Resources> <Style TargetType="ToggleB ...

  9. java switch笔记

    1. 在Java7之前,switch只能支持byte.short.char.int或者其对应的封装类以及Enum类型 2. Java7后,开始支持String 3. switch语句中,表达式的值不能 ...

  10. QT控件学习

    一.QPushButton 1.设置背景色: ui->pushButton->setStyleSheet("background-color: rgb(170, 0, 255)& ...