Intergalactic Bidding (java大数的运用)

Description

Today the Intergalactic Council of Pebble Coins (ICPC) conducted an intergalactic auction of the Neutronium Chaos Pebble Coin (NCPC). This coin, which was forged in the Ancient Coin Machine (ACM), is rumored to be the key to ruling the universe.

Due to the extremely competitive nature of the auction, as well as the odd mechanics of the intergalactic currency used (far too advanced for mere mortals to understand), the auction was conducted with the following rules:

  1. only one participant was allowed to make a bid at a time,

  2. each participant was only allowed to make one bid, and

  3. a participant making a bid had to bid at least twice the amount of the highest bid at the time.

The first participant making a bid was allowed to make a bid of any positive amount.

After the auction there were a lot of sore losers -- understandably, having just lost their chance at world domination. To make the losers feel a little better and prevent possible rioting, the ICPC has decided to hold a lottery for the participants. The winners of the lottery are determined as follows. The ICPC picks a random number s. A group of participants is called winning if the sum of their bets from the auction is equal to s. A participant wins the lottery and receives a prize -- a shiny Pebble Coin -- if they belong to any winning group of participants.

Given the names of the participants, the bets that they made, and the random number s chosen by the ICPC, help them determine which participants won the lottery.

Input

The first line of input contains two integers n and s, where 1 ≤ n ≤ 1 000 is the number of participants, and 1 ≤ s < 101 000 is the random number chosen by the ICPC.

Then follow n lines describing the participants. Each line contains a string t and an integer b, where t is the name of a participant, and 1 ≤ b < 101 000 is the amount of his bet. The name of each participant is unique and consists of between 1 and 20 letters from the English alphabet.

Output

Output an integer k denoting the number of participants that won the lottery. Then output k​ lines containing the names of the participants that won the lottery, one per line, in any order.

Sample Input

5 63
Vader 3
Voldemort 7
BorgQueen 20
Terminator 40
Megatron 101
---------------------------
4 1112
Blorg 10
Glorg 1000
Klorg 1
Zlorg 100

Sample Output

3
BorgQueen
Terminator
Vader
---------------------
0

这题目当时比赛时碰到的时候说实话第一反应是背包,然后一看s的大小,傻了。怎么像都想不出来,赛后发现自己漏了一个

条件,就是每次报价的时候不能小于前一个的两倍,然后就是大数的操作了,这是我打acm以来第一次碰到大数的题目,吓得我赶紧回去学了一波java大数,的确,和c++比起来,java对大数的操作流氓太多了。

接下来讲一下,这题该怎么写

思路就是将所有报价从大到小排序后,正向遍历,能放的都放,因为每次是前一个两倍或者两倍以上报数的,所以

a[i] > a[i + 1] + a[i + 2] + ... + a[n - 1];(类似于二进制)

下面是代码

import java.math.BigInteger;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Vector;


public class Main{
	private static Scanner cin;

	public static void main(String args[]) {
		cin = new Scanner(System.in);
		int n;
		n = cin.nextInt();
		String s;
		BigInteger p;
		BigInteger sum = cin.nextBigInteger();
		Map<BigInteger, String> myMap = new HashMap<BigInteger, String>();
		Vector<BigInteger> vec = new Vector<BigInteger>();
		for(int i = 0; i < n; i++) {
			s = cin.next();
			p = cin.nextBigInteger();
			myMap.put(p,  s);
			vec.add(p);
		}
		
		Collections.sort(vec, Collections.reverseOrder());
		Vector<String> ans = new Vector<String>();
		for(int i = 0; i < n; i++) {
			if(vec.elementAt(i).compareTo(sum) <= 0) {
				sum = sum.subtract(vec.elementAt(i));
				ans.add(myMap.get(vec.elementAt(i)));
			}
		}
		
		if(sum.compareTo(BigInteger.valueOf(0)) == 0) {
			System.out.println(ans.size());
			for(int i = 0; i < ans.size(); i++) {
				System.out.println(ans.elementAt(i));
			}
		} else {
			System.out.println(0);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43737952/article/details/88539800