Algorithm
This commit is contained in:
19
Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 set不过.cpp
Normal file
19
Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 set不过.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
multiset<int> s;
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
int tmp;
|
||||
for(int i = 0; i < n; i++){
|
||||
cin >> tmp;
|
||||
s.emplace(tmp);
|
||||
}
|
||||
int js = 0;
|
||||
for(auto it = s.begin(); it != s.end(); it++){
|
||||
js += s.count(*it - m);
|
||||
}
|
||||
cout << js << endl;
|
||||
return 0;
|
||||
}
|
26
Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 unordered_map.cpp
Normal file
26
Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 unordered_map.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
//<2F><> hash + map <20><>ԭ<EFBFBD><D4AD><EFBFBD>Dz<EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> O(n^2)
|
||||
using namespace std;
|
||||
int main(){
|
||||
unordered_map<long long, int> s;
|
||||
vector<long long> arr;
|
||||
long long n, m;
|
||||
cin >> n >> m;
|
||||
long long tmp;
|
||||
for(int i = 0; i < n; i++){
|
||||
cin >> tmp;
|
||||
arr.push_back(tmp);
|
||||
s[tmp]++;
|
||||
}
|
||||
long long need;
|
||||
long long js = 0;
|
||||
for(int i = 0; i < n; i++){
|
||||
need = arr[i] - m;
|
||||
if(s.count(need)) js += s[need];//s[need]<5D><>ֵ<EFBFBD><D6B5> pair.seconds
|
||||
//s.emplace(make_pair(arr[i], 1));//<2F><><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>arr[i]<5D><><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
cout << js << endl;
|
||||
return 0;
|
||||
}
|
BIN
Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 unordered_map.exe
Normal file
BIN
Algorithm/Maths/P1102 A-B数对/P1102 A-B 数对 unordered_map.exe
Normal file
Binary file not shown.
@@ -0,0 +1,19 @@
|
||||
#include <unordered_set>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
unordered_multiset<int> s;
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
int tmp;
|
||||
for(int i = 0; i < n; i++){
|
||||
cin >> tmp;
|
||||
s.emplace(tmp);
|
||||
}
|
||||
int js = 0;
|
||||
for(auto it = s.begin(); it != s.end(); it++){
|
||||
js += s.count(*it - m);
|
||||
}
|
||||
cout << js << endl;
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user