이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define Y8o "CollectingStamps3"
#define maxn (int) 205
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)
#define f first
#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
const ll inf = 1e18;
int n, L;
pii a[maxn];
namespace sub1 {
int dp[1 << 12][13][205];
int cost(int i, int j) {
pii x = a[i], y = a[j];
if(x.f > y.f) swap(x, y);
return min( y.f - x.f, L - y.f + x.f - 0 );
}
void solve() {
memset(dp, -0x3f, sizeof dp);
fi(i, 1, n) dp[(1 << (i - 1))][i][cost(0, i)] = cost(0, i) <= a[i].s;
fi(mask, 1, (1 << n) - 1) {
fi(i, 1, n) if(gb(mask, i - 1)) {
fi(j, 1, n) if(!gb(mask, j - 1)) {
int w = cost(i, j);
fi(t, 0, 200 - w) {
dp[mask ^ (1 << (j - 1))][j][t + w] =
max(dp[mask ^ (1 << (j - 1))][j][t + w], dp[mask][i][t] + (t + w <= a[j].s));
}
}
}
}
int ans = 0;
fi(i, 1, n) {
fi(t, 0, 200) {
ans = max(ans, dp[(1 << n) - 1][i][t]);
}
}
cout << ans;
}
}
namespace sub2 {
int cost(int i, int j) {
pii x = a[i], y = a[j];
if(x.f > y.f) swap(x, y);
return min( y.f - x.f, L - y.f + x.f - 0 );
}
ll dp[1 << 15][16][16];
void solve() {
fi(mask, 0, (1 << n) - 1) fi(i, 0, n) fi(sl, 0, n)
dp[mask][i][sl] = inf;
fi(i, 1, n) {
int w = cost(0, i), tt = w <= a[i].s;
dp[1 << (i - 1)][i][tt] = w;
}
fi(mask, 1, (1 << n) - 1) {
fi(i, 1, n) if(gb(mask, i - 1)) {
fi(j, 1, n) if(!gb(mask, j - 1)) {
int w = cost(i, j);
fi(sl, 0, n) {
int tt = dp[mask][i][sl] + w <= a[j].s;
dp[mask ^ (1 << (j - 1))][j][sl + tt] =
min(dp[mask ^ (1 << (j - 1))][j][sl + tt], dp[mask][i][sl] + w);
}
}
}
}
int ans = 0;
fi(i, 1, n) fi(sl, 0, n) if(dp[(1 << n) - 1][i][sl] != inf)
ans = max(ans, sl);
cout << ans;
}
}
namespace sub {
int cost(int i, int j) {
pii x = a[i], y = a[j];
if(x.f > y.f) swap(x, y);
return min( y.f - x.f, L - y.f + x.f - 0 );
}
ll dp[maxn][maxn][maxn][2];
void solve() {
fi(i, 0, n + 2) fi(j, 0, n + 2) fi(sl, 0, n)
dp[i][j][sl][0] = dp[i][j][sl][1] = inf;
dp[0][n + 1][0][0] = dp[0][n + 1][0][1] = 0;
fid(len, n + 1, 0) {
fi(i, 0, (n + 1) - len + 1) {
int j = i + len - 1;
fi(sl, 0, n) {
if(i > 0) {
int tt = dp[i - 1][j][sl][0] + cost(i - 1, i) <= a[i].s;
dp[i][j][sl + tt][0] = min(dp[i][j][sl + tt][0], dp[i - 1][j][sl][0] + cost(i - 1, i));
tt = dp[i - 1][j][sl][1] + cost(j, i) <= a[i].s;
dp[i][j][sl + tt][0] = min(dp[i][j][sl + tt][0], dp[i - 1][j][sl][1] + cost(j, i));
}
int tt = dp[i][j + 1][sl][0] + cost(i, j) <= a[j].s;
dp[i][j][sl + tt][1] = min(dp[i][j][sl + tt][1], dp[i][j + 1][sl][0] + cost(i, j));
tt = dp[i][j + 1][sl][1] + cost(j + 1, j) <= a[j].s;
dp[i][j][sl + tt][1] = min(dp[i][j][sl + tt][1], dp[i][j + 1][sl][1] + cost(j + 1, j));
}
}
}
int ans = 0;
fi(i, 0, n) {
int j = i + 1;
fi(sl, 0, n) if(dp[i][j][sl][0] != inf || dp[i][j][sl][1] != inf) {
ans = max(ans, sl);
}
}
cout << ans;
}
}
void solve() {
cin >> n >> L ;
fi(i, 1, n) cin >> a[i].f;
fi(i, 1, n) cin >> a[i].s;
if(n <= 12 && L <= 200) return sub1 :: solve(), void();
if(n <= 15) return sub2 :: solve(), void();
return sub :: solve(), void();
}
int main() {
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
ho_t3.cpp: In function 'void sub1::solve()':
ho_t3.cpp:54:39: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
54 | fi(i, 1, n) if(gb(mask, i - 1)) {
| ~~^~~
ho_t3.cpp:6:25: note: in definition of macro 'gb'
6 | #define gb(i, j) ((i >> j) & 1)
| ^
ho_t3.cpp:55:44: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
55 | fi(j, 1, n) if(!gb(mask, j - 1)) {
| ~~^~~
ho_t3.cpp:6:25: note: in definition of macro 'gb'
6 | #define gb(i, j) ((i >> j) & 1)
| ^
ho_t3.cpp: In function 'void sub2::solve()':
ho_t3.cpp:95:39: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
95 | fi(i, 1, n) if(gb(mask, i - 1)) {
| ~~^~~
ho_t3.cpp:6:25: note: in definition of macro 'gb'
6 | #define gb(i, j) ((i >> j) & 1)
| ^
ho_t3.cpp:96:44: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
96 | fi(j, 1, n) if(!gb(mask, j - 1)) {
| ~~^~~
ho_t3.cpp:6:25: note: in definition of macro 'gb'
6 | #define gb(i, j) ((i >> j) & 1)
| ^
ho_t3.cpp: In function 'void iof()':
ho_t3.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |