zoukankan      html  css  js  c++  java
  • XKC's basketball 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 n1n 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+mwjwi+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-11 .

    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(2leq nleq 5*10^5, 0leq m leq 10^9)m(2n5105,0m109) .

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

    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?'
    ':' ');
    37     }
    38     return 0;
    39 }
    40 
    41 //10 1
    42 //3 4 5 6 2 20 10 30 40 50
    View Code
  • 相关阅读:
    ubuntu 安装 Java 开发环境
    mtd-utils 的 使用
    容器技术与虚拟化技术
    Shell之作业控制
    Shell常用语句及结构
    Shell常用命令之read
    Shell之函数
    文件的copy
    类中调用初始化方法
    父类中的方法被覆盖以及子类调用父类覆盖的方法
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11516667.html
Copyright © 2011-2022 走看看