ZOJ 3447 Doraemon's Number Game(Java优先队列·BigInteger)

题意  给你一个数组  你每次可以从中删掉2到k个数  然后把删掉的数的积加入到原数组  直到最后只剩一个数   求这样能得到的最大值和最小值的差

每次选的数值越小  选的数量越少  最后得到的结果肯定越大  因为这样大的数可以乘以最大的倍数  运算的次数也是最多从而使+1的次数最多  这显然是有利于最后结果的增大的

同理  每次选的数越大  选的数越多  最后得到的结果越小

这样最大值就是每次取最小的两个数  最大值就是每次取最大的k个数了   很容易用优先队列实现   结果会很大  用Java的BigInteger实现比较方便

import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		int N = 105;

		PriorityQueue<BigInteger> inc = new PriorityQueue<BigInteger>();
		//优先队列默认小的优先 是增序队列
		PriorityQueue<BigInteger> dec = new PriorityQueue<BigInteger>(N,
				new Comparator<BigInteger>() {
					public int compare(BigInteger o1, BigInteger o2) {
						return -o1.compareTo(o2);
					}
				}); //匿名实现Comparator接口  降序大的优先

		int n, k;
		BigInteger a, b, one = BigInteger.ONE;
		List<BigInteger> list = new ArrayList<BigInteger>();

		while (in.hasNext()) {
			n = in.nextInt();
			k = in.nextInt();

			list.clear();
			for (int i = 0; i < n; ++i) {
				a = in.nextBigInteger();
				list.add(a);
			}

			inc.addAll(list);
			while (inc.size() > 1) {
				a = inc.poll();
				b = inc.poll();
				inc.add(a.multiply(b).add(one));
			}

			dec.addAll(list);
			while (dec.size() > 1) {
				a = one;
				for (int i = 0; i < k; ++i)
					if (dec.size() > 0)
						a = a.multiply(dec.poll());
				a = a.add(one);
				dec.add(a);
			}
			System.out.println(inc.poll().subtract(dec.poll()));
		}

		in.close();
	}
}

ZOJ Problem Set - 3447

Doraemon‘s Number Game


Time Limit: 2 Seconds      Memory Limit: 65536 KB



Doraemon and Nobita are playing a number game. First, Doraemon will give Nobita N positive numbers. Then Nobita can deal with these numbers for two rounds. Every time Nobita can delete i (2 ≤ i ≤ K) numbers from the number
set. Assume that the numbers deleted is a[ 1 ], a[ 2 ] ... a[ i ]. There should be a new number X = ( a[ 1 ] * a[ 2 ] * ... * a[ i ] + 1 ) to be inserted back into the number set. The operation will be applied to the number set over and over until
there remains only one number in the set. The number is the result of round. Assume two results A and B are produced after two rounds. Nobita can get |A - B| scores.

Now Nobita wants to get the highest score. Please help him.

Input

Input will contain no more than 10 cases. The first line of each case contains two positive integers N and K (1 ≤ N ≤ 100, 1 ≤ K ≤ 50). The following line contains N positive integers no larger than 50, indicating
the numbers given by Doraemon.

Output

For each case, you should output highest score in one line.

Sample Input

6 3
1 3 4 10 7 15

Sample Output

5563

Hint

For most cases, N ≤ 20

版权声明:本文为博主原创文章,未经博主允许不得转载。

ZOJ 3447 Doraemon's Number Game(Java优先队列·BigInteger)

时间: 2024-12-18 07:53:55

ZOJ 3447 Doraemon's Number Game(Java优先队列·BigInteger)的相关文章

ZOJ 3450 Doraemon&#39;s Railgun (DP&#183;分组背包)

题意  多啦A梦有一个超电磁炮  然后要打死n堆敌人  在同一条射线上的敌人只有先打死前面的一堆才能打后面的一堆  给你打死某堆敌人需要的时间和这堆敌人的人数   问你在T0时间内最多打死多少个敌人 分组背包问题  先要把同一条射线上的敌人放到一个分组里  后面的敌人的时间和人数都要加上前面所有的  因为只有前面的都打完了才能打后面的  然后每组最多只能选择一个   判断共线用向量处理   然后去背包就行了 注意给你的样例可能出现t=0的情况   在分组时需要处理一下    被这里卡了好久 #i

ZOJ 3452 Doraemon&#39;s Stone Game

Doraemon's Stone Game Time Limit: 2 Seconds      Memory Limit: 65536 KB Doraemon is playing a game with Dorami. Initially there are piles of stones on the table. Each pile consists of at most 2 stones. Each stone may be black or white. Doraemon and D

Palindrome Number leetcode java

题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

Valid Number leetcode java

题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambigu

Letter Combinations of a Phone Number leetcode java

题目: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

ZOJ 3407 Doraemon&#39;s Cake Machine [数学]

题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切   经过蛋糕圆心,将蛋糕整个向下切. 2.横切   平行于蛋糕平面进行平切. 3.复制某块小蛋糕    这种操作只能在1和2所有操作都进行完才能进行. 求: 最少进行多少次复制操作可以将蛋糕分为一样的恰好n种.注意需要用恰好m次操作. 如果没有可行解输出-1. 思路: 假设进行三种操作的数目分别是xyz.然后连立两个式子把z消掉,get 2x(y+1)-(x+y

zoj 3816 Generalized Palindromic Number(暴力枚举)

题目链接:zoj 3816 Generalized Palindromic Number 题目大意:给定n,找一个最大的数x,保证x小于n,并且x为palindromic number 解题思路:枚举前i个放于n相同的数,然后去构造后半部分即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef unsigned long long ll; int

LeetCode1046 最后一块石头的重量(贪心—Java优先队列简单应用)

题目: 有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出两块最重的石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么两块石头都会被完全粉碎:如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x.最后,最多只会剩下一块石头.返回此石头的重量.如果没有石头剩下,就返回 0. 提示: 1 <= stones.length <= 301 <= stones[i]

java之BigInteger类

1.BigInteger类概述 可以让超过Integer范围内的数据进行运算. 2.BigInteger类的构造方法 public BigInteger(String val) 将BigInteger的十进制字符串表示形式转换为BigInteger package com; import java.math.BigInteger; /**  * BigInteger类  *  可以让超过Integer范围内的数据进行运算  * 构造方法  *  public BigInteger(String