이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Pantyhose(black) + glasses = infinity
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "data"
const int MAX_N = 402;
const int INF = 1e9;
int n;
map<tuple<int, int, int, int>, int> f;
vector<int> a[3];
void readInput() {
cin >> n;
for (int i=1; i<=n; ++i) {
char c;
cin >> c;
if (c=='R')
a[0].push_back(i);
else if (c=='G')
a[1].push_back(i);
else
a[2].push_back(i);
}
}
int bisect(int idx, int curPos, int bound) {
int l = 0, r = bound;
for (int mid=(l+r)/2; mid!=l && r!=mid; mid=(l+r)/2) {
if (a[idx][mid]>curPos)
l = mid;
else
r = mid;
}
for (int i=l; i<=r; ++i) {
if (a[idx][i]>curPos)
return bound - i + 1;
}
return 0;
}
int cost(int curPos, int newPos, int i, int j, int k) {
int realPos = curPos + bisect(0, curPos, i-1)
+ bisect(1, curPos, j-1) + bisect(2, curPos, k-1);
return realPos - newPos;
}
int dp(int cur, int i, int j, int k, int prv) {
if (cur==n+1)
return 0;
if (f.count(make_tuple(i, j, k, prv)))
return f[make_tuple(i, j, k, prv)];
int res = INF;
// if (i<a[0].size())
// debug(cost(a[0][i], cur, i, j, k));
if (i<a[0].size() && prv!=0) {
res = min(res, dp(cur+1, i+1, j, k, 0) +
cost(a[0][i], cur, i, j, k));
}
if (j<a[1].size() && prv!=1) {
res = min(res, dp(cur+1, i, j+1, k, 1) +
cost(a[1][j], cur, i, j, k));
}
if (k!=a[2].size() && prv!=2) {
res = min(res, dp(cur+1, i, j, k+1, 2) +
cost(a[2][k], cur, i, j, k));
}
return f[make_tuple(i, j, k, prv)] = res;
}
void solve() {
// debug(dp(10, 3, 4, 2, 1));
int tmp = dp(1, 0, 0, 0, 3);
if (tmp==INF)
cout << -1;
else
cout << tmp;
}
int main() {
#ifdef GLASSES_GIRL
freopen(FILE_NAME".in", "r", stdin);
freopen(FILE_NAME".out", "w", stdout);
#endif
ios::sync_with_stdio(0); cin.tie(0);
readInput();
//debug(cost(4, 1, 0, 0, 0));
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
joi2019_ho_t3.cpp: In function 'int dp(int, int, int, int, int)':
joi2019_ho_t3.cpp:71:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i<a[0].size() && prv!=0) {
~^~~~~~~~~~~~
joi2019_ho_t3.cpp:75:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j<a[1].size() && prv!=1) {
~^~~~~~~~~~~~
joi2019_ho_t3.cpp:79:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (k!=a[2].size() && prv!=2) {
~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |