zoukankan      html  css  js  c++  java
  • Cf水题B

    地址:  https://vjudge.net/problem/27861/origin

    Ilya plays a card game by the following rules.

    A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.

    More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.

    Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?

    Input

    The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has.

    Each of the next n lines contains two non-negative space-separated integers — aiand bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly.

    Output

    Print the single number — the maximum number of points you can score in one round by the described rules.

    Examples

    Input
    2
    1 0
    2 0
    Output
    2
    Input
    3
    1 0
    2 0
    0 2
    Output
    3

    Note

    In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.

    In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards.

        以b为基准进行sort排序,再用一个计数器就好了。第三段写得明明白白,题意的理解很重要

        

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    struct node
    {
        int a,b;
    }st[1005];
    bool cmp(node a,node b)
    {
        if(a.b==b.b)
            return a.a>b.a;
        else
            return a.b>b.b;
    }
    int main()
    {
         int n;
         cin>>n;
         for(int i=0;i<n;i++)
             cin>>st[i].a>>st[i].b;
         sort(st,st+n,cmp);
        int sum=0,count=1,all=n;
        for(int i=0;i<n;i++)
        {
            sum+=st[i].a;
            count--;
            all--;
            count+=st[i].b;
            if(count==0)
                break;
            if(all==n)
                break;
        }
        cout<<sum<<endl;
    }
  • 相关阅读:
    错误论:错误是人类实践结果的一种状态,是实践的一个中间环节
    逻辑思维
    正确与错误、真理与谬误
    正确的判断,来自于错误的判断
    PHP+MySQL实现对一段时间内每天数据统计优化操作实例
    centos linux ip地址无法连接数据库,ssh登录服务器时必须使用22端口
    如何更改linux文件目录拥有者及用户组
    navicat ssh通道受限问题处理
    Navicat for MySQL 使用SSH方式链接远程数据库(二)
    Navicat for MySQL 使用SSH方式链接远程数据库
  • 原文地址:https://www.cnblogs.com/liyexin/p/11512717.html
Copyright © 2011-2022 走看看