結果

問題 No.2145 Segment +-
ユーザー lddlinanlddlinan
提出日時 2023-03-21 19:28:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 2,048 bytes
コンパイル時間 745 ms
コンパイル使用メモリ 85,792 KB
実行使用メモリ 36,240 KB
最終ジャッジ日時 2023-10-18 18:33:24
合計ジャッジ時間 3,376 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 23 ms
35,680 KB
testcase_11 AC 22 ms
35,680 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 3 ms
5,960 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 16 ms
26,560 KB
testcase_21 AC 4 ms
5,968 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 16 ms
26,560 KB
testcase_25 AC 24 ms
36,240 KB
testcase_26 AC 23 ms
36,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <algorithm>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

bool rcmp(int a, int b) { return a>b; }
typedef long long LL;
class mypcmp {
public:
    bool operator()(const int& a, const int& b) {
        return a<b;
    }
};


char ib[200004];
int dp[200004][21][2];
int main() {
    int n, i, v, k;
    scanf("%d", &n);
    scanf("%s", ib);
    for (k=0; k<=20; k++) dp[0][k][0]=dp[0][k][1]=-1;
    if (ib[0]=='?') dp[0][0][0]=1;
    else if (ib[0]=='+') dp[0][0][0]=1;
    else dp[0][1][1]=1;
    for (i=1; i<n; i++) {
        for (k=0; k<=20; k++) {
            // (k, 0)
            v=-1;
            if (ib[i]=='?') {
                if (dp[i-1][k][0]>=0) v=max(v, dp[i-1][k][0]+1);
                if (dp[i-1][k][1]>=0) v=max(v, dp[i-1][k][1]+1);
            } else if (ib[i]=='+') {
                if (dp[i-1][k][0]>=0) v=max(v, dp[i-1][k][0]+1);
                if (dp[i-1][k][1]>=0) v=max(v, dp[i-1][k][1]+1);
            } else {
                if (dp[i-1][k][0]>=0) v=max(v, dp[i-1][k][0]-1);
                if (dp[i-1][k][1]>=0) v=max(v, dp[i-1][k][1]-1);
            }
            dp[i][k][0]=v;
            // (k, 1)
            v=-1;
            if (ib[i]=='?') {
                if (k && dp[i-1][k-1][0]>=0) v=max(v, dp[i-1][k-1][0]+1);
                if (dp[i-1][k][1]>=0) v=max(v, dp[i-1][k][1]+1);
            } else if (ib[i]=='+') {
                if (k && dp[i-1][k-1][0]>=0) v=max(v, dp[i-1][k-1][0]-1);
                if (dp[i-1][k][1]>=0) v=max(v, dp[i-1][k][1]-1);
            } else {
                if (k && dp[i-1][k-1][0]>=0) v=max(v, dp[i-1][k-1][0]+1);
                if (dp[i-1][k][1]>=0) v=max(v, dp[i-1][k][1]+1);
            }
            dp[i][k][1]=v;
        }
    }
    for (k=0; k<=20; k++) if (dp[n-1][k][0]>=0||dp[n-1][k][1]>=0) break;
    printf("%d\n", k);
    return 0;
}
0