zoukankan      html  css  js  c++  java
  • UPC-6474 AtCoDeer and Rock-Paper(模拟)

    题目描述
    AtCoDeer the deer and his friend TopCoDeer is playing a game. The game consists of N turns. In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition:
    (※) After each turn, (the number of times the player has played Paper)≤(the number of times the player has played Rock).
    Each player’s score is calculated by (the number of turns where the player wins) − (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.
    (For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)
    With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts. Plan AtCoDeer’s gesture in each turn to maximize AtCoDeer’s score.
    The gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1≤i≤N) character in s is g, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1≤i≤N) character of s in p, TopCoDeer will play Paper in the i-th turn.

    Constraints
    1≤N≤105
    N=|s|
    Each character in s is g or p.
    The gestures represented by s satisfy the condition (※).
    输入
    The input is given from Standard Input in the following format:
    s
    输出
    Print the AtCoDeer’s maximum possible score.
    样例输入
    gpg
    样例输出
    0
    提示
    Playing the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score.

    题意:两个人剪刀石头布,只能出石头和布,赢了得一分,输了扣一分,平手不得分,我们已经预知对手出石头和布的步骤。现在,要求我们每次出一个石头和布时都要保证当前我们出过石头的步骤次数大于等于出布的次数,两人皆要遵守此规则,问最后最多可以得多少分。注意这个出石头和布次数的比较,是每轮都要比较的,也就是说,第一轮,我们什么都没出,因此石头计数为0,布计数为0,为了遵守规则,第一次必出石头,应根据石头的已出步数得知我们是否可以出布。第二轮时,我们出了一个石头,那么我们可以出一次布,之后布和石头次数相等时,就不得再出布,除非垒够了一定数量的石头,符合规则时才能继续使用布。

    直接计数模拟即可,可以发现,只要出布,就能挽回一分差值,因此无论在哪里出布,我们尽量可以出就多出布,那么每轮做一次计数和判断,能出则出,否则出石头攒布。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10;
    char str[maxn];
    int main()
    {
        while(scanf("%s",str)!=EOF)
        {
            int gg=0,pp=0,ans=0;
            int len=strlen(str);
            for(int i=0; i<len; i++)
            {
                if(str[i]=='g')
                {
                    if(pp<gg)ans++,pp++;
                    else gg++;
                }
                else
                {
                    if(pp<gg)pp++;
                    else gg++,ans--;
                }
            }
            printf("%d
    ",ans);
        }
    }
    
  • 相关阅读:
    Java 中的 匿名类
    spring boot 中容器 Jetty、Tomcat、Undertow
    微信小程序-动态设置背景色navigationBarBackgroundColor的值
    vue el-tree:默认展开第几级节点
    safarai
    微信小程序-输入框输入文字后,将光标移到文字中间,接着输入文字后光标又自动跳到最后
    微信小程序-WebSocket应用
    微信小程序-聊天列表-角标
    SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决
    button 去掉原生边框
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135764.html
Copyright © 2011-2022 走看看