MARTIIROSYAN's blog

By MARTIIROSYAN, history, 9 months ago, In Russian

this day i wanna teach you how to solve this hardest problem in codeforces 282A - Bit++ , this is so interesting problem, we can use DP for fast solution, we can calc max and min answer using DP, and answer in problem is min+max

#include <bits//stdc++.h>
using namespace std;
int read_num(){
    int a;
    cin>>a;
    return a;
}
char read_char(){
    char a;
    cin>>a;
    return a;
}
string read_str(int len){
    string a;
    while(len--)a+=read_char();
    return a;
}
int find_score(string a){
    return count(begin(a),end(a),'+')?1:-1;
}
int main()
{
    int n=read_num();
    vector<string>a(n);
    vector<int>score(n);
    vector<vector<int>>dp1(n),dp2(n);
    for(int i=0;i<n;++i)a[i]=read_str(3),score[i]=find_score(a[i]);
    for(int i=0;i<n;++i)dp1[i]=vector<int>(i+2,/*infinity*/n*500),dp2[i]=vector<int>(i+2,/*-infinity*/-n*500);
    dp1[0][0]=0;
    dp1[0][1]=score[0];
    for(int i=1;i<n;++i){
        for(int j=0;j<=i;++j){
            dp1[i][j]=min(dp1[i][j],dp1[i-1][j]);
            dp1[i][j+1]=min(dp1[i][j+1],dp1[i-1][j]+score[i]);
        }
    }
    dp2[0][0]=0;
    dp2[0][1]=score[0];
    for(int i=1;i<n;++i){
        for(int j=0;j<=i;++j){
            dp2[i][j]=max(dp2[i][j],dp2[i-1][j]);
            dp2[i][j+1]=max(dp2[i][j+1],dp2[i-1][j]+score[i]);
        }
    }
    cout<<(*min_element(begin(dp1[n-1]),end(dp1[n-1])))+(*max_element(begin(dp2[n-1]),end(dp2[n-1])))<<'\n';
}

nice problem, after 10 likes i teach you how to solve 1A - Theatre Square

  • Vote: I like it
  • +7
  • Vote: I do not like it