• BZOJ4391: [Usaco2015 dec]High Card Low Card


    Description

    Bessie the cow is a huge fan of card games, which is quite surprising, given her lack of opposable thumbs. Unfortunately, none of the other cows in the herd are good opponents. They are so bad, in fact, that they always play in a completely predictable fashion! Nonetheless, it can still be a challenge for Bessie to figure out how to win.

    Bessie and her friend Elsie are currently playing a simple card game where they take a deck of 2N cards, conveniently numbered 1…2N, and divide them into N cards for Bessie and N cards for Elsie. The two then play NN rounds, where in each round Bessie and Elsie both play a single card. Initially, the player who plays the highest card earns a point. However, at one point during the game, Bessie can decide to switch the rules so that for the rest of the game, the player who plays the lowest card wins a point. Bessie can choose not to use this option, leaving the entire game in "high card wins" mode, or she can even invoke the option right away, making the entire game follow the "low card wins" rule.

    Given that Bessie can predict the order in which Elsie will play her cards, please determine the maximum number of points Bessie can win.

    奶牛Bessie和Elsie在玩一种卡牌游戏。一共有2N张卡牌,点数分别为1到2N,每头牛都会分到N张卡牌。

    游戏一共分为N轮,因为Bessie太聪明了,她甚至可以预测出每回合Elsie会出什么牌。

    每轮游戏里,两头牛分别出一张牌,点数大者获胜。

    同时,Bessie有一次机会选择了某个时间点,从那个时候开始,每回合点数少者获胜。

    Bessie现在想知道,自己最多能获胜多少轮?

    Input

    The first line of input contains the value of N (2≤N≤50,000).

    The next N lines contain the cards that Elsie will play in each of the successive rounds of the game. Note that it is easy to determine Bessie's cards from this information.

    Output

    Output a single line giving the maximum number of points Bessie can score.

    Sample Input

    4
    1
    8
    4
    3

    Sample Output

    3

    HINT

    Here, Bessie must have cards 2, 5, and 6, and 7 in her hand, and she can use these to win at most 3 points. For example, she can defeat the 1 card and then switch the rules to "low card wins", after which she can win two more rounds.

    这是套路题??DP+贪心的路子很明显
    对于每种赢得方式,我们肯定使用最接近的牌最优,考虑DP
    f[i]表示前i个大赢的最多回合数,g[i]表示后i个包括i小赢的最多回合数
    答案=max(f[i]+g[i+1])
    代码如下:
    //MT_LI
    #include<set>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    int f[51000],g[51000];
    int a[51000],b[110000];
    int v[51000];
    int n;
    set<int >q,p;
    bool cmp(int a,int b){return a>b;}
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=2*n;i++)b[i]=i;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)b[a[i]]=0;
        sort(b+1,b+1+2*n,cmp);
        for(int i=n+1;i<=2*n;i++)b[i]=-b[i-n];
        sort(b+1,b+1+2*n,cmp);
        for(int i=1;i<=n;i++)q.insert(b[i]);
        for(int i=n+1;i<=2*n;i++)p.insert(b[i]);
        
        f[0]=0;
        for(int i=1;i<=n;i++)
        {
            set<int >:: iterator it=q.upper_bound(a[i]);    
            if(it==q.end())f[i]=f[i-1];
            else f[i]=f[i-1]+1,q.erase(it);
        }
        g[n+1]=0;
        for(int i=n;i>=1;i--)
        {
            set<int >:: iterator it=p.upper_bound(-a[i]);
            if(it==p.end())g[i]=g[i+1];
            else g[i]=g[i+1]+1,p.erase(it);
        }
        int ans=0;
        for(int i=0;i<=n;i++)ans=max(ans,f[i]+g[i+1]);
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    东南大学2021年高等代数
    北京师范大学2021年数学分析
    华东师范大学2021年数学分析考研试题参考解答修正版
    中国科学院大学2021年数学分析考研试题参考解答
    [数学考研竞赛00052]浙江大学2021年数学分析考研试题参考解答
    python3.6, cv2.imread()导入图片失败,none
    python商品分类信息
    mongodb 聚合(Map-Reduce)
    mongodb 聚合(aggregate)
    Java-Security(七):Spring Security方法注解认证启用
  • 原文地址:https://www.cnblogs.com/MT-LI/p/9847849.html
Copyright © 2020-2023  润新知