Codeforces Round #448 (Div. 2)B. XK Segments (双指针 or 二分 )
来源:互联网 发布:linux修改分区大小 编辑:程序博客网 时间:2024/05/16 10:56
题目:
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.
题意:
给你n个数字 然后让你求 第i个数字到第j个数字中恰好有k个膜x等于零的数字这样的ij对有多少个
思路:
#include <iostream>#include <algorithm>using namespace std;const static int N = 1e5 + 10;typedef long long ll;int main(){ll n , k , x, array[N];ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);while (cin >> n >> x >> k) {for (ll i = 0 ; i < n ; i ++) cin >> array[i];sort(array , array + n);ll res = 0;for (ll i = 0 ; i < n ; i ++) {ll temp = (array[i])/ x;if(array[i] % x) {ll l = (temp + k) * x ;ll r = (temp + k + 1) * x - 1;l = max(l , array[i]);//printf("array == %d l == %d R == %d\n",array[i], l, r);l = lower_bound(array , array + n, l) - array;r = upper_bound(array , array + n, r) - array;if (l == n) continue;if (r < l) continue;//if (k == 0) {//int pos = lower_bound(array , array + n , array[i]) - array;//res -= pos - l;//}res += r - l;} else {//printf("This array == %d\n",array[i]);ll l = (temp + k - 1) * x ;ll r = (temp + k) * x - 1;l = max(l , array[i]);//printf("prel == %d prer == %d\n",l , r);l = lower_bound(array , array + n , l ) - array;r = upper_bound(array , array + n , r) - array;if (l == n) continue;if (r < l) continue;//if (k == 0) {//int pos = lower_bound(array , array + n , array[i]) - array;//res -= pos - l;//}//printf("l == %d R == %d \n", l, r);res += r - l;}}cout << res << endl;}}/*2 5 03 4*/
dalao们的二分是这样的:
#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const static int N = 1e5 + 10;int main(){ll n , k , x, array[N];ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);while (cin >> n >> x >> k) {for (ll i = 0 ; i < n ; i ++) cin >> array[i];sort(array , array + n);ll res = 0;for (ll i = 0 ; i < n ; i ++) {ll temp = (array[i] - 1)/ x;ll l = max((temp + k) * x , array[i]);ll r = (temp + k + 1) * x;res += lower_bound(array , array + n , r) - lower_bound(array , array + n , l);}cout << res << endl;}}/*2 5 03 4*/
#include <iostream>#include <algorithm>using namespace std;const static int N = 1e5 + 10;int array[N];int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n , x , k;cin >> n >> x >> k;for (int i = 0; i < n ; i++) cin >> array[i];sort(array , array + n);long long res = 0;int l , r ;l = r = 0;for (int i = 0 ; i < n; i ++) {while (l < n && array[l] < array[i]) l ++;while (r < n && array[r] < array[i]) r ++;while (l < n && array[l] / x - (array[i] - 1) / x < k) l ++;while (r < n && array[r] / x - (array[i] - 1) / x <= k) r ++;res += r - l;}cout << res << endl;}/*4 2 05 3 1 7*/
- Codeforces Round #448 (Div. 2)B. XK Segments (双指针 or 二分 )
- Codeforces Round #448 (Div. 2) B. XK Segments 排序+二分
- Codeforces Round #448 (Div. 2) 895B. XK Segments
- #448 div.2 B.XK Segments
- codeforces 895B. XK Segments (二分搜索)
- CodeForces 895 B. XK Segments 二分查找
- codeforces 895B XK Segments 思维 二分
- codeforces B. XK Segments
- Codeforces Round #403 (Div. 2) B 二分 or 三分
- Codeforces-895B XK Segments
- CodeForces 895B XK Segments
- codeforce 448 B. XK Segments
- B. XK Segments(暴力)
- Codeforces Round #321 (Div. 2) B 二分
- XK Segments(二分)
- cfB. XK Segments(二分)
- Codeforces Round #271 (Div. 2) --B Worms (二分)
- Codeforces Round #367 (Div. 2) B Interesting drink(二分)
- Leetcode 730. Count Different Palindromic Subsequences
- linux学习第三十二篇:linux任务计划cron,chkconfig工具,systemd管理服务,unit介绍,target介绍
- 在Celery中使用scikit-learn
- 关于适配所有移动端屏幕且到固定宽度时居中的写法
- CentOS 6.8 安装配置 vsftpd 文件服务器
- Codeforces Round #448 (Div. 2)B. XK Segments (双指针 or 二分 )
- myeclipse下hadoop安装并运行
- 51Nod 1347 旋转字符串
- 关于单选框、下拉框、复选框的数据回显问题以及全选和全不选
- WebSocket 教程
- 蓝桥杯 算法提高 12-1三角形 (Java解题)
- 你必须知道的ABI和CPU关系
- Python高效编程的19个技巧
- angular中的ng-content