Codeforces 895.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 by x.
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.
大致题意:求有多少对(i,j),使得a[i]到a[j]之间x的倍数恰好有k个,a[i] ≤ a[j].
分析:我的做法是比较套路的二分加速枚举区间.
先将a数组排序,这样就消除了a[i] ≤ a[j]这个限制,然后枚举左端点,在左端点右边进行二分.二分出倍数正好为k个的下界l和上界r,接下来的统计有点麻烦.如果二分得到的区间正好包含了i,那么答案要-1,因为(i,i)不能被统计两次.接着,还要求出一个区间[i,p],使得这个区间中的所有数都等于a[i].如果这个区间中的每一个数和a[i]都满足要求,那么(i,j)和(j,i)实质上是一样的,答案要*2,剩下的只需要加上区间长度就好了.
这种写法比较繁琐,一个比较好的处理方法是二分的不仅仅局限于i右边的区间,而是被包含整个区间的区间.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; ll n, x, k, a[], ans; ll solve(ll p, ll q)
{
return a[q] / x - (a[p] - ) / x;
} int main()
{
cin >> n >> x >> k;
for (ll i = ; i <= n; i++)
cin >> a[i];
sort(a + , a + + n); for (ll i = ; i <= n; i++)
{
ll l = i, r = n, ans1 = i;
while (l <= r)
{
ll mid = (l + r) >> ;
if (solve(i, mid) >= k)
{
ans1 = mid;
r = mid - ;
}
else
l = mid + ;
}
ll ans2 = n;
l = i, r = n;
while (l <= r)
{
ll mid = (l + r) >> ;
if (solve(i, mid) <= k)
{
ans2 = mid;
l = mid + ;
}
else
r = mid - ;
}
l = i, r = n;
ll ans3 = i;
while (l <= r)
{
ll mid = (l + r) >> ;
if (a[mid] > a[i])
r = mid - ;
if (a[i] == a[mid])
{
l = mid + ;
ans3 = mid;
}
}
if (solve(i, ans1) == k && solve(i, ans2) == k)
{
if (ans3 < ans1)
ans += (ans2 - ans1 + );
else
{
ans += (ans3 - ans1 + ) * ;
ans += (ans2 - ans3);
if (ans1 == i)
ans--;
}
}
}
cout << ans << endl;
return ;
}
Codeforces 895.B XK Segments的更多相关文章
- CodeForces:#448 div2 B. XK Segments
传送门:http://codeforces.com/contest/895/problem/B B. XK Segments time limit per test1 second memory li ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces Round #448 (Div. 2) B. XK Segments【二分搜索/排序/查找合法的数在哪些不同区间的区间数目】
B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- Codeforces 193 D. Two Segments
http://codeforces.com/contest/193/problem/D 题意: 给一个1~n的排列,在这个排列中选出两段区间,求使选出的元素排序后构成公差为1的等差数列的方案数. 换个 ...
- Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
- Codeforces 895.E Eyes Closed
E. Eyes Closed time limit per test 2.5 seconds memory limit per test 256 megabytes input standard in ...
- 【CodeForces】899 E. Segments Removal
[题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...
- 『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
随机推荐
- Centos下安装并设置nginx开机自启动
一.在centos环境下安装下载并安装nginx,由于nginx需要依赖一些环境才能安装,主要依赖g++.gcc.openssl-devel.pcre-devel和zlib-devel这些环境,首先得 ...
- 分布式数据库中间件Mycat百亿级数据存储(转)
此文转自: https://www.jianshu.com/p/9f1347ef75dd 2013年阿里的Cobar在社区使用过程中发现存在一些比较严重的问题,如高并发下的假死,心跳连接的故障,只实现 ...
- 常用 php server
php编程中经常需要用到一些服务器的一些资料,我把常用的用高亮的方式贴出来,其余的放在后面.方便以后查阅 复制代码代码如下: $_SERVER['HTTP_ACCEPT_LANGUAGE']/ ...
- CSS布局之圣杯布局和双飞翼布局
其实圣杯布局和双飞翼布局实现的都是三栏布局,两边的盒子宽度固定,中间盒子自适应,也就是我们常说的固比固布局.它们实现的效果都是一样的,差别在于其实现的思想. 一.圣杯布局 html代码中,将重要的内容 ...
- Scrum立会报告+燃尽图(十月二十八日总第十九次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2288 项目地址:https://git.coding.net/zhang ...
- 02慕课网《进击Node.js基础(一)》——CommonJs标准
是一套规范管理模块 每个js 为一个模块,多个模块作为一个包 node.js和Couchdb是对其的实现: 不同于jQuery 模块:定义.标识.引用(地址/模块名称) 模块类型: 核心模块http ...
- [转]精选!15个必备的VSCode插件
Visual Studio Code 是由微软开发的一款免费.跨平台的文本编辑器.由于其卓越的性能和丰富的功能,它很快就受到了大家的喜爱. 就像大多数 IDE 一样,VSCode 也有一个扩展和主题市 ...
- tensorboard入门
Tensorboard tensorboard用以图形化展示我们的代码结构和图形化训练误差等,辅助优化程序 tensorboard实际上是tensorflow机器学习框架下的一个工具,需要先安装ten ...
- P4 Runtime和p4 info
p4runtime P4 Runtime是一套基于Protobuf以及gRPC框架上的协议,通过P4runtime,SDN控制器可以控制能够支援p4的设备. p4runtime当前由p4 API wo ...
- web登录密码加密
文章:如何实现登录页面密码加密 文章:用RSA加密实现Web登录密码加密传输 文章:web登录用户名密码加密 知乎文章:Web前端密码加密是否有意义? 文章:记录一次黑客模拟攻击 成功拿到淘宝账号和密 ...