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

排序按值的大小排序,之后从后向前更新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. 数据库Dao层编增删改查写,数据库事务,数据库升级

    数据库事务 有两个特点 1.安全性 情景:正常的转账行为,这个时候如果出现停电等异常,已经扣钱但是没有加钱:这个时候就可用数据库事务解决问题 2.高效性: 使用数据库事务添加享受同数量的数据,对比耗时 ...

  2. Exception in thread "http-apr-8080-exec-1" java.lang.StackOverflowError

    Exception in thread "http-apr-8080-exec-1" java.lang.StackOverflowError 可能执行了递归,陷入了死循环 如下我 ...

  3. C语言结构体数组遇上typedef

    昨天韩同学在做数据结构题的时候,问了我一个关于typedef 与结构体数组的问题: typedef struct vexnode { int vertex; arcnode* firstarc; }a ...

  4. 注释web.xml

    注释掉红框里的内容

  5. sql查询 —— 模糊查询

    --模糊查询 --like --%至少替换一个 -- _只替换一个 -- 查姓李的人 select *from student name like "李%"; -- 查名为杰伦的人 ...

  6. util之Stack

    定义 Stack<Integer>s = new Stack<>(); boolean isEmpty()  测试堆栈是否为空. Object peek( ) 查看堆栈顶部的对 ...

  7. 链接测试工具:Xenu

    Xenu 是一款深受业界好评,并被广泛使用的死链接检测工具.时常检测网站并排除死链接,对网站的 SEO(搜索引擎优化) 非常重要,因为大量死链接存在会降低用户和搜索引擎对网站的信任. 最大支持100线 ...

  8. 微信环境中如何实现跳转到手机默认外部浏览器下载apk文件

    由于微信的限制,应用文件在内置浏览器中下载全部被屏蔽掉,造成很多人用微信扫描二维码下载时点击下载按钮没反应,我想到的是做一个提示用户在浏览器中打开下载.其实原来很简单,就是判断当前是在微信内置浏览器中 ...

  9. 矩阵快速幂 F[n]=F[n-2]*2+F[n-1]+i^4 hdu5950

    #include<cstdio> #include<algorithm> #include<math.h> #include<string.h> usi ...

  10. beego登录退出与检查登录过滤器

    // ShowLogin 登陆显示 func (c *UserController) ShowLogin() { username := c.Ctx.GetCookie("username& ...