The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team(排序+二分)
这题其实就是瞎搞,稍微想一想改一改就能过。
排序按值的大小排序,之后从后向前更新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(排序+二分)的更多相关文章
- 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 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team
题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team (线段树)
题目链接:https://nanti.jisuanke.com/t/41387 题目大意:对于给定序列,求出对于每个位置求出比该数大于m的最靠右的位置. 思路:首先对序列进行离散化,然后对于每个数的下 ...
- 计蒜客 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 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019
A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...
- 计蒜客 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 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING
题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...
- 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 ...
- 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); ...
随机推荐
- 《深入理解Java虚拟机》读书笔记二
第三章 垃圾收集器与内存分配策略 1.判断对象是否已死 引用计数法: 给对象添加一个引用计数器,每当有一个地方引用它时,计数器值就加1,每当引用失效时,计数器值就减1. 任何时刻计数器为0的对象就是不 ...
- 【转】VS2017离线安装
[转自]https://www.cnblogs.com/feigao/p/8409606.html 第一步:下载离线安装包 https://www.visualstudio.com/zh-hans/d ...
- oracle 锁表处理
1.查询 select object_name,machine,s.sid,s.serial#from v$locked_object l,dba_objects o ,v$session swher ...
- please execute the cleanup command
解决方法: (1)用dos命令进入项目文件夹,运行svn cleanup:不要直接右键点击找cleanup选项 (2)到上一层目录去cleanup试下,或者到.svn文件夹下(隐藏的)找到所有的loc ...
- 一次列表页伪静态的实现;结合nginx rewrite
nginx伪静态: rewrite ^/(.*)-htm-(.*)$ /$1.php?$2; 将 list-html-t-3-p-4.html 转到list.php?t-3-p-4 t-3-p-4 用 ...
- C++-POJ2352-Stars[数据结构][树状数组]
/* 虽然题目没说,但是读入有以下特点 由于,输入是按照按照y递增,如果y相同则x递增的顺序给出的 所以,可以利用入读的时间进行降为处理 */ 于是我们就得到了一个一维的树状数组解法啦 值得一提:坐标 ...
- TOYS(计算几何-入门)
题目 ‘^’代表叉乘 ‘•’代表点乘 点积:a•b=ax*bx+ay*by 叉积:a^b=ax*by-bx*ay
- AcWing 两个简单的位运算操作
//是柱状数组的一个基操作 //返回n的最后一位1:lowbit(n) = n & -n //比如 x=1010 那么返回10 x=101000 返回1000 #include<bits ...
- 网络https工作原理
网络https工作原理 待办 https://www.runoob.com/w3cnote/https-ssl-intro.html
- access denied (2)
除了https://www.cnblogs.com/bneglect/p/11558593.html 这里提到的以外,今天再记录一个新的原因造成这种情况的解决方法.path_info 这个算是路由模 ...