ZOJ 3447 Doraemon's Number Game(Java優先隊列·BigInteger)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.