Codeforces #158B Taxi

一. 问题描述

  After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?

二. 问题分析

  我们可以开一个数组利用类似桶的方法将有多少个1,2,3,4放入桶中同时对桶中数的个数进行计数,随后我们对于各数字有多少具体分析,采用贪心策略对问题分析有:

  对于4:每有一个4都可以使总数直接加一;

  对于3:3应先与1配对再根据3与1数量的多少具体分析;

  对于2:偶数:直接除2;

               奇数:除2所得余数与1配对;

  对于1:满足以上条件后直接自行配对;

三. 自身遇到小问题

  在用数组作为函数参数使应用类似 (int ch[]) ,主函数中调用时用 sth(ch),类似

do_it(char ch[]){
  ......
}

main(){
  do_it(ch);
}
 

四. 代码实现

#include<stdio.h>//中间代码比较冗长,一段时间后改进
#include<stdlib.h>

calculate(int time[]){
	int sums,k;
	k=0;
	if (time[2]%2==1){
		k=1;
	}
	if (time[1]>time[3]){
		if (k==0){
			if((time[1]-time[3])%4!=0){
			sums=time[4]+(time[2]/2)+(time[1]-time[3])/4+1+time[3];
		}
		 else{
		 	sums=time[4]+(time[2]/2)+(time[1]-time[3])/4+time[3];
		 }
			printf("%d",sums);
		}
		else{
			if (k==1){
			sums=time[4]+(time[2]/2)+time[3];
			if (time[1]-time[3]>=2){
				sums++;
				if ((time[1]-time[3]-2)/4!=0){
					if((time[1]-time[3]-2)%4==0){ 
				        sums+=(time[1]-time[3]-2)/4;
				        }
						else{
						  sums+=(time[1]-time[3]-2)/4+1;
					}
			  }
			    else {
				     if ((time[1]-time[3]-2)!=0){
				         sums++;
			          }
			}
			}
			else{
				sums++;
			}
			printf("%d",sums);
		}
		}
	}
	else{
		if (k==0){
			sums=time[4]+(time[2]/2)+time[3];
			printf("%d",sums);
		}
		else{
			if (k==1){
			sums=time[4]+(time[2]/2)+time[3]+1;
			printf("%d",sums);
		}
	}
   }
}

main(){
	int times[5]={0,0,0,0,0};
	int i,n,num;
	scanf("%d",&n);
	for (i=0;i<n;i++){
		scanf("%d",&num);
		times[num]++;
	}
	calculate(times);
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值