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 ...
随机推荐
- USACO Section2.1 Ordered Fractions 解题报告
frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- Python 模块:random 随机数生成
Python中的random模块用于生成随机数. 使用该模块之前需要 import random 几个常用的函数用法: 1.random.random 函数原型: random.random() 用于 ...
- 1、python 循环控制
案例1: lucky_num = 19 input_num = int(input("Input the guess number:")) if input_num == luc ...
- winform对图片进行灰度处理
//图片进行灰度处理 //originalImage为原图像 返回灰度图像 private Bitmap GrayImage(Bitmap originalImage) { ImageAttribut ...
- 爬虫:Scrapy8 - Item Pipeline
当 Item 在 Spider 中被收集之后,它将会被传递到 Item Pipeline,一些组件会按照一定的顺序执行对 Item 的处理. 每个 item pipeline 组件(有时也称之为“It ...
- CodeForces Round #515 Div.3 D. Boxes Packing
http://codeforces.com/contest/1066/problem/D Maksim has nn objects and mm boxes, each box has size e ...
- canvas 基础
1.<canvas>元素 <canvas id="tutorial" width="150" height="150"&g ...
- Java分布式数据导出实践
伴随业务发展日益剧增,对数据的要求越来越多也越来越高. 用户在浏览器发起导出请求--web服务器接收请求--请求后台获取数据--数据统计后生成excel或其他图标--响应给客户端 整个过程至少5步,才 ...
- Java操作 Redis 集群
// 连接redis集群 @Test public void testJedisCluster() { JedisPoolConfig config = new JedisPoolConfig(); ...
- 【bzoj3669】[Noi2014]魔法森林 Kruskal+LCT
原文地址:http://www.cnblogs.com/GXZlegend/p/6797748.html 题目描述 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看 ...