当前位置:网站首页>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;
}
边栏推荐
- Record of force deduction and question brushing
- [exercise-6] (UVA 725) division = = violence
- Find 3-friendly Integers
- Record of brushing questions with force deduction -- complete knapsack problem (I)
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
- TCP的三次握手与四次挥手
- JS调用摄像头
- nodejs爬虫
- Research Report on market supply and demand and strategy of China's earth drilling industry
- 【练习-11】4 Values whose Sum is 0(和为0的4个值)
猜你喜欢
【练习-5】(Uva 839)Not so Mobile(天平)
PySide6 信号、槽
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
Ball Dropping
Essai de pénétration (1) - - outils nécessaires, navigation
Information security - Analysis of security orchestration automation and response (soar) technology
差分(一维,二维,三维) 蓝桥杯三体攻击
Frida hook so layer, protobuf data analysis
VS2019初步使用
随机推荐
HDU - 6024 Building Shops(女生赛)
SSM框架常用配置文件
用C语言写网页游戏
[exercise-6] (UVA 725) division = = violence
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
快速转 TypeScript 指南
Penetration test (1) -- necessary tools, navigation
Opencv learning log 18 Canny operator
frida hook so层、protobuf 数据解析
【练习4-1】Cake Distribution(分配蛋糕)
China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
Accounting regulations and professional ethics [5]
【高老师UML软件建模基础】20级云班课习题答案合集
信息安全-威胁检测引擎-常见规则引擎底座性能比较
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
Opencv learning log 13 corrosion, expansion, opening and closing operations
The most complete programming language online API document
Opencv learning log 33 Gaussian mean filtering
Matlab comprehensive exercise: application in signal and system
China earth moving machinery market trend report, technical dynamic innovation and market forecast