ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

XKC's basketball team(单调栈+二分)

2019-09-13 13:00:56  阅读:269  来源: 互联网

标签:arr basketball sta int XKC top wi leq team


XKC , the captain of the basketball team , is directing a train of nnn team members. He makes all members stand in a row , and numbers them 1⋯n1 \cdots n1⋯n from left to right.

The ability of the iii-th person is wiw_iwi​ , and if there is a guy whose ability is not less than wi+mw_i+mwi​+m stands on his right , he will become angry. It means that the jjj-th person will make the iii-th person angry if j>ij>ij>i and wj≥wi+mw_j \ge w_i+mwj​≥wi​+m.

We define the anger of the iii-th person as the number of people between him and the person , who makes him angry and the distance from him is the longest in those people. If there is no one who makes him angry , his anger is −1-1−1 .

Please calculate the anger of every team member .

Input

The first line contains two integers nnn and m(2≤n≤5∗105,0≤m≤109)m(2\leq n\leq 5*10^5, 0\leq m \leq 10^9)m(2≤n≤5∗105,0≤m≤109) .

The following  line contain nnn integers w1..wn(0≤wi≤109)w_1..w_n(0\leq w_i \leq 10^9)w1​..wn​(0≤wi​≤109) .

Output

A row of nnn integers separated by spaces , representing the anger of every member .

样例输入

6 1
3 4 5 6 2 10

样例输出

4 3 2 1 0 -1

解题思路:单调栈+二分 从后往前维护一个单调递增的栈 然后二分查找即可!
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n,m;
 4 const int maxn=5e5+5;
 5 int arr[maxn];
 6 int sta[maxn];
 7 int top=0;
 8 map<int,int> ma;
 9 
10 int Binary(int left,int right,int value){
11     int ans=-1;
12     while(left<=right){
13         int mid=left+right>>1;
14         if(arr[sta[mid]]>=value) ans=mid,right=mid-1;
15         else left=mid+1;
16     }
17     return ans;
18 }
19 
20 int main(){
21     ios::sync_with_stdio(false);
22     cin>>n>>m;
23     for(int i=1;i<=n;i++) cin>>arr[i];
24     int top=1;
25     sta[top]=n;ma[n]=-1;
26     for(int i=n-1;i>=1;i--){
27         if(arr[i]>arr[sta[top]]-m) ma[i]=-1;
28         else{
29             int flag=Binary(1,top,arr[i]+m);   //找大于等于这个数的最左边的位置
30 //            cout << arr[sta[flag]] << endl;
31             ma[i]=sta[flag]-i-1;
32         }
33         if(arr[i]>arr[sta[top]]) sta[++top]=i;
34     }
35     for(int i=1;i<=n;i++){
36         printf("%d%c",ma[i],i==n?'\n':' ');
37     }
38     return 0;
39 }
40 
41 //10 1
42 //3 4 5 6 2 20 10 30 40 50
View Code

 

标签:arr,basketball,sta,int,XKC,top,wi,leq,team
来源: https://www.cnblogs.com/qq-1585047819/p/11516667.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有