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注释&标识符
注释: 单行注释:// 多行注释:/* */ 文档注释:/** */ 其中文档注释可以Export导出dox文档,常用Javadox标记如下: @author:指定Java程序的作者 @vers ...
- Python获取当前文件路径及父文件路径
import os # 当前文件的路径 1.os.getcwd(): 2.os.path.realpath(__file__) # 当前文件的父路径 1.pwd=os.getcwd() os.pa ...
- html中特殊符号对应表
html中特殊符号的对应表 符号 说明 编码 ‘' 双引号 " & and符 & < 小于号 < > 大于号 > 空格 ...
- js清空子节点
删除全部子节点 function removeAllChild(){ var div = document.getElementById("div1"); while(div.ha ...
- linux修改主机名(hostname)
修改/etc/sysconfig/network文件并重启
- 中国城市区号脚本-mysql
中国城市区号 300个. SET NAMES utf8mb4; ; DROP TABLE IF EXISTS `citycode`; CREATE TABLE `citycode` ( `codeId ...
- HDU1024 Max Sum Plus Plus(dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 #include<iostream> #include<vector> #i ...
- 代码反向生成数据库注释更新sql
原理 通过反射实体所在程序集,得到枚举值列表,再通过sql获取数据库表结构,两者拼接成sql. 规范 实体枚举字段最好也加上Description特性,方便多次更新: 代码 实体定义 public p ...
- url中的参数加密
有时候我们需要在地址栏传输一些信息,比如查询数据的时候,传一个参数location.href = "/admin/extract?name="+"参数aaa"’ ...
- 手机chrome书签文件导出教程
重大发现!!!本人亲自测试可以导出chrome书签文件登录下面的链接https://takeout.google.com/settings/takeout/custom/chrome?pli=1