[51NOD1420] 数袋鼠好有趣(贪心)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1420

 

由于最少的袋鼠数量也就是n/2取上整,那么可以排序,然后从[1,n/2] [n/2+1,n]两个部分中匹配。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 500500;
 5 int n, s[maxn];
 6 
 7 int main() {
 8     // freopen("in", "r", stdin);
 9     while(~scanf("%d", &n)) {
10         for(int i = 1; i <= n; i++) scanf("%d", &s[i]);
11         sort(s+1, s+n+1);
12         int ret = 0;
13         int mid = n / 2;
14         for(int i = n; i >= mid; i--) {
15             while(s[i] / 2 < s[mid] && mid >= 1) mid--;
16             if(mid <= 0) break;
17             ret++; mid--;
18         }
19         printf("%d\n", n - ret);
20     }
21     return 0;
22 }

 

posted @ 2017-05-16 16:38  Kirai  阅读(146)  评论(0编辑  收藏  举报