Codeforces gym101933 I 大數(Java&C++) 超遞增揹包

I. Intergalactic Bidding

time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

standard output

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 ss. A group of participants is called winning if the sum of their bets from the auction is equal to ss. 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 ss chosen by the ICPC, help them determine which participants won the lottery.

Input

The first line of input contains two integers nn and ss, where 1≤n≤10001≤n≤1000 is the number of participants, and 1≤s<1010001≤s<101000 is the random number chosen by the ICPC.

Then follow nn lines describing the participants. Each line contains a string tt and an integer bb, where tt is the name of a participant, and 1≤b<1010001≤b<101000 is the amount of his bet. The name of each participant is unique and consists of between 11 and 2020 letters from the English alphabet.

Output

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

Examples

input

5 63
Vader 3
Voldemort 7
BorgQueen 20
Terminator 40
Megatron 101

output

3
Terminator
BorgQueen
Vader

input

4 1112
Blorg 10
Glorg 1000
Klorg 1
Zlorg 100

output

0

 

其實就是個密碼學超遞增揹包,從大到小排序,遍歷時如果小於就減去,因爲只要小於就一定在裏面,有一點兒二進制思想,因爲後面加起來的總數也不會超過這一個。最後如果是0,則可。

主要是大數有一些問題。

Java不會寫,C++大數類也不會寫(好麻煩,一些細節第一次寫注意不到

 

Java大數,排序

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

public class Intergalactic_Bidding {
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(System.in);
        int n=cin.nextInt();
        BigInteger s = cin.nextBigInteger();

        String[] name = new String[n];
        BigInteger[] bid = new BigInteger[n];

        Map<BigInteger,String>m = new HashMap<BigInteger, String>();
        Vector<BigInteger>v = new Vector<BigInteger>();

        for(int i=0;i<n;++i){
            name[i] = cin.next();
            bid[i] = cin.nextBigInteger();
            m.put(bid[i],name[i]);
            v.add(bid[i]);
        }

        Collections.sort(v,Collections.reverseOrder()); //降序排序
        Vector<String>ans = new Vector<String>();

        for(int i=0;i<n;++i){
            if(v.elementAt(i).compareTo(s)<=0){
                s = s.subtract(v.elementAt(i));
                ans.add(m.get(v.elementAt(i)));
            }
        }

        if(s.compareTo(BigInteger.valueOf(0)) != 0)
            ans.removeAllElements();
        System.out.println(ans.size());
        for(int i=0;i<ans.size();++i)
            System.out.println(ans.elementAt(i));
    }
}

 

C++大數,簡略版

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn=1e3+5;

struct Big{
    int len;
    char x[maxn];
    string s;
    bool operator == (const Big &A) const {
        if(A.len!=len) return 0;
        for(int i=0;i<len;++i) if(A.x[i]!=x[i]) return 0;
        return 1;
    }
    bool operator < (const Big &A) const {
        if(A.len!=len) return len<A.len;
        for(int i=len-1;i>=0;--i) if(A.x[i]!=x[i]) return x[i]<A.x[i];
        return 0;
    }
    Big operator + (const Big &A) const {
        Big ans=*this;
        for(int i=0;i<A.len;++i){
            ans.x[i]=ans.x[i]+A.x[i];
        }
        int mx=max(A.len,len),f=0;
        for(int i=0;i<mx+1;++i){
            if(ans.x[i]>=10){
                ans.x[i]-=10;
                ++ans.x[i+1];
            }
            if(ans.x[i]) ans.len=i+1,f=1;
        }
        if(!f) ans.len=1;
        return ans;
    }
    Big operator - (const Big &A) const {
        Big ans=*this;
        for(int i=0;i<A.len;++i){
            ans.x[i]=ans.x[i]-A.x[i];
        }
        int mx=max(A.len,len),f=0;
        for(int i=0;i<mx+1;++i){
            if(ans.x[i]<0){
                ans.x[i]+=10;
                --ans.x[i+1];
            }
            if(ans.x[i]) ans.len=i+1,f=1;
        }
        if(!f) ans.len=1;
        return ans;
    }
    void read(){
        cin>>s;len=s.size();
        for(int i=0;i<len;++i) x[len-i-1]=s[i]-'0';
        s.clear();
    }
    void write(){
        for(int i=len-1;i>=0;--i) printf("%d",x[i]);
        printf("\n");
    }
}s,zero;

int n;
struct node{
    string name;
    Big bid;
    bool operator < (const node & a) const {
        return bid<a.bid;
    }
}parti[maxn];

vector <int> ans;

int main()
{
    zero.len=1;zero.x[0];
    scanf("%d",&n);
    s.read();
    for(int i=0;i<n;++i){
        cin>>parti[i].name;
        parti[i].bid.read();
    }
    sort(parti,parti+n);
    for(int i=n-1;i>=0;--i){
        if(parti[i].bid<s||parti[i].bid==s){
            ans.push_back(i);
            s = s-parti[i].bid;
        }
    }
    if(s==zero){
        printf("%d\n",ans.size());
        for(int i=0;i<ans.size();++i){
            cout<<parti[ans[i]].name<<endl;
        }
    }
    else printf("0\n");

    return 0;
}

 

C++大數,封裝完整版

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章