静かで孤独な日記

のんびりたまに

ABC027-D ロボット

using namespace std;
//const ll MOD=(ll)1e9+7;
//const ll inf=(ll)1e14;
const int dy[]={1,0,-1};
const int dx[]={1,0,-1};
int n,m,h,w;
string s;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout<<fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  //FILE *stream2;
  stream1=freopen("in","r",stdin);
  //stream2=freopen("out","w",stdout);
  if(stream1==NULL)return 0;
  //if(stream2==NULL)return 0;
#endif
  cin>>s;
  n=(int)s.size();
  int now=0;
  vector<int> v;
  for(int i=n-1;i>=0;i--){
    if(s[i]=='+'){
      now++;
    }else if(s[i]=='-'){
      now--;
    }else{
      v.push_back(now);
    }
  }
  sort(v.begin(),v.end());
  int ans=0;
  for(int i=0;i<(int)v.size();i++){
    if(i<(int)v.size()/2){
      ans+=-v[i];
    }else ans+=v[i];
  }
  print(ans);
#ifdef LOCAL_DEFINE
  cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
  fclose(stream1);
  //fclose(stream2);
#endif
  return 0;
}
  • 僕は入力される+と-にしか注目できませんでした。
  • 視点を変えて、Mに注目すると見えるんですね。