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,

最新文章

  1. java new Date()得到的时间和系统时间不一样
  2. winform窗体最大化、最小化、还原
  3. MVC3学习:利用mvc3+ajax实现登录
  4. discuz x2 diy 模块的样式点击不管用,模块的数据、标题都可以编辑
  5. Chapter 5: Container
  6. 【学习笔记】【C语言】注释
  7. C# 计时器的三种使用方法
  8. oracle锁
  9. &#39;WinMain&#39; : function cannot be overloaded
  10. PLSQL性能优化技巧
  11. Linux命令之进程的管理
  12. KM算法专题
  13. MySQL基本语句与经典习题
  14. exports
  15. java 运行环境
  16. 线程、进程、协程 异步io
  17. ElasticSearch入门介绍一
  18. jquery訪问ashx文件演示样例
  19. flexbox父盒子align-content属性
  20. (转)iPhone开发关于UDID和UUID的一些理解

热门文章

  1. bash 中的 :=、=、:-、-、=?、?、:+、+
  2. 消息队列: rabbitMQ
  3. 向PHP使用Post方式上传文件
  4. app嵌套的vue项目更新后,页面没有更新
  5. js页面加载时候的调用函数的方法
  6. jeesite直接登录——真实破解
  7. MySQL数据库笔记五:多表查询
  8. 怎么处理Win7系统备份还原提示代码0x80042302的错误?
  9. 解决国内安装tensorflow, opencv等安装不成功或下载太慢问题
  10. Beta冲刺版本第二天