CodeForces 372 A. Counting Kangaroos is Fun
题意,有n只袋鼠,没每只袋鼠有个袋子,大小为si,一个袋鼠可以进入另外一个袋鼠的袋子里面,当且仅当另一个袋鼠的袋子是他的二倍或二倍一上,然后中国袋鼠就是不可见的,不能出现多个袋鼠嵌套的情况。让你求最少可见袋鼠的数量。
解题方法是先排序再贪心,贪心策略是从中间开始贪心。
#include <stdio.h>
#include <algorithm>
const int maxn = 500005;
using namespace std;
int s[maxn];
int n;
int main()
{
while (scanf("%d", &n) != EOF)
{
for (int i = 1; i <= n; i++)
{
scanf("%d", &s[i]);
}
sort(s+1, s+n+1);
int j = n/2 + 1;
int ans = 0;
for (int i = 1; i <= n>>1 && j <= n;)
{
if (s[j] >= 2*s[i])
{
ans++;
i++;
j++;
}
else
{
j++;
}
}
printf("%d\n", n-ans);
}
return 0;
}

浙公网安备 33010602011771号