D - Laying Cables

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

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.

Sample Input

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

题意:给定一维数轴上的N个点和对应的权值,同时定义一个点的父亲为:离它最近且权值比它大的点下标,如果距离相同则选择权值较大的,如果没有则为-1,求出所有点的父亲?

思路1:单调栈预处理出左边和右边第一个比它大的元素的下标即可。
单调栈的作用:求出某个数的左边或右边第一个比它大或比它小的元素,复杂度O(n)。
单调递增栈:元素从栈底到栈顶严格单调递增。
单调递减栈:元素从栈底到栈顶严格单调递减。 代码1:
 #include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mt(A,B) memset(A,B,sizeof(A))
#define mod 1000000007
using namespace std;
typedef long long LL;
const int N=+;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL val[N],id[N],x[N],ans[N],S[N],L[N],R[N];//S[N]表示单调栈
bool cmp(LL a,LL b)
{
return x[a]<x[b];
}
int main()
{
#ifdef Local
freopen("data.txt","r",stdin);
#endif
ios::sync_with_stdio();
LL i,j,k,n;
cin>>n;
for(i=;i<=n;i++)
{
cin>>x[i]>>val[i];//x[i]表示在数轴上的位置,val[i]表示该点的价值
id[i]=i;
}
sort(id+,id++n,cmp);//按在数轴上的位置从小到大排序
k=;S[k++]=;val[]=2e9;//先把INF压入栈底
for(i=;i<=n;i++)
{
while(val[S[k-]]<=val[id[i]])k--;//如果当前的val比栈顶的大,就把栈顶的元素弹出来
L[id[i]]=S[k-];//找到了左边第一个比他大的数的下标
S[k++]=id[i];//把当前的下标压入栈
}
k=n+;S[k--]=n+;val[n+]=2e9;
for(i=n;i>=;i--)//同理
{
while(val[S[k+]]<=val[id[i]])k++;
R[id[i]]=S[k+];
S[k--]=id[i];
}
for(i=;i<=n;i++)
{
if(L[i]==&&R[i]==n+)ans[i]=-;
else if(L[i]==)ans[i]=R[i];
else if(R[i]==n+)ans[i]=L[i];
else
{
if(abs(x[L[i]]-x[i])<abs(x[R[i]]-x[i]))ans[i]=L[i];
else if(abs(x[L[i]]-x[i])>abs(x[R[i]]-x[i]))ans[i]=R[i];
else
{
if(val[L[i]]<val[R[i]])ans[i]=R[i];
else if(val[L[i]]>val[R[i]])ans[i]=L[i];
}
}
}
for(i=;i<=n;i++)
{
if(i==n)cout<<ans[i]<<endl;
else cout<<ans[i]<<" ";
}
}

思路2:先按数轴上的位置排序,RMQ预处理区间的最大值,再往左往右二分。训练的时候并不会单调栈,所以只能用RMQ二分瞎几把乱搞。

代码2:

 #include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mod 1000000007
