Submission #2927671

Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))

vector<pair<int, int>> rle(string& s) {
    vector<pair<int, int>> work;
    char last = -1;
    int count = 0;
    for (char c : s) {
        if (c == last) {
            ++count;
        } else {
            if (count > 0) {
                work.push_back(make_pair(last, count));
            }
            last = c; count = 1;
        }
    }
    if (count > 0) {
        work.push_back(make_pair(last, count));
    }
    return work;
}

int solve(string& T){
  vector<ii> R = rle(T);

  int L = R.size();
  int best = 0;
  vector<ii> st;
  st.pb(ii(0,0)); // (st,cnt)
  rep(i,L) {
      int ch = R[i].first, c = R[i].second;
      // vector<ii> st2;
      int st0 = 0, st1 = -1;
      for (ii si : st) {
          int _st = si.first, _cnt = si.second;
          // best = max(best, _cnt);
          if (_st == 0) {
              switch (ch) {
                  case '2': if (c == 1) st1 = max(st1, _cnt);
                            else st1 = max(st1, 0);
                            break;
                  case '?': if (_cnt) {
                              if (c%2) st1 = max(st1, _cnt+c/2);
                              else st0 = max(st0, _cnt+c/2);
                              // best = max(best, _cnt+c/2);
                            }
                            if (c % 2) {
                              st1 = max(st1, c/2);
                              st0 = max(st0, c/2);
                            } else {
                              st0 = max(st0, c/2);
                              st1 = max(st1, c/2-1);
                            }
                            break;
                  default: break;
              }
          } else {
              switch (ch) {
                  case '2': st1 = max(st1, 0);
                            break;
                  case '5': if (c == 1) {
                              st0 = max(st0, _cnt+1);
                            } else {
                              best = max(best, _cnt+1);
                            }
                            break;
                  case '?': if (_cnt) {
                              if ((c+1)%2) st1 = max(st1, _cnt+(c+1)/2);
                              else st0 = max(st0, _cnt+(c+1)/2);
                            }
                            if (c % 2) {
                              st0 = max(st0, c/2+1);
                              st1 = max(st1, c/2);
                            } else {
                              st1 = max(st1, c/2);
                              st0 = max(st0, c/2);
                            }
                            break;
                  default: break;
              }
          }

      }
//      printf("{%d %d}\n", st0, st1);
      st.clear();
      if (st0 >= 0) {
          st.pb(ii(0, st0));
          best = max(best, st0);
      } 
      if (st1 >= 0) {
          st.pb(ii(1, st1));
          best = max(best, st1);
      }
  }
  return best*2;
}

int main() {
  string T; cin >> T;
  cout << solve(T) << endl;
  return 0;
}

Submission Info

Submission Time
Task B - ニコニコレベル
User naoya_t
Language C++14 (GCC 5.4.1)
Score 300
Code Size 4214 Byte
Status
Exec Time 7 ms
Memory 1528 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
All 300 / 300 sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, large_01.txt, large_02.txt, large_03.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
Case Name Status Exec Time Memory
hand_01.txt 1 ms 256 KB
hand_02.txt 1 ms 256 KB
hand_03.txt 1 ms 256 KB
hand_04.txt 1 ms 256 KB
hand_05.txt 4 ms 512 KB
large_01.txt 7 ms 1528 KB
large_02.txt 6 ms 1528 KB
large_03.txt 6 ms 1528 KB
random_01.txt 6 ms 1528 KB
random_02.txt 6 ms 1020 KB
random_03.txt 5 ms 1020 KB
random_04.txt 7 ms 1528 KB
sample_01.txt 1 ms 256 KB
sample_02.txt 1 ms 256 KB
sample_03.txt 1 ms 256 KB
sample_04.txt 1 ms 256 KB
sample_05.txt 1 ms 256 KB