Code Forces Gym 100971D Laying Cables(单调栈)
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
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 题意:给定一维数轴上的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(单调栈)的更多相关文章
- Gym 100971D Laying Cables 单调栈
Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...
- Gym 100971D Laying Cables 二分 || 单调栈
要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...
- Codeforces gym 100971 D. Laying Cables 单调栈
D. Laying Cables time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Gym - 101102D Rectangles (单调栈)
Given an R×C grid with each cell containing an integer, find the number of subrectangles in this gri ...
- Code Forces Gym 100886J Sockets(二分)
J - Sockets Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Valera ...
- D - Laying Cables Gym - 100971D (单调栈)
题目链接:https://cn.vjudge.net/problem/Gym-100971D 题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...
- Gym 100971D 单调栈
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- Gym - 101334F 单调栈
当时我的第一想法也是用单调栈,但是被我写炸了:我也不知道错在哪里: 看了大神的写法,用数组模拟的: 记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置. 当有数据要出栈的时候,说明栈里的数据 ...
随机推荐
- 【转】ORACLE日期时间 等函数大全
转自:ORACLE日期时间函数大全 ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: ...
- javascript 写策略模式,商场收银打折优惠策略
[Decode error - output not utf-8] ----------------------------- 购物清单 方便面 : 100 x 50 = 5000 | 4000 菊花 ...
- Dataset
1,if(ds == null) 这是判断内存中的数据集是否为空,说明DATASET为空,行和列都不存在!! 2,if(ds.Tables[0].Count == 0) 这应该是在内存中存在一个DAT ...
- mysql扩展库操作mysql数据库
环境搭建 启用mysql扩展库,在php.ini文件中去配置mysql扩展库 extension=php_mysql.dll 查询数据库 1.建库建表 //建库testcreate database ...
- php设计模式:工厂模式
php设计模式:工厂模式 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类. 工厂模式实现: 工厂模式中任何创建对象的工厂类都要实现这个接口,实现接口的方法体中都要实现接口中的方法,它声明 ...
- php图片上传代码
使用copy函数 if (!empty($_FILES)) { //图片 if(isset($_FILES['image'])) { $img_data = $_FILES['image']['tmp ...
- php最短的HTTP响应代码
刚刚发现在CodeProject给我推送了一篇文章叫:the Shortest PHP code for Returning HTTP Response Code 翻译过来就是(PHP最短的HTTP ...
- torch7在mac上的安装
在安装torch7的时候,一开始一直参考的是官方的文档: http://torch.ch/docs/getting-started.html#_ 但是无论怎么装都无法成功,主要是卡在了source那一 ...
- iBatis入手案例
第一部分,iBatis组织架构分析 1.1 组织架构图 1.2 架构分析 DAO层上面,DAO类通过SqlMapConfig文件,来构建iBatis提供的SqlMapClient,SqlMapConf ...
- 简谈Comparable和Comparator区别
对于Comparable和Comparator这连个相似的接口,还是做一下比较比较好: Comparable Comparator (1)只包含一个compareTo()方法,此方法可以给两个对象排序 ...