D. Walking Robot[模拟]

There is a robot staying at X=0X=0 on the OxOx axis. He has to walk to X=nX=n. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel.
The ii-th segment of the path (from X=i−1X=i−1 to X=iX=i) can be exposed to sunlight or not. The array ss denotes which segments are exposed to sunlight: if segment ii is exposed, then si=1si=1, otherwise si=0si=0.
The robot has one battery of capacity bb and one accumulator of capacity aa. For each segment, you should choose which type of energy storage robot will use to go to the next point (it can be either battery or accumulator). If the robot goes using the battery, the current charge of the battery is decreased by one (the robot can’t use the battery if its charge is zero). And if the robot goes using the accumulator, the current charge of the accumulator is decreased by one (and the robot also can’t use the accumulator if its charge is zero).
If the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can’t become higher than it’s maximum capacity).
If accumulator is used to pass some segment, its charge decreases by 1 no matter if the segment is exposed or not.
You understand that it is not always possible to walk to X=nX=n. You want your robot to go as far as possible. Find the maximum number of segments of distance the robot can pass if you control him optimally.
Input
The first line of the input contains three integers n,b,an,b,a (1≤n,b,a≤2⋅1051≤n,b,a≤2⋅105) — the robot’s destination point, the battery capacity and the accumulator capacity, respectively.
The second line of the input contains nn integers s1,s2,…,sns1,s2,…,sn (0≤si≤10≤si≤1), where sisi is 11 if the ii-th segment of distance is exposed to sunlight, and 00 otherwise.
Output
Print one integer — the maximum number of segments the robot can pass if you control him optimally.
Examples
inputCopy
5 2 1
0 1 0 1 0
outputCopy
5
inputCopy
6 2 1
1 0 0 1 0 1
outputCopy
3

模拟,大意为一个机器人走路,1为有光,0没有,有2块电池,每走一格消耗1的电池,有光且用电池a时可以补充b的一个电量。
分情况模拟

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int n,a,b,ma[500005];
int main()
{
    cin>>n>>a>>b;
    int as=a,bs=b;
    for(int i=1;i<=n;i++)
        cin>>ma[i];
    for(int i=1;i<=n;i++){
        if(ma[i]==0){
            if(b>0)
                b--;
            else if(a>0)
                a--;
            else {
                cout<<i-1;
                return 0;
            }

        }else{
            if(a>0 && b<bs)
                a--,b++;
            else if(b>0)
                b--;
            else if(a>0)
                a--;
            else{
                cout<<i-1;
                return 0;
            }
        }
    }
    cout<<n;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值