贪心 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容器

 

posted @ 2015-04-27 12:46  Running_Time  阅读(159)  评论(0编辑  收藏  举报