Codeforces gym 100971 D. Laying Cables 单调栈
2016-07-12 14:13
603 查看
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
output
input
output
input
output
思路:找到左边和右边比他大的值,最后比较左右;
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+10,M=1e6+10,inf=1e9+10; struct is { int x,val; int pos; }a ; int cmp(is x,is y) { return x.x<y.x; } int d ,k; int l ,r ; int ans ; int check(int x,int len) { if(x<1||x>len) return 0; return 1; } int main() { int x,y,z,i,t; scanf("%d",&x); for(i=1;i<=x;i++) scanf("%d%d",&a[i].x,&a[i].val),a[i].pos=i; sort(a+1,a+1+x,cmp); a[0].val=a[x+1].val=inf; k=0; d[++k]=0; for(i=1;i<=x;i++) { while(a[d[k]].val<a[i].val)k--; l[i]=d[k]; d[++k]=i; } k=0; d[++k]=x+1; for(i=x;i>=1;i--) { while(a[d[k]].val<a[i].val)k--; r[i]=d[k]; d[++k]=i; } for(i=1;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]=-1; } for(i=1;i<=x;i++) printf("%d ",ans[i]); return 0; }
相关文章推荐
- 空调原理与能量守恒定律无关
- php.ini翻译解析
- 【Shiro】Apache Shiro架构之集成web
- mysql当前时间,日期转换,日期格式化
- QKingbase的批量操作
- Win8系统下VS2012添加MSComm控件
- Android 界面滑动卡顿分析与解决方案(入门)
- linux ERROR c.a.d.s.DruidDataSourceStatManager - unregister mbean error javax.management.InstanceNo
- MyEclipse 快捷键1(CTRL)
- 抽象布局 include merge ViewStub 自定义标题
- ubuntu安装和配置SVN
- 动态规划求解最长公共子序列(LCS)
- BFC问题
- HDOJ 4277 USACO ORZ DFS+剪枝
- ANN神经网络
- Spring和Mybatis整合时无法读取properties的处理方案
- Linux系统postfix邮件部署
- Eclipse让js进行代码提示,做到和java文件一样;---HTML/CSS/JSP代码提示,同理
- SecureCRT 7.3.4破解版(含注册机)
- jdbc学习