標籤:acm 優先隊列
題意 給你一個數組 你每次可以從中刪掉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 - 3447Doraemon‘s Number GameTime 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 31 3 4 10 7 15
Sample Output
5563
Hint
For most cases, N ≤ 20
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
ZOJ 3447 Doraemon's Number Game(Java優先隊列·BigInteger)