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个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...
随机推荐
- Redis在实际项目中的一应用场景
1.在游戏的等级排名,可以将用户信息放入到redis的有序集合中,然后取得相应的排名,不用自己写代码去排序. 2.利用rediss的数据特性的自增,自减属性,可以将项目中的一些列入阅读数,点赞数放入到 ...
- POJ1185:炮兵阵地(状压dp)
题目:http://poj.org/problem?id=1185 大神的题解: 方法就是用DP[i][r][p]表示第i行状态为r,第i-1行状态是p时的最多个数.而这里p受到r的限制,而第i-2行 ...
- WSDL(WebService描述语言)文件介绍
一.WSDL 1.WSDL 文档的组成部分 <portType>:web service 执行的操作 <message>:web service 使用的消息 <types ...
- 网络爬虫Java实现抓取网页内容
package 抓取网页; import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream; ...
- 64. Minimum Path Sum(最小走棋盘 动态规划)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- AtCoder Regular Contest 080 C - 4-adjacent
地址:http://arc080.contest.atcoder.jp/tasks/arc080_a 题目: C - 4-adjacent Time limit : 2sec / Memory lim ...
- ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat (树链剖分-线性变换线段树)
树链剖分若不会的话可自行学习一下. 前两种操作是线性变换,模\(2^{64}\)可将线段树全部用unsigned long long 保存,另其自然溢出. 而取反操作比较不能直接处理,因为其模\(2^ ...
- Gym - 101875I I Will Go (dfs序)
题意:N个人要参加一个局,每个人有自己的好朋友,如果他的好朋友来,他才有可能来.N个人的关系不够成环.Q次查询,问若x来了,y是否肯定来. 分析:若点y是x的祖先,则y肯定回来.一次dfs确定每个点覆 ...
- unity手势插件《FingerGestures 》使用入门
什么是FingerGestures? FingerGestures是Unity上,非常热门的一款用于处理用户输入的插件 为什么要使用FingerGestures? 1:它统一了鼠标点击和用户触摸的输入 ...
- Ubuntu16.04 Docker 安装
前提条件 Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker. 通过 uname -r 命令查看你当前的内核版本 ...