PAT class a 1044 Shopping in Mars (JAVA version)

Simple analysis of this question

The so-called "binary search" is investigated, but I use JAVA to realize the binary search method. When AC is used, there are fewer test points than the previously written code.
Here is a comparison of code run times:
Test case (PAT standard case 1):

16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
Edition Running time
Version 1 written by myself 5295904300ns
Version 2 written by myself 1823066099ns
according to Liu Shen blog Binary search version written 4099952500ns

The difference between version 1 and version 2 is to determine whether a value is greater than the minimum value when entering the loop. If so, skip the loop.

The idea of self written version

Very simply, each time a number is read in, the number is added to the previous elements in the array, and the number is stored in the corresponding position.
Take PAT standard case 1 as an example

16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
Array after first read:
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 Array after second read:
5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 Array after the third read:
6 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0
......

According to the above ideas, the first element of the array represents the value that starts from the first diamond, and the second element represents the value that starts from the second diamond
And set the mincost variable to represent the minimum diamond value expenditure.

Thinking of binary search

: stack the value of each diamond in order, starting from the first diamond given in the title, set the diamond obtained in each cycle as Z, and note (set the value of the diamond obtained in the first cycle as 0, and take the first diamond given in the title in the second cycle) find a diamond in line with "Z as the first diamond in the result of this cycle and the result of drilling" The diamond string whose total value is equal to or greater than the value to be paid according to the given topic "is compared with the minimum value and the value to be paid according to the given topic, so as to get the result.

Version 1, 2 code

Version 2 is to remove the comment characters from the three lines of the following code:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		long startTime=System.nanoTime();   //Get start time 
		Scanner scaner = new Scanner(System.in);
		int diamondsNum = scaner.nextInt();
		int totalMoney = scaner.nextInt();
		int[] diamondsString = new int[diamondsNum];
		boolean flag = false;
		ArrayList<Integer> start = new ArrayList<>();
		ArrayList<Integer> end = new ArrayList<>();
		int mincost = Integer.MAX_VALUE;
		int[] addValue = new int[diamondsNum];
		for(int i = 0 ; i < diamondsNum ; i++) {
			diamondsString[i] = scaner.nextInt();
			addValue[i] = 0;
			for(int j = i ; j >= 0 ; j--) {
				addValue[j] += diamondsString[i];
//				if(addValue[j] > mincost) {
//					break;
//				}
				if(addValue[j] == totalMoney) {
					flag = true;
					if((i + 1) > (j + 1))
						System.out.println((j + 1) + "-" + (i + 1));
					else
						System.out.println((i + 1) + "-" + (j + 1));
				}
				else if(addValue[j] > totalMoney) {
					if(addValue[j] < mincost) {
						mincost = addValue[j];
						start.clear();
						end.clear();
						start.add((i + 1));
						end.add((j + 1));
					}
					else if(addValue[j] == mincost) {
						start.add((i + 1));
						end.add((j + 1));
					}
					
				}
			}
		}
		scaner.close();
		if(!flag) {
			for(int i = 0 ; i < start.size() ; i++) {
				if(start.get(i) < end.get(i))
					System.out.println(start.get(i) + "-" + end.get(i));
				else
					System.out.println(end.get(i) + "-" + start.get(i));
				
			}
		}
		long endTime=System.nanoTime(); //Get end time  
		System.out.println("Program run time: "+(endTime-startTime)+"ns"); 
	}

}

Binary search version

according to Liu Shen blog Rewrite:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	static int diamondsNum = -1;
	static int totalMoney = -1;
	static int[] diamondsString;
	
	static int[] binaryCheck(int start) {
		int[] result = new int[2];
		int left = start - 1;
		int leftValue;
		if(left == -1)
			leftValue = 0;
		else
			leftValue = diamondsString[left];
		int right = diamondsNum - 1;
		while(left < right) {
			int mid = (left + right)/2;
			if(diamondsString[mid] - leftValue >= totalMoney)
				right = mid;
			else
				left = mid + 1;			
		}
		result[0] = right;
		result[1] = diamondsString[right] - leftValue;
		return result;
	}

	public static void main(String[] args) {
		long startTime=System.nanoTime();   //Get start time   
		Scanner scaner = new Scanner(System.in);
		diamondsNum = scaner.nextInt();
		totalMoney = scaner.nextInt();
		diamondsString = new int[diamondsNum];
		ArrayList<Integer> start = new ArrayList<>();
		ArrayList<Integer> end = new ArrayList<>();
		int mincost = Integer.MAX_VALUE;
		for(int i = 0 ; i < diamondsNum ; i++) {
			diamondsString[i] = scaner.nextInt();
			if(i >= 1)
				diamondsString[i] += diamondsString[i - 1];
		}
		scaner.close();
		for(int i = 0 ; i < diamondsString.length ; i++) {
			int[] temp = binaryCheck(i);
			int left = i;
			int right = temp[0];
			int tempSum = temp[1];
			if(tempSum >= totalMoney) {
				if(tempSum < mincost) {
					start.clear();
					end.clear();
					mincost = tempSum;
					start.add(left + 1);
					end.add(right + 1);
				}
				else if(tempSum == mincost) {
					start.add(left + 1);
					end.add(right + 1);
				}
			}
		}
		long endTime=System.nanoTime(); //Get end time  
		System.out.println("Program run time: "+(endTime-startTime)+"ns");
		for(int i = 0 ; i < start.size() ; i++) {
			System.out.println(start.get(i) + "-" + end.get(i));
		}
		  
	}

}

Analysis

It seems that Java can't run faster than c + +, and the test case given by the topic is quite large. In addition, I should write Liu Shen's code, which may also have errors, leading to the long running time of the rewritten java code. Please see that all the big guys of this blog are correct. I'm very grateful.

Published 8 original articles, praised 0, visited 126
Private letter follow

Tags: Java

Posted on Sat, 01 Feb 2020 10:47:40 -0500 by cbn_noodles