Codeforces Round #448 (Div. 2) B. XK Segments【二分搜索/排序/查找合法的数在哪些不同区间的区间数目】
1 second
256 megabytes
standard input
standard output
While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j)such that ai ≤ aj and there are exactly k integers y such that ai ≤ y ≤ aj and y is divisible byx.
In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1).
The first line contains 3 integers n, x, k (1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), where n is the size of the array a and x and k are numbers from the statement.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
Print one integer — the answer to the problem.
4 2 1
1 3 5 7
3
4 2 0
5 3 1 7
4
5 3 1
3 3 3 3 3
25
In first sample there are only three suitable pairs of indexes — (1, 2), (2, 3), (3, 4).
In second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4).
In third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25.
【题意】:find the number of different ordered pairs of indexes (i, j) such that ai ≤ aj and there are exactly k integers y such that ai ≤ y ≤ aj and y is divisible by x.
y is divisible by x.(y被x整除)
比如12/4=3,我们就可以说12被4整除,4整除12
A可被B整除,表示A是B的倍数
A可整除B,表示B是A 的倍数
输入 n, x, k an
找到不同有序下标对(i,j) 满足ai ≤ aj 并且刚好有k个整数 y( ai ≤ y ≤ aj ),y%x==0的数量。
【分析】:二分求上下界,紫薯227页。编码实现不是很难。但是我不是特别懂,希望哪位大佬不吝赐教!右边这个博客解释的比较好,https://www.cnblogs.com/lemonbiscuit/archive/2017/11/27/7903860.html
虽然是连续区间,因为仅仅是求得合法二元组个数,而不是求具体是哪些。将其排序不影响最终结果。
//(官方)First, we need to understand how to find the number of integers in [l, r] segment which are divisible byx. It is r / x–(l - 1) / x. After that we should sort array in ascending order. For each left boundary of the segment l = a[i] we need to find minimal and maximal index of good right boundaries. All right boundaries r = a[j] should satisfy the following condition a[j] / x–(a[i] - 1) / x = k. We already know (a[i] - 1) / x, a[j] / x is increasing while a[j] increases. So we can do binary search on sorted array to find minimal/maximal index of good right boundaries and that mean we can find the number of good right boundaries.
一些大佬的解释:假设现在a[i]是x的倍数的话,设a[i] = t*x,那么要[a[i],a[j]]中有k个x的倍数的话,a[j]的范围就是[(t+k-1)*x,(t+k)*x-1],如果a[i]不是x的倍数的话,设a[i] = t*x + b,那么现在a[j]的范围就是[(t+k)*x,(t+k+1)*x-1],然后用二分找这个范围里的数就好啦。
排序后,选定一个数ai,我们可以根据这个数,和 k 个x的倍数的限制,算出大于等于ai的数的满足条件的范围;比如第一个样例选中了 ai = 3,这是x=2,k=1, (ai/x)就是小于等于ai的x的倍数的个数,aj =(ai/x)*x + x*k 就是满足 i-j 对之间恰好含有k个x的倍数的 一个最小值,当 aj在加上x后,ai - aj 之间就有 k+1个x的倍数了,我们要找的就是给定的数列中 aj 和 (aj+x) 之间的数的个数,这样就求出每个数和大于等于他的数组成的合法对的个数。
先把所有数字升序排序,之后枚举每对点对(i,j)中的 j 。对于一个固定的 j ,满足条件的 i 肯定在已经排序好的数组当中构成了一段连续的区间,且Ai<=Aj。设这段下标区间对应的元素范围为[l,r],则
l=(Ai/x-k)*x+1
r=l+k-1
用stl的lower_bound和upper_bound找到 l r 对应的下标位置即可。需要特别注意k=0的情况,此时 l r是不满足上述公式的,需要特判。
【代码】:
#include <bits/stdc++.h>
using namespace std;
const int maxn =1e5+;
#define LL long long
LL a[maxn]; int main()
{
ios::sync_with_stdio(false);
cin.tie();cout.tie();
LL n,x,k,cnt;
cin>>n>>x>>k;
for(LL i=; i<n; i++) cin>>a[i];
sort(a,a+n);
cnt=; for(LL i=;i<n;i++)
{
LL left=max( ( (a[i]-)/x + k )*x, a[i] );
LL right=( (a[i]-)/x + k + )*x; cnt+=lower_bound(a,a+n,right)-lower_bound(a,a+n,left);
}
cout<<cnt<<endl;
return ;
}
LL 防止爆int
#include <iostream>
#include <algorithm>
#include <vector> using namespace std; int main()
{
int n, x, k;
cin >> n >> x >> k;
vector<int> a(n); for (int i = ; i < n; i++)
cin >> a[i]; sort(a.begin(), a.end()); long long cnt = ; for (int i = ; i < n; i++) {
vector<int>::iterator l = lower_bound(a.begin(), a.end(), max((long long)a[i], (long long)x * (k + (a[i] - ) / x)));
vector<int>::iterator r = lower_bound(a.begin(), a.end(), max((long long)a[i], (long long)x * (k + + (a[i] - ) / x))); cnt += r - l;
} cout << cnt;
}
cf一位大佬
Codeforces Round #448 (Div. 2) B. XK Segments【二分搜索/排序/查找合法的数在哪些不同区间的区间数目】的更多相关文章
- Codeforces Round #452 (Div. 2) 899E E. Segments Removal
题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...
- Codeforces Round #448(Div.2) Editorial ABC
被B的0的情况从头卡到尾.导致没看C,心情炸裂又掉分了. A. Pizza Separation time limit per test 1 second memory limit per test ...
- Codeforces Round #672 (Div. 2) D. Rescue Nibel!(排序)
题目链接:https://codeforces.com/contest/1420/problem/D 前言 之前写过这场比赛的题解,不过感觉这一题还可以再单独拿出来好好捋一下思路. 题意 给出 $n$ ...
- Codeforces Round #448 (Div. 2) B
题目描述有点小坑,ij其实是没有先后的 并且y并不一定存在于a中 判断y的个数和所给数组无关 对于2 - 7来说 中间满足%2==0的y一共有3个 2 4 6 这样 可以看出对于每个数字a 都能够二分 ...
- Codeforces Round #448 (Div. 2)C. Square Subsets
可以用状压dp,也可以用线型基,但是状压dp没看台懂... 线型基的重要性质 性质一:最高位1的位置互不相同 性质二:任意一个可以用这些向量组合出的向量x,组合方式唯一 性质三:线性基的任意一个子集异 ...
- Codeforces Round #448 (Div. 2) A. Pizza Separation【前缀和/枚举/将圆(披萨)分为连续的两块使其差最小】
A. Pizza Separation time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【Codeforces Round #455 (Div. 2) B】Segments
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ...
- Codeforces Round #595 (Div. 3) D2Too Many Segments,线段树
题意:给n个线段,每个线段会覆盖一些点,求删最少的线段,使得每个点覆盖的线段不超过k条. 思路:按右端点排序,之后依次加入每个线段,查询线段覆盖区间内的每个点,覆盖的最大线段数量,如果不超过k,那就可 ...
- Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
A. Mike and palindrome time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
随机推荐
- java中多态的概念
概念: 简答来说,多态(Polymorphism)是具有表现多种形态的能力的特征. 更专业的说法是:同一个实现接口,使用不同的实例而执行不同的操作. 好处: 通过多态可以减少类中代码量,可以提高 ...
- Javascript在浏览器中的加载顺序详解!
现在前端用javascript用的比较多,当然真心的说这个语言是一个非常业余的语言,但是用的人很多,所以也比较火.今天想完成一个javascript外部文件自动加载的设计(类似于java或者php的i ...
- iphone 8 plus 红色特别版,突然自动关机无法启动
今天早上我的iphone 8p 突然自己在床上闪动开机图标,闪了半个多小时它就光荣的自动关机了,我尝试了长按开机键,长按home+开机键15秒,通通木有用,它就是没!反!应! 于是找了售后,学到了正确 ...
- Java基础-5运算符
一).算数运算符: 算术运算符的功能是做各种算术运算,其操作数可以是字符型.整型或浮点型数据. 运算符 运算 示例 结果 备注 + 加 5+5 10 - 减 4-2 2 * 乘 2*3 6 既 ...
- JavaScript中最常用的55个经典技巧,没事的时候看看,拓展解决问题的思路
都转烂了,不过还是贴上来了.查的时候方便... test 1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 & ...
- operator、explicit与implicit
说这个之前先说下什么叫隐式转换和显示转换 1.所谓隐式转换,就是系统默认的转换,其本质是小存储容量数据类型自动转换为大存储容量数据类型. 例如:float f = 1.0: double d=f:这样 ...
- ASP.NET Core 认证与授权[2]:Cookie认证 (笔记)
原文链接:https://www.cnblogs.com/RainingNight/p/cookie-authentication-in-asp-net-core.html 由于HTTP协议是无状态的 ...
- Python全栈 MySQL 数据库 (简述 、安装、基本命令)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 开局一张图 一个月的python已经结束了 下面就是数据库了 先说M ...
- EasyUi DataGrid 请求Url两次问题
easyui datagrid 1.4 当total为0时,请求两次url问题 框架问题:需要在easyui文件后加修补补丁 /** * The Patch for jQuery EasyUI 1.4 ...
- 架构-UML类图
在UML 2.0的13种图形中,类图是使用频率最高的UML图之一.Martin Fowler在其著作<UML Distilled: A Brief Guide to the Standard O ...