Codeforces Round #448 (Div. 2)B
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.
自己真的很菜啊
這個題想了好久 看了別人的程式碼才看出來
找到滿足的區間 分別找到與之相鄰的下標相減
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000000+100
#define ll long long
long long a[maxn];
int main(){
ll n,k,x;
scanf("%lld%lld%lld",&n,&x,&k);
for(ll j=0;j<n;j++){
scanf("%lld",&a[j]);
}
sort(a,a+n);
long long ans=0;
for(ll j=0;j<n;j++){
long long z=a[j]/x;
long long l=(z+k)*x;
long long r=l+x;
if(a[j]%x==0){
l=(z+k-1)*x;
r=l+x;
}
l=max(a[j],l);
//cout<<l<<" "<<r<<" ";
l=lower_bound(a,a+n,l)-a;
r=lower_bound(a,a+n,r)-a;
//cout<<l<<" "<<r<<endl;
ans+=r-l;
}
cout<<ans<<endl;
}
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000000+100
#define ll long long
long long a[maxn];
ll binary_search1(ll l,ll r,ll x){
while(l<=r){
int mid=(l+r)/2;
if(a[mid]>=x){
r=mid-1;
}
else l=mid+1;
}
return l;
}
int main(){
ll n,k,x;
scanf("%lld%lld%lld",&n,&x,&k);
for(ll j=0;j<n;j++){
scanf("%lld",&a[j]);
}
sort(a,a+n);
long long ans=0;
for(ll j=0;j<n;j++){
long long z=a[j]%x;
// cout<<a[j]+x-z<<" ";
long long l=a[j]+(x-z)%x+(k-1)*x;
long long r=l+x;
//l=max(a[j],l);
if(!k){
l=a[j];
r=a[j]+(x-z)%x;
}
//cout<<l<<" "<<r<<" ";
l=binary_search1(0,n-1,l);
r=binary_search1(0,n-1,r);
//cout<<l<<" "<<r<<endl;
ans+=r-l;
}
cout<<ans<<endl;
}
相關文章
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #450 (Div. 2) B
- Codeforces Round #251 (Div. 2) A/B/D
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)B
- Codeforces Round #362 (Div. 2) B 模擬
- Codeforces Round #336 (Div. 2) B 暴力
- Codeforces Round #325 (Div. 2) B 遞推
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #245 (Div. 2) B - Balls GameGAM
- Codeforces Round 977 (Div. 2)(B-C2)
- Codeforces Round #491 (Div. 2) B. Getting an A
- Codeforces Round #361 (Div. 2) B BFS最短路
- Codeforces Round #323 (Div. 2) B 模擬
- Codeforces Round #321 (Div. 2) B 二分
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #242 (Div. 2) B. Megacity
- Codeforces Round #249 (Div. 2) B. Pasha Maximizes
- Codeforces Round #247 (Div. 2) B - Shower Line
- Codeforces Round #253 (Div. 2) B - Kolya and Tandem Repeat
- Codeforces Round #252 (Div. 2) B. Valera and FruitsUI
- Codeforces Round #246 (Div. 2) B. Football Kit
- Codeforces Round #244 (Div. 2) B. Prison Transfer
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Codeforces Round #228 (Div. 2) B. Fox and CrossROS
- Codeforces Round #215 (Div. 2) B. Sereja and Suffixes
- Codeforces Round #243 (Div. 2) B. Sereja and Mirroring
- Codeforces Round #196 (Div. 2) B. Routine Problem
- Codeforces Round #251 (Div. 2) B. Devu, the Dumb Guydev
- Codeforces Round #209 (Div. 2) B. Permutation
- Codeforces Round #676 (Div. 2) B. Putting Bricks in the Wall
- Codeforces Round #213 (Div. 2) B. The Fibonacci Segment
- Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's StonesAI
- Codeforces Round #216 (Div. 2) B. Valera and Contest
- Codeforces Round 975 (Div. 2)題解記錄(A,B,E)
- Codeforces Round 976 (Div. 2) and Divide By Zero 9.0(A,B,C)IDE
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)