D - Laying Cables

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

standard input/output 
Announcement

 
  • Statements

    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 the i-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

Input
1 1000
7 10
9 1
12 100
Output
-1 4 2 1
Input
1 100
2 1
3 10
Output
-1 1 1
Input
1 10
3 100
2 1
Output
2 -1 2

题意:给你n个点在x轴上的位置x和权值pos 
  对于一个第i点 他的父亲定义为 和他最近并且 权值大于p[i]的 为点
  输出每个点父亲,没有满足的作其父亲的点输出-1;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; #define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f;
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
const int maxn=200000+10;
struct node{
int x,id,p,f;
}ne[maxn];
int l[maxn],r[maxn]; bool cmpx(node a,node b)
{
return a.x<b.x;
} bool cmpid(node a,node b)
{
return a.id<b.id;
} stack<node> stl,str;
int main()
{
int n;
while(~scanf("%d",&n))
{
FOR(i,n) scanf("%d%d",&ne[i].x,&ne[i].p),
ne[i].id=i;
while(stl.size()) stl.pop();
while(str.size()) str.pop(); sort(ne+1,ne+n+1,cmpx);
FOR(i,n){
int id=ne[i].id;
while(stl.size()&&stl.top().p<=ne[i].p) stl.pop();
l[id]=stl.empty()?-1:stl.top().id;
stl.push(ne[i]);
} for(int i=n;i>=1;i--){
int id=ne[i].id;
while(str.size()&&str.top().p<=ne[i].p) str.pop();
r[id]=str.empty()?-1:str.top().id;
str.push(ne[i]);
} sort(ne+1,ne+n+1,cmpid);
FOR(i,n){
if(l[i]==-1&&r[i]==-1) {PF("-1 ");CT;}
else if(l[i]==-1) {PF("%d ",r[i]);CT;}
else if(r[i]==-1) {PF("%d ",l[i]);CT;} if(ne[r[i]].x-ne[i].x>ne[i].x-ne[l[i]].x) PF("%d ",l[i]);
else if(ne[r[i]].x-ne[i].x<ne[i].x-ne[l[i]].x) PF("%d ",r[i]);
else {
if(ne[r[i]].p>ne[l[i]].p) PF("%d ",r[i]);
else PF("%d ",l[i]);
}
}
PF("\n");
}
return 0;
}

  错因分析:刚开始想用lower_bound的,先按x从小到大排好序后,再利用lower_bound查找到枚举

的点的右侧,比当前枚举点人口数大的第一个点,后来写了后才想起lower_bound是基于二分查找的,

但是按x排好序后的人口数并不呈现有序性,当然不能用lower_bound;

解决:竟然忘了单调栈这么典型的算法,左右各扫一遍,记录大于当前点的最左和最右的点的id,

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

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

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

  2. Gym - 101334F 单调栈

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

  3. Gym 100971D Laying Cables 单调栈

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

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

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

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

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

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

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

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

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

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

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

  9. BZOJ1767/Gym207383I CEOI2009 Harbingers 斜率优化、可持久化单调栈、二分

    传送门--BZOJCH 传送门--VJ 注:本题在BZOJ上是权限题,在Gym里面也不能直接看,所以只能在VJ上交了-- 不难考虑到这是一个\(dp\). 设\(dep_x\)表示\(x\)在树上的带 ...

随机推荐

  1. DevOps 之 Jenkins 安装、配置、美化、插件及常见错误处理

    继续上一篇的话题,既然已经搭建了 GitLab 的代码仓库,那么现在就可以开始进行下一步持续集成环境的搭建了.公司准备利用 Jenkins CI 进行持续集成,本文记录了 Jenkins 的安装.基础 ...

  2. 富文本二进制转换成string

    ].KindExplain); ) { explainString = explainString.Substring(, explainString.IndexOf() + , explainStr ...

  3. js下载blob的形式

    前端构建blob的方式就是通过服务器返回的文件来创建blob,需要知道文件在服务器的具体路径,用bob创建object url对象,添加到a标签上,然后触发,blob有两个问题,1.对浏览器有兼容性限 ...

  4. js相关的时间获取方法

    1.获取时间 var time=new Date();//返回的是GMT,格林尼治标准时间. console.log(time)://Thu Jul 27 2017 16:55:21 GMT+0800 ...

  5. 从无建立一个vue项目

    node.js安装 首先安装Node,官网地址 :https://nodejs.org/en/download/ ,进去下载关于符合自己电脑的下载. 具体的Node安装步骤参考地址: https:// ...

  6. (๑•̀ㅂ•́)و✧QQ用户信息管理系统

    这是第二篇文章,就直接切主题 这次剖析的也是自己的作业之一:QQ信息管理系统 一:(此处省略了建Proarams类,其实目的只是想强调把连接语句单独放一个类里更容易调用,命名规范如图) 二:(导入SQ ...

  7. 鼠标悬停设置layui tips提示框

    官方介绍:吸附层,灵活判断出现的位置,默认在元素的右侧弹出. layer.tips(content, follow, options) layer.tips(msg, '#id',{tips: 1}) ...

  8. 分布式消息中间件之kafka设计思想及基本介绍(一)

    Kafka初探 场景->需求->解决方案->应用->原理 我该如何去设计消息中间件--借鉴/完善 场景 跨进程通信(进程间生产消费模型) 需求 基本需求 实现消息的发送和接收. ...

  9. 内置函数 lambda sorted filter map 递归

    一 lambda 匿名函数 为了解决一些简单的需求而设计的一句话函数 # 计算 n 的 n次方 def func(n): return n**n print(func(10)) f = lambda ...

  10. LNMP安装与配置之Python3

    环境 我们是在CentOS7下安装python3,但CentOS已经默认安装了Python2,而 Yum 等工具依赖原来的Python2.所以我们需要稍作配置让Python2与Python3可以共存. ...