Codeforces gym 100971 D. Laying Cables 单调栈
2 seconds
256 megabytes
standard input
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.
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 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.
4
1 1000
7 10
9 1
12 100
-1 4 2 1
3
1 100
2 1
3 10
-1 1 1
3
1 10
3 100
2 1
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 单调栈的更多相关文章
- Gym 100971D Laying Cables 单调栈
Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...
- Codeforces 1107G Vasya and Maximum Profit [单调栈]
洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...
- Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...
- codeforces gym 100971 K Palindromization 思路
题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...
- codeforces 817 D. Imbalanced Array(单调栈+思维)
题目链接:http://codeforces.com/contest/817/problem/D 题意:给你n个数a[1..n]定义连续子段imbalance值为最大值和最小值的差,要你求这个数组的i ...
- Educational Codeforces Round 23 D. Imbalanced Array 单调栈
D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces Gym 100971 A、B、C、F、G、K、L
A题 直接把问号全部变为陆地如果所有陆地连通 那么再逐个把刚才变成陆地的问号变为水如果依旧连通有多种解 为什么我的代码跑不过去,和网上的题解思路一模一样!!?? #include<cst ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- D - Laying Cables Gym - 100971D (单调栈)
题目链接:https://cn.vjudge.net/problem/Gym-100971D 题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...
随机推荐
- Clairewd’s message--hdu4300(Next数组的运用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题意就是给你26的字母的加密方式,然后又给了一个串s1是包含加密后的和没有加密的但是没有加密的可 ...
- linux知识体系
0. Linux简介与厂商版本 1. Linux开机启动 2. Linux文件管理 3. Linux的架构 4. Linux命令行与命令 5. Linux文件管理相关命令 6. Linux文本流 7. ...
- python自然语言处理——学习笔记:Chapter3纠错
2017-12-06更新:很多代码执行结果与书中不一致,是因为python的版本不一致.如果发现有问题,可以参考英文版: http://www.nltk.org/book/ 第三章,P87有一段处理h ...
- spring 自定义事物同步器(一): TransactionSynchronizationManager 解析
一..JPA 获取 Hibernate的session try { session = entityManager.unwrap(Session.class); } catch (Exception ...
- python学习笔记(十三)接口开发
一.开发接口的作用 1.mock接口,模拟一些接口,在别的接口没有开发好的时候,需要开发一些模拟接口进行调试和测试. 2.查看数据,比如,获取所有学员信息的接口,就不需要提供数据库的查看信息. 二.接 ...
- Storm程序永久代内存溢出
在集群中部署Storm应用程序的时候报错,并通过jdk自带的jconsole监控发现,永久代内存瞬间爆炸了 org.springframework.beans.factory.BeanCreation ...
- su 与 su - 区别
su与su -都是用来切换用户的命令,简单说它们之间的区别就是:su -切换的干净彻底,而su 切换用户却拖泥带水. su su username,切换到指定用户,但是当前目录不会变化,环境变量还是上 ...
- PAT 1078 Hashing[一般][二次探查法]
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
- SHELL脚本自动备份数据库
#!/bin/bash db_user="root" db_passwd="dddddd " db_name="mmmmmm" cd /db ...
- php微信支付接口开发程序(流程已通)
php微信支付接口开发程序(流程已通) 来源:未知 时间:2014-12-11 17:11 阅读数:11843 作者:xxadmin [导读] 微信支付接口现在也慢慢的像支付宝一个可以利 ...