Submission #1689888
Source Code Expand
#include <iostream>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <algorithm>
#include <climits>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <sstream>
#define REP(i,n) for(ll i=0;i<(n);i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
//typedef pair<double, double> P;
#ifdef LOCAL
#define dump(...) \
do { \
std::ostringstream os; \
os << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
print_to(os, ", ", "\n", __VA_ARGS__); \
std::cerr << os.str(); \
} while (0)
#define dump_(a) \
do { \
std::ostringstream os; \
os << __LINE__ << ":\t" << #a << " = ["; \
print_to_(os, ", ", "]\n", all(a)); \
std::cerr << os.str(); \
} while (0)
#else
#define dump(...)
#define dump_(...)
#endif
template <typename T>
void print_to(std::ostream &os, const char *, const char *tail, const T &fst) {
os << fst << tail;
}
template <typename Fst, typename... Rst>
void print_to(std::ostream &os, const char *del, const char *tail, const Fst &fst, const Rst &... rst) {
os << fst << del;
print_to(os, del, tail, rst...);
}
template <typename Iter>
void print_to_(std::ostream &os, const char *del, const char *tail, Iter bgn, Iter end) {
for (Iter it = bgn; it != end;) {
os << *it;
if (++it != end) {
os << del;
} else {
os << tail;
}
}
}
template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &... rst) {
print_to(std::cout, "\n", "\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &... rst) {
print_to(std::cout, " ", "\n", fst, rst...);
}
template <typename Iter>
void println_(Iter bgn, Iter end) {
print_to_(std::cout, "\n", "\n", bgn, end);
}
template <typename Iter>
void print_(Iter bgn, Iter end) {
print_to_(std::cout, " ", "\n", bgn, end);
}
// const int MOD = 1000000007;
namespace trush {
int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed), std::cin.tie(0),
std::ios::sync_with_stdio(0), 0);
}
/*
string S;
int dp[110][110][310];
signed main()
{
cin >> S;
REP(i,110) REP(j,110) REP(k,310) dp[i][j][k] = -INF;
dp[0][0][0] = (S[0] == '(' ? 1 : -1);
REP(i,S.length()-1) {
REP(j,110) {
REP(k,310) {
if (dp[i][j][k] != -INF) {
//変えない
if ((dp[i][j][k] + (S[i+1] == '(' ? 1 : -1)) >= 0)
dp[i+1][j][k+1] = dp[i][j][k] + (S[i+1] == '(' ? 1 : -1);
//変える
if ((dp[i][j][k] + (S[i+1] == '(' ? -1 : 1)) >= 0)
dp[i+1][i+2][k+2] = dp[i][j][k] + (S[i+1] == '(' ? -1 : 1);
}
}
}
}
int ans = INF;
REP(j,110) REP(k,310) if (dp[S.length()-1][j][k] == 0) {
dump(k - (S.length() - j));
ans = min(ans, k - ((int)S.length() - j));
}
cout << ans << endl;
}
*/
//dp[i][j][k] = i文字目までで最後に変更したのがj文字目で
//cnt(そこまでの'('の数-')'の数)の数がkである最小の変更回数
int dp[101][101][101];
string S;
signed main()
{
cin >> S;
int N = S.length();
REP(i,N+1) REP(j,N+1) REP(k,N+1) dp[i][j][k] = INF;
dp[0][0][0] = 0;
REP(i,N) REP(j,N) REP(k,N+1) if (dp[i][j][k] != INF) {
if (S[i] == '(') {
dp[i+1][j][k+1] = min(dp[i+1][j][k+1], dp[i][j][k]);
if (0 < k) dp[i+1][i][k-1] = min(dp[i+1][i][k-1], dp[i][j][k] + 1);
} else {
dp[i+1][i][k+1] = min(dp[i+1][i][k+1], dp[i][j][k] + 1);
if (0 < k) dp[i+1][j][k-1] = min(dp[i+1][j][k-1], dp[i][j][k]);
}
}
int ans = INF;
REP(j,N) ans = min(ans, dp[N][j][0] + j);
cout << ans << endl;
}
Submission Info
Submission Time |
|
Task |
B - 天下一魔力発電 |
User |
hiyokko2 |
Language |
C++14 (GCC 5.4.1) |
Score |
400 |
Code Size |
4600 Byte |
Status |
AC |
Exec Time |
5 ms |
Memory |
8320 KB |
Judge Result
Set Name |
All |
Score / Max Score |
400 / 400 |
Status |
|
Set Name |
Test Cases |
All |
01_sample_01, 01_sample_02, 01_sample_03, 02_manual_01, 02_manual_02, 02_manual_03, 10_random_00, 10_random_01, 10_random_02, 10_random_03, 10_random_04, 10_random_05, 10_random_06, 10_random_07, 10_random_08, 10_random_09, 10_random_10, 10_random_11, 10_random_12, 10_random_13, 10_random_14, 10_random_15, 10_random_16, 10_random_17, 10_random_18, 10_random_19, 20_max_00, 20_max_01, 20_max_02, 20_max_03, 20_max_04, 20_max_05 |
Case Name |
Status |
Exec Time |
Memory |
01_sample_01 |
AC |
1 ms |
384 KB |
01_sample_02 |
AC |
1 ms |
256 KB |
01_sample_03 |
AC |
1 ms |
384 KB |
02_manual_01 |
AC |
1 ms |
256 KB |
02_manual_02 |
AC |
1 ms |
384 KB |
02_manual_03 |
AC |
1 ms |
384 KB |
10_random_00 |
AC |
2 ms |
3328 KB |
10_random_01 |
AC |
1 ms |
384 KB |
10_random_02 |
AC |
2 ms |
3200 KB |
10_random_03 |
AC |
3 ms |
5888 KB |
10_random_04 |
AC |
1 ms |
384 KB |
10_random_05 |
AC |
1 ms |
768 KB |
10_random_06 |
AC |
2 ms |
3328 KB |
10_random_07 |
AC |
5 ms |
7808 KB |
10_random_08 |
AC |
3 ms |
5760 KB |
10_random_09 |
AC |
1 ms |
768 KB |
10_random_10 |
AC |
1 ms |
640 KB |
10_random_11 |
AC |
1 ms |
256 KB |
10_random_12 |
AC |
4 ms |
6528 KB |
10_random_13 |
AC |
4 ms |
6784 KB |
10_random_14 |
AC |
1 ms |
640 KB |
10_random_15 |
AC |
1 ms |
768 KB |
10_random_16 |
AC |
5 ms |
7552 KB |
10_random_17 |
AC |
4 ms |
7168 KB |
10_random_18 |
AC |
4 ms |
7552 KB |
10_random_19 |
AC |
4 ms |
7296 KB |
20_max_00 |
AC |
5 ms |
8320 KB |
20_max_01 |
AC |
5 ms |
8320 KB |
20_max_02 |
AC |
5 ms |
8320 KB |
20_max_03 |
AC |
5 ms |
8320 KB |
20_max_04 |
AC |
5 ms |
8320 KB |
20_max_05 |
AC |
5 ms |
8320 KB |