当前位置:网站首页>Luogu P1102 A-B number pair (dichotomy, map, double pointer)
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
2022-07-06 16:03:00 【It's Xiao Zhang, ZSY】
Simple A-B
describe
This is a simple question , Give a string of numbers and a number C, Ask to calculate all A - B = C The number of pairs of ( The same number pairs in different positions count different number pairs ).
Input
There are two lines of input .
first line , Two integers N, C.1=<N<=2e5 , C>=1
The second line ,N It's an integer , As the number of strings required to be processed .
Output
a line , Indicates that the number contained in the string satisfies A - B =C The number of pairs of .
sample input 1
4 1
1 1 2 3
sample output 1
3
sample input 2
6 1
1 1 1 2 2 2
sample output 2
9
It is worth noting that , May be violent int
such as
Input
200000 1
1e5 individual 1,1e5 individual 2
Output
1e10 > int type
(int The maximum number of deposits is about 2e9)
Two points
Simple dichotomous template question
Prioritize , Find out bidi i It's a big number C Coordinates of the leftmost endpoint and the rightmost endpoint of ,( Right endpoint coordinates minus left endpoint coordinates plus 1) It means how many are better than i It's a big number C
Examples 2 in
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const N=2e5+10;
int n,c,a[N];
ll res;
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// save cin,cout Time ;
cin>>n>>c;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
int l=i+1,r=n;
while(l<r) // Find the leftmost endpoint ;r=mid;
{
int mid=l+r >>1;
// amount to (l+r)/2, It looks a little more powerful hh,+ Priority is greater than >>( Move right ) priority ;
if(a[mid]-a[i]>=c)
r=mid;
else
l=mid+1;
}
int k=l;
l=i+1,r=n;
while(l<r)// Find the rightmost endpoint ;l=mid;
{
int mid=l+r+1 >> 1;// Change l, want +1, Prevent a dead cycle
if(a[mid]-a[i]<=c)
l=mid;
else
r=mid-1;
}
int kk=l;
if(a[kk]-a[i]==c&&a[k]-a[i]==c)
res+=kk-k+1;
}
cout<<res<<endl;
return 0;
}
lower_bound()
upper_bound()
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const N=2e5+10;
int n,c,a[N],b[N];
ll res;
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// save cin,cout Time ;
cin>>n>>c;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=a[i]+c;// use b[N] Array storage ( Corresponding ) The big C Value
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
int k=lower_bound(a+1,a+n+1,b[i])-a;// Returns the first greater than or equal to b[i] Of a Index of the array
int kk=upper_bound(a+1,a+n+1,b[i])-a;// Return is greater than the
res+=kk-k;// because upper_bound Returns a subscript greater than , So there's no need to add 1
}
cout<<res<<endl;
return 0;
}
Double pointer
The concrete realization is , We maintain two endpoints kk , k, Every time kk Move right to a[kk] - a[i] <= c
Next to the last position of ,k Move right to meet a[k] - a[l] < c
Last .
in other words , If at this time a[kk] - a[i] == c && a[k] - a[i] == c
, The middle paragraph must meet the conditions , We let res += kk - k
that will do .
#include <bits/stdc++.h>
using namespace std;
int const N=200010;
typedef long long ll;
int a[N],n,c;
ll res;
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// save cin,cout Time ;
cin>>n>>c;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n);
int kk=2,k=2;
for(int i=1;i<=n;i++)
{
while(kk<=n&&a[kk]-a[i]<=c)
kk++; // Because it's orderly , Next time look for kk Start ; Reduce unnecessary enumeration
while(k<=n&&a[k]-a[i]<c)
k++;
if(a[kk-1]-a[i]==c&&a[k]-a[i]==c)
res+=kk-k;
}
cout<<res<<endl;
return 0;
}
MAP Time complexity O(n)
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map> //map The header file ;
using namespace std;
typedef long long ll;
int const N=2e5+10;
int n,c,a[N],b[N];
ll res;
map<int,int> mp;//key-value; Establish a number to occurrence mapping map<a[i],times>
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// save cin,cout Time ;
cin>>n>>c;
for(int i=1;i<=n;i++)
{
cin>>a[i];
mp[a[i]]++;
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
res+=mp[a[i]+c];
}
cout<<res<<endl;
return 0;
}
边栏推荐
- Market trend report, technical innovation and market forecast of geosynthetic clay liner in China
- Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
- 0-1 knapsack problem (I)
- 最全编程语言在线 API 文档
- Matlab comprehensive exercise: application in signal and system
- Ball Dropping
- 基于web的照片数码冲印网站
- [exercise 4-1] cake distribution
- Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
- C语言必背代码大全
猜你喜欢
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
X-Forwarded-For详解、如何获取到客户端IP
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Penetration test (3) -- Metasploit framework (MSF)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
Analysis of protobuf format of real-time barrage and historical barrage at station B
滲透測試 ( 1 ) --- 必備 工具、導航
Borg maze (bfs+ minimum spanning tree) (problem solving report)
【练习-5】(Uva 839)Not so Mobile(天平)
STM32 learning record: LED light flashes (register version)
随机推荐
编程到底难在哪里?
Research Report on market supply and demand and strategy of China's earth drilling industry
Opencv learning log 33 Gaussian mean filtering
对iptables进行常规操作
Determine the Photo Position
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
【练习-7】Crossword Answers
Information security - threat detection engine - common rule engine base performance comparison
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
Penetration test (1) -- necessary tools, navigation
If you want to apply for a programmer, your resume should be written like this [essence summary]
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
【练习-5】(Uva 839)Not so Mobile(天平)
洛谷P1102 A-B数对(二分,map,双指针)
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Accounting regulations and professional ethics [4]
Matlab comprehensive exercise: application in signal and system
TCP的三次握手与四次挥手
Truck History
Shell Scripting