轻重搭配链接
n 个同学去动物园参观,原本每人都需要买一张门票,但售票处推出了一个优惠活动,一个体重为 x 的人可以和体重至少为 2x 配对,这样两人只需买一张票。现在给出了 n 个人的体重,请你计算他们最少需要买几张门票?
样例输入
6
1 9 7 3 5 5
样例输出
4
题目分析—贪心
输入数据先从小到大排序
sum=n
选取中间点的坐标pos与第1个点比较,如果不满足条件 pos++ 满足sum-- ,pos++
直到比较完所有点
AC代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int mmax=1e6;
int a[mmax];
int main()
{
int n,sum;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int pos=n/2;sum=n;
for(int i=0;i<n/2;i++)
{
while(a[i]*2>a[pos]&&pos<n)
pos++;
if(pos==n)
break;
if(a[i]*2<=a[pos])
{
sum--;
pos++;
}
}
printf("%d\n",sum);
return 0;
}
```