P2036 PERKET食物(DFS+不降原则)

import java.util.Scanner;

public class P2036 {
	static int N,s=1,b=0;
	static int min;
	static int[][] taste;
	static boolean[] flag;
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		taste = new int[N][2];
		flag = new boolean[N];
		for(int i=0;i<N;i++) {
			taste[i][0] = sc.nextInt();
			taste[i][1] = sc.nextInt();
		}

		min = (int)Math.pow(10, 9);
		for (int i = 1; i <=N; i++) {
			dfs(0,1,i);
		}
		
		System.out.println(min);
	}

	private static void dfs(int a,int pos, int num) {
		if(pos==num+1) {
			return ;
		}
		for(int i=a;i<N;i++) {
			if(flag[i]==false) {
				s *= taste[i][0];
				b += taste[i][1];
				flag[i]=true;
				
				min = Math.min(min, Math.abs(s-b));
				dfs(i+1,pos+1, num);
				
				s /= taste[i][0];
				b -= taste[i][1];
				flag[i]=false;
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_28635317/article/details/115248983
今日推荐