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), ...
随机推荐
- Swift翻译之-关于Swift
IMPORTANT 重要的 This is a preliminary document for an API or technology in development. Apple is suppl ...
- Linux unzip解压文件到某个目录下面
1,例如我想解压Metinfo5.2.zip 到某一个文件夹下,执行下面的命令就可以了 sudo unzip MetInfo5.2.zip -d metinfo-bak
- 关于angularjs中的ng-class 变量问题
时常会用到ng-class.非常的方便,基本的用法就是 当title等于通金所的时候,就增加error-tip这个class,但是,我们有时候这个值不能写死,明白我的意思吗,明白吗,好吧,反正就要是个 ...
- Python webpy微信公众号开发之 回复图文消息
新建图文回复模板reply_pictext.xml: $def with (toUser,fromUser,createTime,title1,description1,picurl1,url1)&l ...
- HDU2191多重背包例题
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d ...
- Shell编程基础
写之前我们先来搞清楚为什么要学shell,学习要有目的性shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题shell可以实现自动化管理,让系统管理员的工作变得容易.简单.高效shell脚 ...
- Android runProguard配置 导致module lib 中的包编译时无法识别
今天写代码时用到了另一个lib型的工程,把它添加到dependencies后,在原工程中可以引用lib中的文件了,但是编译时就会报错,提示包不存在,后来在build.gradle中设置runProgu ...
- DroidDraw - Android的界面设计工具
ADT中的界面开发工具实在是很烂,通常情况下都需要硬编码,对于程序员来说不但效率比较低下,而且调试起来极其不方便,还好在Google未推出GUI的"所见即所得"的工具之前,我们找到 ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- php的socket通信(二)
案例一:代码详解 // 设置一些基本的变量$host = "192.168.1.99";$port = 1234;// 设置超时时间set_time_limit(0);// 创建一 ...