算法入门-排序算法

BGN25 小红书推荐系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// Created by XIAOBAI on 2026/5/7.
//

#include <iostream>
#include<unordered_map>
#include<set>
#include<string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <bitset>
#include <unordered_set>
#include <map>
using namespace std;
//小红书推荐系统
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
using ll = long long;
using ull = unsigned long long;

bool cmp(int a,int b) {
return a>b;
}
bool cmp1(pair<string,int> a,pair<string,int> b) {
if (a.second!=b.second) return a.second>b.second;
return a.first<b.first;
}

void solve() {
map<string,int> a;
vector<pair<string,int>> v;
string s;
while(cin>>s) {
a[s]++;
}
for (auto& i:a) {
v.push_back(i);
}
sort(v.begin(),v.end(),cmp1);
for (auto i : v) {
if (i.second >=3) {
cout<<i.first<<'\n';
}
}
}


int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T=1;
//cin>>T;
while(T--) {
solve();
}

}

BGN27 谐距下标对

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// Created by XIAOBAI on 2026/5/7.
//
//
// Created by XIAOBAI on 2026/5/7.
//

#include <iostream>
#include<unordered_map>
#include<set>
#include<string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <bitset>
#include <unordered_set>
#include <map>
using namespace std;
//谐距下标对
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
using ll = long long;
using ull = unsigned long long;
bool cmp(int a,int b) {
return a>b;
}

bool cmp1(pair<string,int> a,pair<string,int> b) {
if (a.second!=b.second) return a.second>b.second;
return a.first<b.first;
}

void solve() {
unordered_map<int,int> a;
ll n,ans=0;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++) {
cin>>v[i];
if (!a[v[i]])a[v[i]]=1;
else {ans+=a[v[i]];a[v[i]]++;}
}
cout<<ans;


}


int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T=1;
//cin>>T;
while(T--) {
solve();
}

}