这题其实就是瞎搞,稍微想一想改一改就能过。

排序按值的大小排序,之后从后向前更新node节点的loc值,如果后一个节点的loc大于(不会等于)前一个节点的loc,就把前一个节点的loc值设置为后面的loc值。

这样的话, 后面的节点的值是大于前面节点的,如果该节点的loc也大于前面节点的,说明前面节点的loc就没有用了,用后面更大的数的更大的loc就可以了。

之后是一个二分,二分写的时候要把各种情况处理清楚,比如,m的值为0,搜索的时候可能会搜到它自己,所以如果搜索val+m,搜到的节点的loc<=i的话,说明就是它本身,或者就是一个比它大的值,m不为0,但是loc却小于了i,说明这个也是无效值。

以及val+m之后值过大,搜索不到,此时的l就会大于n,或者最后搜到的val大于了我们要搜的val,但是位置也小于我们当前的位置i,此时也是无效值,返回-1就可以了。

这题的二分搜索处理好就可以了,复杂度2e7左右。

#include <bits/stdc++.h>
using namespace std;
const int maxn=5e5+10; struct Node {
long long val,loc;
}node[maxn]; long long num[maxn];
long long n,m; bool cmp(const Node &a,const Node &b)
{
if (a.val!=b.val) {
return a.val<b.val;
}
return a.loc<b.loc;
} long long BinarySearch(long long val,int i)
{
long long l=1,r=n,mid;
while (l<=r) {
mid=(l+r)/2;
if (node[mid].val==val) {
if (node[mid].loc<=i) {
return -1;
}
else {
return node[mid].loc-i-1;
}
}
else if (node[mid].val<val) {
l=mid+1;
}
else {
r=mid-1;
}
// printf("%lld %lld %lld\n",l,r,mid);
}
return l>n||node[l].loc<=i?-1:node[l].loc-1-i;
} int main()
{
scanf("%lld%lld",&n,&m);
for (int i=1;i<=n;i++) {
scanf("%lld",&node[i].val);
node[i].loc=i;
num[i]=node[i].val;
}
sort(node+1,node+n+1,cmp);
for (int i=n;i>1;i--) {
if (node[i].loc>node[i-1].loc) {
node[i-1].loc=node[i].loc;
}
// printf("%lld %lld\n",node[i].val,node[i].loc);
}
for (int i=1;i<n;i++) {
printf("%lld ",BinarySearch(num[i]+m,i));
}
printf("%lld\n",BinarySearch(num[n]+m,n)); return 0;
}
/*
5 0
5 5 5 5 5
9 2
1 2 3 4 10 7 8 11 9
7 2
1 2 10 3 1 3 2
7 2
1 9 10 3 1 3 2
5 0
1 4 3 2 2
5 2
1 4 3 2 2
*/

The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team(排序+二分)的更多相关文章

  1. The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]

    也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...

  2. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team

    题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...

  3. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team (线段树)

    题目链接:https://nanti.jisuanke.com/t/41387 题目大意:对于给定序列,求出对于每个位置求出比该数大于m的最靠右的位置. 思路:首先对序列进行离散化,然后对于每个数的下 ...

  4. 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)

    query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...

  5. The Preliminary Contest for ICPC Asia Xuzhou 2019

    A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...

  6. 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛

    XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...

  7. The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING

    题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...

  8. G.Colorful String(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/4 #include <bits/stdc++.h> using namespace std; ,; typedef unsign ...

  9. E.XKC's basketball team(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/41387 解: 离散化+线段树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); ...

随机推荐

  1. 动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)

    分析: 完整代码: // 最长公共子序列 #include <stdio.h> #include <algorithm> using namespace std; ; char ...

  2. Mahmoud and Ehab and the message

    Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from ...

  3. 【C语言】输入一个整数x并判断x是否存在于数组a中

    #include<stdio.h> int main() { ] = { ,,,,,,,, };//数组初始化 printf("请输入要查找的数据:\n"); scan ...

  4. C# 获取变量的指针(IntPtr)

     1. 获取数组的指针(IntPtr) 通过Marshal.UnsafeAddrOfPinnedArrayElement(Array,Int32)方法获得一个数组的第某个元素的内存地址. Array是 ...

  5. python setup.py 安装和卸载 的正确姿势

    1.install python setup.py install --record files.txt 2. uninstall 删除这些文件 cat files.txt | xargs rm -r ...

  6. C正数负数的原码补码反码以及内存地址分析

    #include<stdio.h> void swap(int a, int b); void main1(){ int i = 10; //正数的原码 00000000 00000000 ...

  7. Css3 里的弹性盒的比例关系

    前两天朋友出去面试遇到了尴尬的问题,原题是:"在一个盒子里包裹着三个子元素,让子元素的宽度以1:1.1:2.1:3的关系依次展示" 这就尴尬了啊.................. ...

  8. html中实现数据的显示和隐藏

    Author: YangQingQing <!DOCTYPE html><html><meta http-equiv="Content-Type" c ...

  9. 题解【CJOJ2608】[JZOJ 100043]第k小数

    P2608 - [JZOJ 100043]第k小数 Description 有两个非负整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到N*M个数,询问这N*M个数中第K ...

  10. MyBatis Generator 超详细配置

    想快速开始,请直接拉到最后,看整体配置. MyBatis Generator 是 MyBatis 提供的一个代码生成工具.可以帮我们生成 表对应的持久化对象(po).操作数据库的接口(dao).CRU ...