Codeforces gym101933 I 大数(Java&C++) 超递增背包

16 篇文章 0 订阅
1 篇文章 0 订阅

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++大数,封装完整版

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值