zoukankan      html  css  js  c++  java
  • 牛客笔试题

    今天,Alice 和 Bob 两个人发明了一个新的取石子游戏。我们将 n 枚石子摆放成一行,从左到右每枚石子有两个参数,能量ei 和得分ai 。Alice 和 Bob 两人轮流决策,从左到右依次取石子,Alice 先手。每个回合,玩家可以选择下列两个操作之一:1. 消耗一个单位的能量,跳过这个回合。2. 取当前位置的石子。初始时,Alice 和 Bob分别有A和B单位的能量,两个玩家的目的都是最大化自己的得分,双方都采取最优决策,问游戏结束时,Alice 和 Bob 的得分分别为多少。

    题目给定A和B(0≤A≤10^9,0≤B≤10^9)同时给定石子个数n(1≤n≤100), 从左向右,每颗石子的能量e和得分a(所有数字均为正整数,e中元素均小于等于10^9,a中元素的和小于等于100)。请返回一个数组,其中第一个元素为Alice的得分,第二个元素为Bob的得分。

    测试样例:
    9,0,7,[66,2,6,2,8,4,3],[7,12,65,7,4,40,15]
    返回:[112,38]


    #include<vector>
    class GetT
    {
    public:
        //目的,最大化自己的得分
        void Get(int &A, int &B, int N, int *e, int *a,int re[])
        {
            //先手
            int *pCurPer = &A;
            int curPer = 1;
    
    
            int  cur = 0;
            while (cur<N)
            {
                //有能量且判定下回合的更值钱
                //if (Magic() && NextIsBetter())
                if (*pCurPer && a[cur+1]>a[cur])
                {
                    (*pCurPer)--;
                    GetNext(pCurPer, curPer, A, B);
                    continue;
                }
                else
                {
                    //获取能量,获取得分。
                    *pCurPer += e[cur];
                    re[curPer] += a[cur];
    
                    GetNext(pCurPer, curPer, A, B);
                    cur++;
                }
                //取当前位置的石子。
            }
        }
        void GetNext(int *&cur,int &curper,int &p,int &q)
        {
            curper = !curper;
            if (cur == &p)
            {
                cur = &q;
            }
            else
            {
                cur = &p;
            }
        }
    };

    测试:

    void main()
    {
        int A = 9;
        int B = 0;
        const int s = 7;
        int see[] = { 66, 2, 6, 2, 8, 4, 3 };
        int sea[] = { 7, 12, 65, 7, 4, 40, 15 };
    
        int re[] = {0,0};
        GetT get;
    
        get.Get(A, B, s, see, sea,re);
        cout << re[0] << "	";
        cout << re[1] << endl;
    }

    运行结果

    112 38
    请按任意键继续. . .

  • 相关阅读:
    Python解释器
    js子节点children和childnodes的用法
    添加jar包需注意
    Class.forName("com.mysql.jdbc.driver");
    java集合类总结
    interface思考练习一
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    Struts2的配置文件中, <package>的作用,<action><result>重名?
    在Struts2的Action中获得request response session几种方法
    学习一直都是一个相见恨晚的过程,我希望我的相见恨晚不会太晚。
  • 原文地址:https://www.cnblogs.com/lang5230/p/5138081.html
Copyright © 2011-2022 走看看