1. 程式人生 > >CF1061B Views Matter

CF1061B Views Matter

思路:

貪心。

實現:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a[100005];
 4 int main()
 5 {
 6     int n, m;
 7     while (cin >> n >> m)
 8     {
 9         for (int i = 1; i <= n; i++) cin >> a[i];
10         sort(a + 1, a + n + 1);
11         long long ans = 0;
12 int maxn = a[1], cnt = a[1] - 1; 13 for (int i = 2; i <= n; i++) 14 { 15 if (a[i] == a[i - 1]) 16 { 17 if (cnt) { ans += a[i]; cnt--; } 18 else ans += a[i] - 1; 19 } 20 else 21 {
22 cnt += a[i] - maxn - 1; 23 ans += maxn; 24 maxn = a[i]; 25 } 26 } 27 cout << ans << endl; 28 } 29 return 0; 30 }