#include <bits/stdc++.h> #include<algorithm> #include <math.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pi; const ll maxn = 2e5 + 100; const ll mod = 1e9 + 7; const ll inf = 1e9; #define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define pb push_back #define pii pair<int, pair<int, int>> #define mp make_pair #define f first #define s second #define all(x) (x).begin(), (x).end() int main() { fast_io; int n, k; cin >> n >> k; vector <int> l; for (int i = 0; i < n; i++) { int x; cin >> x; l.pb(x); } sort(l.begin(), l.end()); int ans = k; int K = k; if (k >= n) { cout << n; } else { while (K > 1) { l.pop_back(); K -= 1; } int val = l.back(); l.pop_back(); while (!l.empty()) { if (l.back() == val) { ans += 1; l.pop_back(); } else { l.clear(); } } cout << ans; } }
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 | #include <bits/stdc++.h> #include<algorithm> #include <math.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pi; const ll maxn = 2e5 + 100; const ll mod = 1e9 + 7; const ll inf = 1e9; #define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define pb push_back #define pii pair<int, pair<int, int>> #define mp make_pair #define f first #define s second #define all(x) (x).begin(), (x).end() int main() { fast_io; int n, k; cin >> n >> k; vector <int> l; for (int i = 0; i < n; i++) { int x; cin >> x; l.pb(x); } sort(l.begin(), l.end()); int ans = k; int K = k; if (k >= n) { cout << n; } else { while (K > 1) { l.pop_back(); K -= 1; } int val = l.back(); l.pop_back(); while (!l.empty()) { if (l.back() == val) { ans += 1; l.pop_back(); } else { l.clear(); } } cout << ans; } } |