zoukankan      html  css  js  c++  java
  • Codeforces Gym 100269E Energy Tycoon 贪心

    Energy Tycoon

    题目连接:

    http://codeforces.com/gym/100269/attachments

    Description

    Little Vasya is playing a new computer game — turn-based strategy “Energy Tycoon”.
    The rules of the game are quite simple:
    • The board contains n slots arranged in a line.
    • There are power plants, one power plant occupies one or two consecutive slots, and produces one
    unit of energy.
    • Each turn the game allows you to build one new power plant, you can put it on the board if you
    wish. If there is no place for the new power plant, you can remove some older power plants.
    • After each turn, the computer counts the amount of energy produced by the power plants on the
    board and adds it to the total score.
    2
    2
    1
    2 1
    1
    1 1
    2
    1 1
    1
    1 1 1
    Vasya already knows the types of power plant he will be able to build each turn. Now he wants to know,
    what the maximum possible score he can get is. Can you help him?

    Input

    The first line of the input contains one integer n (1 ≤ n ≤ 100 000) — the number of slots on the board.
    The second line contains the string s. The i-th character of the string is 1 if you can build one-slot power
    plant at the i-th turn and the character is 2 if you can build two-slot power plant at the i-th turn. The
    number of turns does not exceed 100 000.

    Output

    The output should contain a single integer — the maximal score that can be achieved

    Sample Input

    3
    21121

    Sample Output

    10

    Hint

    题意

    有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去

    每个东西在每秒钟都会产生1的能力。

    然后问你怎么放才能使得最后能力最大,输出出来

    题解:

    贪心,最后肯定1越多越好

    所以我们放1的时候,注意一下,如果放不下的话,就把其中一个2扔掉,然后放1就好了

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    string s;
    int main()
    {
        freopen("energy.in","r",stdin);
    	freopen("energy.out","w",stdout);
        int n;
        cin>>n;
        cin>>s;
        long long ans = 0,a1 = 0,a2 = 0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='1')
            {
                if(a1+a2*2+1<=n)
                    a1++;
                else if(a2)
                {
                    a2--;
                    a1++;
                }
            }
            if(s[i]=='2')
            {
                if(a1+a2*2+2<=n)
                    a2++;
            }
            ans+=(a1+a2);
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    Python中判断字典的键或值在不在字典中
    Python中字符串,列表,元组三者之间相互转换
    mysql /*! */
    【kubernetes入门学习】使用minikube创建k8s本地单节点集群
    Java中发生内存泄漏的常见场景
    python的list()列表数据类型的方法详解
    python的str()字符串类型的方法详解
    python的运算符
    python基础之while语句continue以及break --语法以及案例
    python的if条件语句的语法和案例
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5199522.html
Copyright © 2011-2022 走看看