D. Walking Robot
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There is a robot staying at X=0 on the Ox axis. He has to walk to X=n. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel.
The i-th segment of the path (from X=i−1 to X=i) can be exposed to sunlight or not. The array s denotes which segments are exposed to sunlight: if segment i is exposed, then si=1, otherwise si=0.
The robot has one battery of capacity b and one accumulator of capacity a. 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=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,a (1≤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 n integers s1,s2,…,sn (0≤si≤1), where si is 1 if the i-th segment of distance is exposed to sunlight, and 0 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
Note
In the first example the robot can go through the first segment using the accumulator, and charge levels become b=2 and a=0. The second segment can be passed using the battery, and charge levels become b=1 and a=1. The third segment can be passed using the accumulator, and charge levels become b=1 and a=0. The fourth segment can be passed using the battery, and charge levels become b=0 and a=1. And the fifth segment can be passed using the accumulator.
In the second example the robot can go through the maximum number of segments using battery two times and accumulator one time in any order.
题意 :两种类型的robot 做一定数量的操作 要让他们走的路程最远
思路:
1.若遇到有光的区域,肯定是优先选择battery的,这样可以让accumulator的充电,这样消耗的电和充的电正好抵消。
2.若遇到无光的区域, 肯定优先选择accumulator的,让他电耗耗光么正好可以充电。
3.WA两发的原因:若遇到有光的区域 并且accumulator的电正好是满的,也就是充不进电,那这时就不能选battery的,而要选accumulator的了。可以参照要通过路径是1(此时充满电) 1 1这种情况。
下面附上ac代码:
#include <bits/stdc++.h>
using namespace std;
int arr[300000], n, sum = 0, a, b;
int main() {
ios::sync_with_stdio(false);
cin >> n >> a >> b;
int tempa = a;
int tempb = b;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 1; i <= n; i++) {
if (arr[i]) {
if(tempb < b) {
if(tempa) {
tempa--;
tempb++;
}
else if(tempb){
tempb--;
}
}
else {
tempb--;
}
sum++;
if (tempa == 0 && tempb == 0) {
cout << sum << endl;
return 0;
}
}
else {
if(tempb) {
tempb--;
}
else if(tempa) {
tempa--;
}
sum++;
if (tempa == 0 && tempb == 0) {
cout << sum << endl;
return 0;
}
}
}
cout << n << endl; //全都能走完
return 0;
}