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. 【Android】自己定义相机的实现(支持连续拍照、前后摄像头切换、连续对焦)

    ~转载请注明http://blog.csdn.net/u013015161/article/details/46921257 介绍 这几天.写了一个自己定义照相机的demo.支持连续拍照和摄像头切换. ...

  2. django 模板语言之 simple_tag 自定义模板

    自定义函数 simple_tag a. app项目下创建templatetags目录 b. 创建任意xxoo.py文件 用做自定义py函数 c. 创建template对象 register 在函数或者 ...

  3. 18.让sublime text3支持Vue语法高亮显示

    1.准备语法高亮插件vue-syntax-highlight. 下载地址: https://github.com/vuejs/vue-syntax-highlight 下载页面并下载: 解开压缩包vu ...

  4. DecisionTree

    1.信息增益的定义,也就是互信息 2.信息增益的推导 由公式即可得到信息增益 信息增益存在偏向于选择取值较多的特征的问题,信息增益比可以对这一问题进行修正 3.信息增益比 4.基尼指数,基尼指数越大, ...

  5. 算法总结之动态规划(DP)

    适用动态规划的特点 所解决的问题是最优化问题. 所解决的问题具有"最优子结构".可以建立一个递推关系,使得n阶段的问题,可以通过几个k<n阶段的低阶子问题的最优解来求解. 具 ...

  6. 20165324 Java实验三 敏捷开发与XP实验

    20165324 Java实验三 敏捷开发与XP实验 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年4月16日 ...

  7. Outputting Strings in the Console

    Outputting Strings in the Console #include <windows.h> class Console {public:  enum fore_color ...

  8. oracle连接池问题

    1.数据来源. 控制台地址:172.30.252.164:7001/console 登陆: weblogic/weblogic#123数据库jdbc:oracle:thin:@172.30.252.1 ...

  9. Python-argparse-命令行与参数解析

    import argparse import numpy as np import cv2 import os import numpy.random as npr from dface.core.u ...

  10. windows 常用cmd命令

    为了减少使用鼠标的频次,熟记一些常用应用的快捷键与系统本身常用的命令是必须的,以下记录一些常用的windows系统命令. 查看网络端口占用情况 :netstat -ano | findstr 8080 ...