Gym 100971D Laying Cables 单调栈
Description
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
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+, M = 1e6+, mod = 1e9+, inf = 2e9;
typedef long long ll; int n,ans[N];
struct ss{long long x,p;int id;}a[N],lefts[N],rights[N];
vector<ss> G;
bool cmp(ss s1,ss s2) {return s1.x<s2.x;} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].p),a[i].id = i;
sort(a+,a+n+,cmp);
a[].x=-inf, a[].p = inf; a[].id = -;
G.push_back(a[]);
for(int i=;i<=n;i++) {
while(G.back().p<=a[i].p) G.pop_back();
lefts[i]=G.back();
G.push_back(a[i]);
}
G.clear();
a[n+] = (ss) {inf,inf,-};
G.push_back(a[n+]);
for(int i=n;i>=;i--) {
while(G.back().p<=a[i].p) G.pop_back();
rights[i]=G.back();
G.push_back(a[i]);
} for(int i=;i<=n;i++) {
if(abs(lefts[i].x-a[i].x)==abs(rights[i].x-a[i].x)) {
if(lefts[i].p>rights[i].p) ans[a[i].id] = lefts[i].id;
else ans[a[i].id] = rights[i].id;
}
else if(abs(lefts[i].x-a[i].x)<abs(rights[i].x-a[i].x)) {
ans[a[i].id] = lefts[i].id;
}
else ans[a[i].id] = rights[i].id;
}
for(int i=;i<=n;i++) printf("%d ",ans[i]);
printf("\n");
}
单调栈
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+, M = 1e6+, mod = 1e9+, inf = 2e9;
typedef long long ll; int n;
ll dp[N][];
int ans[N]; struct ss{long long x,p;int id;}a[N],lefts[N],rights[N]; bool cmp(ss s1,ss s2) {return s1.x<s2.x;}
ll cal(int l,int r)
{
if(l==r) return a[l].p;
int k = (int) (log((double) r-l+) / log(2.0));
return max(dp[l][k], dp[r - (<<k) + ][k]);
}
int main() {
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].p), a[i].id=i;
sort(a+,a+n+,cmp);
a[] = (ss) {-inf,inf,-};
a[n+] = (ss){inf,inf,-};
for(int i=;i<=n+;i++) dp[i][] = a[i].p;
for(int j=;(<<j)<=n+;j++) {
for(int i=;i + (<<j) - <= n+; i++) {
if(dp[i][j-] > dp[i+(<<(j-))][j-])
dp[i][j] = dp[i][j-];
else {
dp[i][j] = dp[i+(<<(j-))][j-];
}
}
}
//cout<<cal(0,1)<<endl;
for(int i=;i<=n;i++) {
int l = , r = i-,A=;
while(l<=r) {
int mid = (l+r) >> ;
if(cal(mid,i-)> a[i].p) l = mid+,A=mid;
else r = mid-;
}
lefts[i] = a[A];
// cout<<A<<" ";
l = i+, r = n+;A = n+;
while(l<=r) {
int mid = (l+r) >> ;
if(cal(i+,mid)> a[i].p) r=mid-,A = mid;
else l = mid+;
}
rights[i] = a[A];
// cout<<A<<endl;
} for(int i=;i<=n;i++) {
if(abs(lefts[i].x-a[i].x)==abs(rights[i].x-a[i].x)) {
if(lefts[i].p>rights[i].p) ans[a[i].id] = lefts[i].id;
else ans[a[i].id] = rights[i].id;
}
else if(abs(lefts[i].x-a[i].x)<abs(rights[i].x-a[i].x)) {
ans[a[i].id] = lefts[i].id;
}
else ans[a[i].id] = rights[i].id;
}
for(int i=;i<=n;i++) printf("%d ",ans[i]);
printf("\n"); }
RMQ+二分
Gym 100971D Laying Cables 单调栈的更多相关文章
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- 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 ...
- 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 单调栈
当时我的第一想法也是用单调栈,但是被我写炸了:我也不知道错在哪里: 看了大神的写法,用数组模拟的: 记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置. 当有数据要出栈的时候,说明栈里的数据 ...
- Gym - 102028H Can You Solve the Harder Problem? (后缀数组+RMQ+单调栈)
题意:求一个序列中本质不同的连续子序列的最大值之和. 由于要求“本质不同”,所以后缀数组就派上用场了,可以从小到大枚举每个后缀,对于每个sa[i],从sa[i]+ht[i]开始枚举(ht[0]=0), ...
随机推荐
- C语言or和and运算
#include <stdio.h> void main () { printf( | ); printf( | ); printf( | ); printf( | ); printf( ...
- [Winform]DataGridView列自适应宽度
引言 在做winform项目中,数据控件DataGridView的使用多多少少是会用到的,如果不设置它的属性,默认情况下是不会自适应宽度的,你想查看某项的数据,就不得不将标题栏拖来拖去,挺烦的. 方法 ...
- Socket网络编程(1)
TCP/IP 简单介绍 应用层 (Application):应用层是个很广泛的概念,有一些基本相同的系统级 TCP/IP 应用以及应用协议,也有许多的企业商业应用和互联网应用. 传输层 (Transp ...
- DOM: 如何获取元素下的第一个子元素
Element.firstChild ?,是的,这是第一种方法,当然,通常来说支持 W3C 规范的浏览器,如 Firefox 等取到的应该是 TEXT_NODE.很早之前,或者说现在最流行的方法可能是 ...
- There are no interfaces on which a capture can be done.
There are no interfaces on which a capture can be done. 今天启动了Wireshark 但是提示→There are no interfaces ...
- 抓包工具 wireshark
http://fangxin.blog.51cto.com/1125131/735178
- google推出的SwipeRefreshLayout下拉刷新用法
使用如下: 1.先下载android-support-v4.jar最新版本,之前的版本是没有SwipeRefreshLayout下拉刷新控件的,如果已经更新,此步骤可省略. 2.在xml文件中引用an ...
- qsort用法总结
一.对int类型数组排序 ]; int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; } qsort(nu ...
- 查看现有运行的linux服务器有多少内存条
i161 admin # ssh 192.168.5.209 dmidecode | grep 'Ending Address' -B1 -A2 Starting Address: 0x0000 ...
- SQL union和union all的区别
Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...