P7083

· · 题解

不说了,上代码

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;

string str;
int main(){
    long long n,s=0,s1=0,s2=0,fin=0;
    /*s(space)表示剩余空格,s1(1 space)表示占一个
    空格的装置,s2(2 space)表示占两个空格的装置) ,fin表示分数*/
    cin>>n>>str;
    s=n;
    int len=str.size();
    for(int i=0;i<len;i++){
        int v=int(str[i])-'0';
        if(v==1){
            if(s>=1){//有剩余空间 建造
                s--;
                s1++;
            }else if(s2>=1){//无剩余空间 有占两格的装置 拆除 建造 
                s2--;
                s1++;
                s++;
            }
        }else if(v==2){
            if(s>=2){//有超过两格的空格 建造 
                s2++;
                s-=2;
            }
        }fin+=s2+s1;
        //cout<<s1<<' '<<s2<<endl;
    }cout<<fin;
    return 0;
}