当前位置:网站首页>【CF #277.5 (Div. 2)】B. BerSU Ball
【CF #277.5 (Div. 2)】B. BerSU Ball
2022-06-11 07:10:00 【percation】
The question , Find the elements in two arrays , Is there an absolute value that does not exceed 1.( If exist , Then form a pair , And cannot be paired with other elements )
Find out how many pairs can be matched .
Ideas : The ability to represent the dancing skills of boys and girls , Sort .
Due to the small data range , So we used O ( n 2 ) O(n^2) O(n2) Time complexity of
You can also use the double pointer method for this problem , Let's do some optimization .
O ( n 2 ) O(n^2) O(n2) Methods , Double cycle
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n,t,m;
int a[N],b[N],c[N];
bool vis[N];
int ans;
int main(){
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
sort(a,a + n);
cin >> m;
for(int j = 0; j < m; j++) cin >> b[j];
sort(b, b + m);
for(int j = 0; j < m; j++){
for(int i = 0 ; i < n; i++){
if(abs(a[i] - b[j]) <= 1 && !vis[i]){
ans++;
vis[i] = 1;
break;
}
}
}
cout << ans << endl;
return 0;
}
Double finger needling
Ideas :
Sort two arrays
Two pointers point to the first element of two arrays respectively .
If the absolute value of the element that these two pointers currently point to is less than or equal to 1, be ans++,
otherwise , Compare the size of two pointers pointing to elements , To determine which pointer moves toward the end of the array
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n,t,m;
int a[N],b[N],c[N];
bool vis[N];
int ans;
int main(){
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
sort(a,a + n);
cin >> m;
for(int j = 0; j < m; j++) cin >> b[j];
sort(b, b + m);
for(int i = 0, j = 0; i < n && j < m;){
if(abs(a[i]-b[j]) <= 1){
i++;
j++;
ans++;
}
else if(a[i] < b[j]){
i++;
}
else if(a[i] > b[j]){
j++;
}
}
cout << ans << endl;
return 0;
}
边栏推荐
- Web API、DOM
- The gap between the parent box and the child box
- Deep Attentive Tracking via Reciprocative Learning
- LEARNING TARGET-ORIENTED DUAL ATTENTION FOR ROBUST RGB-T TRACKING
- Practice: how to reasonably design functions to solve practical problems in software development (I)
- 品牌定位个性六种形态及结论的重大意义
- Leetcode hot topic 100 topic 6-10 solution
- Cv2.rectangle() picture frame
- P5431 [template] multiplicative inverse 2
- Grayscale publishing through ingress
猜你喜欢
![[Xunwei dry goods] opencv test of Godson 2k1000 development board](/img/94/312bb1f0d5e8d49506f659ad23cd3a.jpg)
[Xunwei dry goods] opencv test of Godson 2k1000 development board

Heartless sword Chinese English bilingual poem 001 Love

Leetcode-104. Maximum Depth of Binary Tree

Leetcode-104. Maximum Depth of Binary Tree

Leetcode-141. Linked List Cycle

First day of database

Whether the ZABBIX monitoring host is online

【LeetCode】-- 17.电话号码的字母组合

webserver

. Net C Foundation (6): namespace - scope with name
随机推荐
[deploy private warehouse based on harbor] 3 deploy harbor
Latex various arrows / arrows with text labels / variable length arrows
Analysis of key points and difficulties of ES6 promise source code
Records how cookies are carried in cross domain requests
【迅为干货】龙芯2k1000开发板opencv 测试
1269. number of options left in place
**Count the characters with the largest number of words**
Grayscale publishing through ingress
Leetcode-9.Palindrome Numbber
Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
P3811 [template] multiplicative inverse
Promises/a+ standard Chinese Translation
Leetcode hot topic 100 topic 21-25 solution
1190. invert the substring between each pair of parentheses
Set center alignment
webserver
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
Library management system 2- demand analysis
Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C
RGB-D Salient Object Detection withCross-Modality Modulation and Selection