Algorithm training-carriage

Problem Description

  Winter vacation is here, and the students are traveling by train together. There are N compartments in a train. Each compartment can accommodate four people. The compartments are all full. The tickets bought by the students are not together. Some compartments have no classmates, some compartments have only one classmate, and some have Some have two, some have three, and some are full of classmates. If there are less than three classmates in a cubicle, they will feel very lonely. Fortunately, you can negotiate with other passengers to change seats so that students can live in the same compartment. However, persuading others to change seats is a troublesome matter. How many times can the exchange be done at least so that all students can be accommodated in the same compartment? No less than three classmates?

Input format

  The first line of input contains a positive integer N, indicating the number of compartments.

  The N numbers from 0 to 4 entered in the second line indicate how many students there are in each compartment.

Output format

  An integer indicating the minimum number of exchanges. If the requirement cannot be satisfied by any exchange, -1 is output.

Sample input

5

1 2 2 4 3

Sample output

2


import java.util.Scanner;
public class Main {
public static void main(String [] args) {
		Scanner scanner =new Scanner(System.in);
		int N=scanner.nextInt();//隔间的数量
		int num[]=new int[N];//记录每一个隔间内人数
		
		int a=0;//4人隔间数目
		int b=0;//3人隔间数目
		int c=0;//2人隔间数目
		int d=0;//1人隔间数目
		
		int sum=0;//最少调换次数
		
		//初始化num数组
		for(int i=0;i<N;i++) {
			num[i]=scanner.nextInt();
			switch(num[i]) {
			case 1: d++;break;
			case 2: c++;break;
			case 3: b++;break;
			case 4: a++;break;
			}
		}
		//分为两种情况(隔间人数与隔间数量对应关系为a:4,b:3,c:2,d:1
		//第一种:c=d
		if(c==d) {
			sum=d;
		}
		else {
			//1的隔间数比2的隔间数多
			 if(d>c) {
				int temp=d-c;			
				switch(temp%3) {
				//temp除3余0时,3个1人隔间只需要调换两次
				case 0: {
					sum +=(temp/3)*2+c;
					break;
				}
            //temp除3余1时,3个1人隔间只需要调换两次,剩余一个一人隔间,需判断调换之后是否有3人间。若没有3人隔间,就不符合题意返回-1
				case 1: {
					b+=c;//记录将1人隔间和2人隔间调换之后,3人隔间的总数量
					sum+=(temp/3)*2+c;
					if(b>=1)
					  sum+=1;
					else 
					sum=-1;
					break;
				}
			//temp除3余2时,需寻找是否有两个以上的3人隔间或一个人隔间,若没有返回-1。
				case 2: {
					sum+=(temp/3)*2+c;
					b+=c;//记录将1人隔间和2人隔间调换之后,3人隔间的总数量
					if(b>=2)
					   sum+=2;
					else {
						 if(a>=1) {
							 sum+=3;
						 }
						 else {
							 sum=-1;
						 }		
					}
	
					break;
					}					
				
				}

			 }
			 //第二种情况2人隔间数量比1人隔间数量多
			 else {
				 
				 int temp=c-d;//2人隔间数量比1人隔间数量多的量
				 b+=d+(temp/3)*2;//3人隔间数量
				 
				 switch(temp%3) {
				 //多余的2人隔间数量除3余0
				 case 0:sum+=d+(temp/3)*2;break;
				 //多余的2人隔间数量除3余1
				 case 1:{
					
					 if(a>=1) {
						 sum+=d+(temp/3)*2+1;
					 }
					 else {
						 if(b>=2) {
							 sum+=d+(temp/3)*2+2;
						 }
						 else {
							 sum=-1;
						 }
						
					}
					 break;
				 }
				 case 2:
						 sum+=d+(temp/3)*2+2;
						 break;		
				 }
				 
			 }
			 
		}
		System.out.println(sum);
}    
}

Guess you like

Origin blog.csdn.net/qq_62994974/article/details/128553304
Recommended