#define mt(A) memset(A,0,sizeof(A))
using namespace std;
typedef long long LL;
const int N=+;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL dp[N][];
int ans[N];
struct node
{
LL dis,val;
int id;
} a[N],b[N];
bool cmp(struct node a,struct node b)
{
if(a.dis!=b.dis)return a.dis<b.dis;
else return a.val>b.val;
}
void RMQ(int n)
{
for(int i=; i<=n; i++)dp[i][]=a[i].val;
for(int j=; (<<j)<=n; j++)
{
for(int i=; (i+(<<j)-)<=n; i++)
{
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
}
LL query(int l,int r)
{
int k=;
while((<<(k+))<=(r-l+))k++;
return max(dp[l][k],dp[r-(<<k)+][k]);
}
struct node found(int l,int r,LL Val)//you
{
int mid;
struct node p1;
LL V;
while(l<r)
{
mid=(l+r)/;
V=query(l,mid);
if(V>Val)
{
r=mid;
}
else
{
l=mid+;
}
}
//cout<<l<<" "<<r<<endl;
if(a[l].val>Val)p1=a[l];
else p1.id=-;
return p1;
}
struct node found1(int l,int r,LL Val)//you
{
int mid;
struct node p1;
LL V;
while(l<=r)
{
mid=(l+r)/;
V=query(mid,r);
if(V>Val)
{
l=mid+;
}
else
{
r=mid-;
}
}
//cout<<l<<" "<<r<<endl;
if(a[r].val>Val)p1=a[r];
else p1.id=-;
return p1;
}
int main()
{
#ifdef Local
freopen("data","r",stdin);
#endif
int i,j,k,n;
cin>>n;
for(i=; i<=n; i++)
{
scanf("%lld%lld",&a[i].dis,&a[i].val);
a[i].id=i;
b[i]=a[i];
}
sort(a+,a+n+,cmp);
RMQ(n);
for(i=; i<=n; i++)
{
int l,r,mid;
struct node p1,p2;
p1.id=-;p2.id=-;
LL V;
l=;
r=i;
p1=found1(l,r,a[i].val);
l=i;r=n;
p2=found(l,r,a[i].val);
//cout<<i<<" "<<p1.id<<" "<<p2.id<<endl;
if(p2.id==-&&p1.id==-)ans[a[i].id]=-;
else if(p1.id==-&&p2.id!=-)ans[a[i].id]=p2.id;
else if(p1.id!=-&&p2.id==-)ans[a[i].id]=p1.id;
else
{
if(abs(a[i].dis-p1.dis)>abs(a[i].dis-p2.dis))
{
ans[a[i].id]=p2.id;
}
else if(abs(a[i].dis-p1.dis)<abs(a[i].dis-p2.dis))
{
ans[a[i].id]=p1.id;
}
else
{
if(p1.val>p2.val)ans[a[i].id]=p1.id;
else ans[a[i].id]=p2.id;
}
}
}
for(i=;i<=n;i++)
{
if(i==n)printf("%d\n",ans[i]);
else printf("%d ",ans[i]);
}
}


Code Forces Gym 100971D Laying Cables(单调栈)的更多相关文章

  1. Gym 100971D Laying Cables 单调栈

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

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

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

  3. Codeforces gym 100971 D. Laying Cables 单调栈

    D. Laying Cables time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

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

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

  5. Code Forces Gym 100886J Sockets(二分)

    J - Sockets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Valera ...

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

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

  7. Gym 100971D 单调栈

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

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

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

  9. Gym - 101334F 单调栈

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

随机推荐

  1. php简简单单搞定中英文混排字符串截取,只需2行代码!

    提到中英文混排计数.截取,大家首先想到的是ascii.16进制.正则匹配.循环计数. 今天我给大家分享的是php的mb扩展,教你如何轻松处理字符串. 先给大家介绍用到的函数: mb_strwidth( ...

  2. Building Python 2.7.10 with Visual Studio 2010 or 2015 - Google Chrome

    您的浏览器(Chrome 33) 需要更新.该浏览器有诸多安全漏洞,无法显示本网站的所有功能. 了解如何更新浏览器 × p-nand-q.com C++  Python  Programming  L ...

  3. Csharp Winfrom 多串口通信

    Csharp 多串口通信 顾名思义,多串口通信,普通的PC机一般只有一个串口,现在很多家用的PC都没有串口,那么问题来了,如何保证多串口呢? 有一种神器,MOXA CP-168U Series PCI ...

  4. 《C和指针》章节后编程练习解答参考——第9章

    9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...

  5. asp.net学习资源汇总

    名称:快速入门地址:http://chs.gotdotnet.com/quickstart/描述:本站点是微软.NET技术的快速入门网站,我们不必再安装.NET Framework中的快速入门示例程序 ...

  6. 关于 jquery.showLoading 中 出现的 图标不在页面中间的问题

    很多人喜欢 showLoading   因为 这个实在是太简单了直接 showLoading() hideLoading() 就可以解决这个问题. 今天我们就来看一下  这个插件里面的一个错误 或者说 ...

  7. CT 来值班,让您安心过新年!

    春节,盼了整整一年的节日,我们一定要抛开工作,狠狠的开心,狠狠的幸福,但是作为苦逼的运维,你们真的能完全抛开工作(对网站不闻不问)吗?OneAPM CT 24 小时监控您的网站,让您无忧无虑过新年. ...

  8. draw9patch超详细教程

    这篇文章是android开发人员的必备知识,内容摘选自网络,友我为大家整理和总结,不求完美,但是有用. 视频教程地址:http://player.youku.com/player.php/sid/XM ...

  9. OA学习笔记-003-Hibernate3.6配置

    一.jar包:核心包, 必须包, jpa, c3p0, jdbc antlr-2.7.6.jarc3p0-0.9.1.jarcommons-collections-3.1.jardom4j-1.6.1 ...

  10. Android代码中使用Ping命令

    项目中需要搜索同一WIFI局域网中的设备并进行通信,暂时想到的办法是得到局域网网段的地址,因为同一局域网中的IP地址前三位是相同的,而第四位的范围从0~250,所以对第四位进行遍历搜索,能ping通的 ...