当前位置:网站首页>轻重搭配(贪心)
轻重搭配(贪心)
2022-07-17 05:21:00 【winkiii】
描述
n个同学去动物园参观,原本每人都需要买一张门票,但售票处推出了一个优惠活动,一个体重为x的人可以和体重至少为 2x的人配对,这样两人只需买一张票。现在给出了 n个人的体重,请你计算他们最少需要买几张门票?
输入
第一行整数n,表示人数;
第二行有n个整数,表示每个人的体重。
输出
最少需购买的门票数
输入样例 1
6
1 9 7 3 5 5
输出样例 1
4
#include<stdio.h>
int main(){
int n;
scanf("%d",&n); //输入人数
int a[100];
int i;
int flag[100] = {
0};
for(i=0;i<n;i++){
//输入各人体重
scanf("%d",&a[i]);
}
int j,k,t;
for(i=0;i<n;i++){
//升序排序
k = i;
for(j=i+1;j<n;j++){
if(a[j]<a[k]){
k = j;
}
}
if(k!=i){
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
int sum=0;
int p=0;
for(i=n/2;i<n;i++){
//从体重较重后半段开始
if(a[i]>2*a[p]){
flag[p] = 1; //可以搭配2x体重的人
p++;
}
sum++; //两人只需买一张票
}
for(i=0;i<n/2;i++){
if(flag[i]==0){
//找不到搭配的人,需另外再购票
sum++;
}
}
printf("%d\n",sum);
}
边栏推荐
猜你喜欢

Common user password encryption methods and cracking methods
![[jmeter] TCP Sampler](/img/ac/3b08db8808d84eb144262d78880050.png)
[jmeter] TCP Sampler

使用Altium Designer软件绘制一个基于stm32设计

Application case of CS brand sdnand in color detector industry

lock

Arabic numerals to Chinese

剑指offer刷题

STM32串口通信相关学习

Share the application cases of CS brand patch T card in the field of hunting camera

Wu Enda machine learning chapter 6-7
随机推荐
Talk about Zhongtai: my understanding and thinking about Zhongtai
Flip linked list
Double code time scale network diagram
《PyTorch深度学习实践》-B站 刘二大人-day7
Performance comparison between merge into and update in Oracle
Pytorch deep learning practice-b station Liu erden-day4
Slag learning road (2) pure white direction: win Server 2003 server building
Wu Enda machine learning chapter 6-7
C 语言结构体数组指针以及函数
X11 forwarding
️️固高运动控制卡的相关知识点
[jmeter] TCP Sampler
Vcenter6.7安装及排错
Causes and solutions of over fitting
Drawing PCB with Altium Designer
tail -f暂停方法
剑指offer刷题
5-17 hidden student services of Shaanxi University of science and technology
libevent 报 undefined reference to `getrandom‘
ANAME