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
(这题set也能水过。。。。。。
)
#include<stdio.h>
#include<math.h>
#include<vector>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
#define maxn 550000
using namespace std;
#include <unordered_map>
#include<map>
int a[maxn],ans[maxn];
struct node
{
int id,num;
friend bool operator <(node n1,node n2)
{
return n1.num>n2.num;//从小到大
}
}b[maxn];
int main()
{
int n,m;
memset(ans,-1,sizeof(ans));
priority_queue<node>que;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i].id=i;
b[i].num=a[i];
que.push(b[i]);
}
for(int i=n;i>=1;i--)
{
while(!que.empty())
{
node st=que.top();
//printf("i=%d id=%d num=%d\n",i,st.id,st.num);
if(st.id>=i) {que.pop();continue;}
if(st.num>a[i]-m) break;
ans[st.id]=i-st.id-1;
//printf("ans[%d]=%d\n",st.id,ans[st.id]);
que.pop();
}
}
for(int i=1;i<=n;i++)
{
//if(ans[i]==0) ans[i]=-1;
if(i==n)
{
printf("%d\n",ans[i]);
}
else printf("%d ",ans[i]);
}
}
本文介绍了一种算法,用于计算篮球队伍中成员的愤怒值。每个成员的能力不同,若右侧有人能力超过其m点,则会生气。算法通过并查集或set实现,计算每个人与使其生气者间的距离。
834

被折叠的 条评论
为什么被折叠?



