D - Laying Cables Gym - 100971D (单调栈)
题目链接:https://cn.vjudge.net/problem/Gym-100971D
题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父亲具体满足的条件是:作为父亲的城市的坐标和当前城市的人数最多,,如果有多个满足的城市,则和原来的点相隔最近的作为父亲。
具体思路:首先,题目中说坐标和人数是不会有相同的值,所以我们对于每一个点,按照x轴进行排序之后,找出当前点的左边的第一个人数大于当前点的,然后再找出当前点的右边的第一个人数大于当前点的,这个过程可以通过单调栈来维护,然后再具体找的过程中,先看当前的这个点的左边和右边时候都存在,如果都没有,就证明没有父亲,如果有一个,这个点就是父亲,如果都有的话,就是寻找相隔最近的就可以了。
AC代码:
#include<iostream>
#include<stack>
#include<cmath>
#include<stdio.h>
#include<algorithm>
#include<stack>
#include<string>
using namespace std;
# define ll long long
const int maxn = 2e5+;
int L[maxn],R[maxn];
int val[maxn],x[maxn];
int pos[maxn],ans[maxn];
bool cmp(int t1,int t2){
return x[t1]<x[t2];
}
int main(){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d %d",&x[i],&val[i]);
pos[i]=i;
}
sort(pos+,pos+n+,cmp);
stack<int>q;
for(int i=;i<=n;i++){
if(!q.empty())L[pos[i]]=-;
while(!q.empty()&&val[q.top()]<=val[pos[i]])q.pop();
if(q.empty())L[pos[i]]=-;
else L[pos[i]]=q.top();
q.push(pos[i]);
}
while(!q.empty())q.pop();
for(int i=n;i>=;i--){
if(!q.empty())R[pos[i]]=-;
while(!q.empty()&&val[q.top()]<=val[pos[i]])q.pop();
if(q.empty())R[pos[i]]=-;
else R[pos[i]]=q.top();
q.push(pos[i]);
}
for(int i=;i<=n;i++){
if(L[i]==-&&R[i]==-)ans[i]=-;
else if(L[i]==-)ans[i]=R[i];
else if(R[i]==-)ans[i]=L[i];
else {
if(abs(x[R[i]]-x[i])>abs(x[L[i]]-x[i]))ans[i]=L[i];
else if(abs(x[R[i]]-x[i])<abs(x[L[i]]-x[i]))ans[i]=R[i];
else {
if(val[R[i]]>val[L[i]])ans[i]=R[i];
else ans[i]=L[i];
}
}
}
for(int i=;i<=n;i++){
if(i==)printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
return ;
}
D - Laying Cables Gym - 100971D (单调栈)的更多相关文章
- 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 单调栈
当时我的第一想法也是用单调栈,但是被我写炸了:我也不知道错在哪里: 看了大神的写法,用数组模拟的: 记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置. 当有数据要出栈的时候,说明栈里的数据 ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- 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 - 102028H Can You Solve the Harder Problem? (后缀数组+RMQ+单调栈)
题意:求一个序列中本质不同的连续子序列的最大值之和. 由于要求“本质不同”,所以后缀数组就派上用场了,可以从小到大枚举每个后缀,对于每个sa[i],从sa[i]+ht[i]开始枚举(ht[0]=0), ...
- Gym - 101102D Rectangles (单调栈)
Given an R×C grid with each cell containing an integer, find the number of subrectangles in this gri ...
随机推荐
- Uva1001-floyd算法-建图
给出一些球,球内的时间为零,球之间的速度为10每单位. 给两个点,求最短时间. 把每一个球当做点,球间的距离就是floyd的d数组.之后跑一遍floyd wa了两发因为d数组定义成int了 #incl ...
- Codeforces Round #412 B. T-Shirt Hunt
B. T-Shirt Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 【BZOJ4815】[CQOI2017]小Q的表格(莫比乌斯反演,分块)
[BZOJ4815][CQOI2017]小Q的表格(莫比乌斯反演,分块) 题面 BZOJ 洛谷 题解 神仙题啊. 首先\(f(a,b)=f(b,a)\)告诉我们矩阵只要算一半就好了. 接下来是\(b* ...
- sql server 小技巧(8) visual studio 2013里使用Sql server compact 4.0及发布问题处理
1. 安装 Microsoft SQL Server Compact 4.0 https://www.microsoft.com/zh-cn/download/confirmation.aspx?i ...
- cf1063B Labyrinth (bfs)
可以证明,如果我搜索的话,一个点最多只有两个最优状态:向左剩余步数最大时和向右剩余步数最大时 然后判一判,bfs就好了 dfs会T惨... #include<bits/stdc++.h> ...
- 几个面试经典算法题Java解答
题目一: public class testClockwiseOutput { //顺时针打印一个矩阵 @Test public void test(){ int[][] num = new int[ ...
- 洛谷P1600 天天爱跑步
天天放毒... 首先介绍一个树上差分. 每次进入的时候记录贡献,跟出来的时候的差值就是子树贡献. 然后就可以做了. 发现考虑每个人的贡献有困难. 于是考虑每个观察员的答案. 把路径拆成两条,以lca分 ...
- 【POJ3090】Visible Lattice Points
题目大意:求 \[\sum\limits_{i=2}^n\phi(i)\] 题解:利用与埃筛类似的操作,可在 \(O(nlogn)\) 时间求出结果. 代码如下 #include <cstdio ...
- 【洛谷P2617】Dynamic Rankings
题目大意:维护带修改区间 K 小值. 题解:学习到了树状数组套权值线段树. 主席树,即:可持久化权值线段树,支持维护静态区间的 K 小值问题,其核心思想是维护 N 棵权值线段树,每个线段树维护的是序列 ...
- (转)Eclipse配置GitHub代码库(以Windows7为例)
原文地址:http://blog.csdn.net/twlkyao/article/details/26340685 1.安装Git 首先安装git.这里只讲Windows环境下安装Git方法. 从G ...