贪心 BestCoder Round #39 1001 Delete
生活随笔
收集整理的這篇文章主要介紹了
贪心 BestCoder Round #39 1001 Delete
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
題目傳送門
1 /* 2 貪心水題:找出出現次數>1的次數和res,如果要減去的比res小,那么總的不同的數字tot不會少; 3 否則再在tot里減去多余的即為答案 4 用set容器也可以做,思路一樣 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <algorithm> 11 using namespace std; 12 13 const int MAXN = 1e4 + 10; 14 const int INF = 0x3f3f3f3f; 15 int cnt[110]; 16 17 int main(void) //BestCoder Round #39 1001 Delete 18 { 19 //freopen ("1001.in", "r", stdin); 20 21 int n; 22 while (scanf ("%d", &n) == 1) 23 { 24 int k; 25 memset (cnt, 0, sizeof (cnt)); 26 27 int tot = 0, res = 0, x; 28 for (int i=1; i<=n; ++i) 29 { 30 scanf ("%d", &x); 31 if (cnt[x] == 0) tot++; 32 else if (cnt[x] >= 1) res++; 33 cnt[x]++; 34 } 35 36 scanf ("%d", &k); 37 if (res >= k) printf ("%d\n", tot); 38 else printf ("%d\n", tot - (k - res)); 39 } 40 41 return 0; 42 } 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <set> 7 using namespace std; 8 9 int main(void) //BestCoder Round #39 1001 Delete 10 { 11 //freopen ("1001.in", "r", stdin); 12 13 set<int> S; 14 int n, k; 15 16 while (cin >> n) 17 { 18 S.clear (); 19 int x; 20 for (int i=1; i<=n; ++i) 21 { 22 cin >> x; S.insert (x); 23 } 24 25 cin >> k; 26 cout << ((n-S.size () <= k) ? n - k : S.size ()) << endl; 27 } 28 29 return 0; 30 } 使用set容器?
轉載于:https://www.cnblogs.com/Running-Time/p/4459855.html
總結
以上是生活随笔為你收集整理的贪心 BestCoder Round #39 1001 Delete的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序一旦发觉写得不理想,那就得重构它
- 下一篇: Unix/Linux环境C编程入门教程(