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. CSS三角形广告文字

    街上经常碰到一些发各类广告传单的,有一次收到一张房地产广告的传单,顺手留下来,看着里面有些广告挺吸引人,同时也想练练自己css技术,故抽空做了一下. 原图某区域如下: 实现上图效果是需要一些想象力的, ...

  2. 服务器端启动soket多线程

    方法一: Socket socket=null try{ ServerSocket serversocket=nwe ServerSocket(8080) while(true){ socket=se ...

  3. C# MySql 操作类

    /* MySql 类 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  4. Csharp Winfrom 多串口通信

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

  5. warning: no newline at end of file

    编译错误:warning: no newline at end of file原因:程序结尾需要有一个空行解决办法:在程序末尾多打个回车就行了

  6. sql server 表空间

    在SqlServer2005中,建表时是默认把所有的表都保存在PRIMARY默认表空间中的.当数据库中表很多,并且数据量很大时,会导致数据库性能严重下降,有必要将一些大的表放到不同的表空间中去.主要的 ...

  7. iOS 之 多线程一

    iOS中实现多线程的技术方案 pthread 实现多线程操作 代码实现: void * run(void *param) {    for (NSInteger i = 0; i < 1000; ...

  8. PPT2010小技巧 教你如何快捷抠图

    相信不少人在做PPT时,都有想插入个漂亮个性图案的想法,但是往往手头上的图片都不太令人满意,需要“裁剪”一下才能达不到自己想要的效果.这时大部分人可能会PS,但是相比起今天要分享给大家的方法,步骤就显 ...

  9. the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header

    the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...

  10. Word续上表

    选中表格的下半部分(全部下半部分),然后按ctrl+shift+enter,这样就换行了.