QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#882936 | #8772. House Deconstruction | ningago | RE | 0ms | 16200kb | C++14 | 9.1kb | 2025-02-05 13:39:58 | 2025-02-05 13:39:58 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <cctype>
#include <set>
namespace uvu
{
#define LOCAL ____________DONT_DEFINE_ME____________
// #define ll long long
// #define inf 0x3f3f3f3f
#define int long long
#define inf 0x3f3f3f3f3f3f3f3fll
#define infll 0x3f3f3f3f3f3f3f3fll
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define gline debug("now is #%d\n", __LINE__)
#define pii std::pair <int, int>
#define mkp std::make_pair
#define fi first
#define se second
char _ST_;
const int BUFSIZE = (1 << 20);
char ibuf[BUFSIZE], *iS = ibuf, *iT = ibuf;
char obuf[BUFSIZE], *oS = obuf, *oT = obuf + BUFSIZE;
char getc()
{
#ifdef LOCAL
return getchar();
#else
if(iS == iT) iT = (iS = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return iS == iT ? EOF : *iS++;
#endif
#define getchar ERR
}
void Flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; }
struct Flusher { ~Flusher(){ Flush(); } }iamflusher;
void putc(char c)
{
#ifdef LOCAL
putchar(c);
#else
*oS++ = c;
if(oS == oT) Flush();
#endif
#define putchar ERR
}
template <typename T = int> T read()
{
T x = 0, f = 1; char c = getc();
for(; !isdigit(c); c = getc()) if(c == '-') f = -1;
for(; isdigit(c); c = getc()) x = (x << 3) + (x << 1) + (c ^ 48);
return x * f;
}
template <typename T> void print(T x, char c)
{
static int sta[BUFSIZE], top;
top = 0;
if(x < 0) putc('-'), x = -x;
if(!x) sta[top = 1] = 0;
for(; x; x /= 10) sta[++top] = x % 10;
for(; top; ) putc(sta[top--] ^ 48);
if(c) putc(c);
}
int readstr(char *s, int base)
{
int idx = base - 1; char c = getc();
for(; !(isdigit(c) || isalpha(c) || c == '(' || c == '?'); c = getc());
for(; isdigit(c) || isalpha(c) || c == '(' || c == '?' ; c = getc()) s[++idx] = c;
return idx - base + 1;
}
void printf(const char *s) { for(; *s; s++) putc(*s); }
template <typename T, typename ... Args>
void printf(const char *s, T x, Args ... rest)
{
for(; *s; s++)
{
if(*s != '%') { putc(*s); continue; }
s++; if(*s == 'd') print(x, 0);
else if(*s == 'c') putc(x);
printf(s + 1, rest ...);
return;
}
}
template <typename T> void ckmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> void ckmin(T &x, T y) { x = x < y ? x : y; }
#define mod 998244353
// #define mod 1000000007
int sm(int x) { return x >= mod ? x - mod : x; }
void plus_(int &x, int y) { x = (x + y >= mod ? x + y - mod : x + y); }
void mul_(int &x, int y) { x = 1ll * x * y % mod; }
int ksm(int a, int b) { int res = 1; for(; b; b >>= 1, mul_(a, a)) if(b & 1) mul_(res, a); return res; }
#define N 200010
int n, m, A, B;
int op_[N], d[N], x_[N];
char s[2];
struct node
{
int mn, cnt;
void operator += (node B)
{
if(mn < B.mn) return;
if(mn > B.mn) { mn = B.mn, cnt = B.cnt; return; }
plus_(cnt, B.cnt);
}
node operator + (int x)
{
return (node){mn + x, cnt};
}
};
node dp[2][N << 1];
int op;
const int DT = 200000;
std::deque <pii> ql, qr;
int pos, base, val;
int sta[N], top;
int calc(int mid)
{
// printf("mid = %d\n", mid);
// int lim = n + n;
// for(int i = -lim; i <= lim; i++) dp[0][i + DT] = dp[1][i + DT] = (node){inf, 0};
// dp[op = 0][mid + DT] = (node){0, 1};
// for(int i = 1; i <= n; i++)
// {
// op ^= 1;
// for(int j = -lim; j <= lim; j++) dp[op][j + DT] = (node){inf, 0};
// for(int j = -lim; j <= lim; j++)
// {
// if(op_[i] == 0) dp[op][j + DT] += dp[op ^ 1][j + DT] + abs(j) * d[i];
// else if(op_[i] == 1 && j != -lim) dp[op][j + DT] += dp[op ^ 1][j - 1 + DT] + abs(j) + abs(j - 1) * (d[i] - 1);
// else if(op_[i] == 2)
// {
// dp[op][j + DT] += dp[op ^ 1][j + DT] + abs(j);
// if(j != lim) dp[op][j + DT] += dp[op ^ 1][j + 1 + DT] + abs(j) + abs(j + 1) * (d[i] - 1);
// }
// }
// }
// return dp[op][mid + DT].mn;
pos = mid, base = 0, val = 1;
ql.clear(), qr.clear();
auto out = [&]() -> void
{
printf("ql : "); for(pii x : ql) printf("[%d %d] ", x.fi, x.se); putc('\n');
printf("pos = %d, base = %d, val = %d\n", pos, base, val);
printf("qr : "); for(pii x : qr) printf("[%d %d] ", x.fi, x.se); putc('\n');
};
for(int i = 1; i <= n; i++)
{
auto flush = [&](int v) -> void
{
// while(pos > 0 && !ql.empty() && ql.back().fi <= v)
// {
// base += ql.back().fi * abs(pos);
// v -= ql.back().fi;
// --pos;
// qr.push_front(mkp(0, val));
// val = ql.back().se;
// ql.pop_back();
// }
// while(pos < 0 && !qr.empty() && qr.front().fi <= v)
// {
// base += qr.front().fi * abs(pos);
// v -= qr.front().fi;
// ++pos;
// ql.push_back(mkp(0, val));
// val = qr.front().se;
// qr.pop_front();
// }
while(pos > 0 && v)
{
int t = v;
if(!ql.empty()) ckmin(t, ql.back().fi);
v -= t;
base += pos * t;
int len = ql.size();
if(0 <= pos - len)
{
if(!ql.empty()) ql.back().fi -= t;
if(!qr.empty()) qr.front().fi += t;
}
else
{
ql.back().fi -= t;
ql[len - pos - 1].fi += 2 * t;
if(!qr.empty()) qr.front().fi += t;
}
if(!ql.empty() && ql.back().fi == 0)
{
pos--;
qr.push_front(mkp(0, val));
val = ql.back().se;
ql.pop_back();
}
}
while(pos < 0 && v)
{
int t = v;
if(!qr.empty()) ckmin(t, qr.front().fi);
v -= t;
base -= pos * t;
int len = qr.size();
if(pos + len <= 0)
{
if(!qr.empty()) qr.front().fi -= t;
if(!ql.empty()) ql.back().fi += t;
}
else
{
qr.front().fi -= t;
qr[-pos].fi += 2 * t;
if(!ql.empty()) ql.back().fi += t;
}
if(!qr.empty() && qr.front().fi == 0)
{
pos++;
ql.push_back(mkp(0, val));
val = qr.front().se;
qr.pop_front();
}
}
if(pos == 0)
{
if(!ql.empty()) ql.back().fi += v;
if(!qr.empty()) qr.front().fi += v;
}
// else if(0 < pos)
// {
// base += pos * v;
// int len = ql.size();
// if(0 <= pos - len)
// {
// if(!ql.empty()) ql.back().fi -= v;
// if(!qr.empty()) qr.front().fi += v;
// }
// else
// {
// ql.back().fi -= v;
// ql[len - pos - 1].fi += 2 * v;
// if(!qr.empty()) qr.front().fi += v;
// }
// }
// else if(pos < 0)
// {
// base -= pos * v;
// int len = qr.size();
// if(pos + len <= 0)
// {
// if(!qr.empty()) qr.front().fi -= v;
// if(!ql.empty()) ql.back().fi += v;
// }
// else
// {
// qr.front().fi -= v;
// qr[-pos].fi += 2 * v;
// if(!ql.empty()) ql.back().fi += v;
// }
// }
};
auto moveright = [&]() -> void
{
pos++;
};
auto moveleft = [&]() -> void
{
top = 0;
while(!ql.empty() && !ql.back().fi) sta[++top] = ql.back().se, ql.pop_back();
std::sort(sta + 1, sta + 1 + top);
sta[++top] = val;
pos -= top;
while(!qr.empty() && !qr.front().fi) sta[++top] = qr.front().se, qr.pop_front();
sta[0] = 0;
for(int i = 1; i <= top; i++) plus_(sta[i - 1], sta[i]);
for(int i = top; i >= 1; i--) qr.push_front(mkp(0, sta[i]));
val = sta[0];
};
flush(d[i] - 1);
// printf("i = %d, flush(%d)\n", i, d[i] - 1), out();
if(op_[i] == 1) moveright();//, printf("moveright\n"), out();
if(op_[i] == 2) moveleft();//, printf("moveleft\n"), out();
flush(1);
// printf("flush(1)\n"), out();
}
// out();
int dt = 0;
while(pos < mid)
{
if(qr.empty()) debug("???\n");
++pos;
dt += qr.front().fi;
base += dt;
ql.push_back(mkp(-qr.front().fi, val));
val = qr.front().se;
qr.pop_front();
}
while(mid < pos)
{
if(ql.empty()) debug("???\n");
--pos;
dt += ql.back().fi;
base += dt;
qr.push_front(mkp(-ql.back().fi, val));
val = ql.back().se;
ql.pop_back();
}
while(!ql.empty() && !ql.back().fi) plus_(val, ql.back().se), ql.pop_back();
while(!qr.empty() && !qr.front().fi) plus_(val, qr.front().se), qr.pop_back();
// out();
// printf("return [%d %d]\n", base, val);
// exit(1);
return base;
}
void solve()
{
// memset(h, idx = -1, sizeof(h));
m = read(), A = read(), B = read(), n = A + B;
for(int i = 1; i <= n; i++)
{
x_[i] = read(); readstr(s, 0);
op_[i] = (s[0] == 'P' ? 1 : 2);
}
// printf("x[%d] = %d, m = %d\n", n, x_[n], m);
x_[0] = x_[n] - m;
for(int i = 1; i <= n; i++) d[i] = x_[i] - x_[i - 1];
pii res = mkp(calc(n), n);
for(int l = -n, r = n, mid, lx, rx; l <= r; )
{
mid = (l + r) >> 1;
lx = calc(mid);
rx = calc(mid + 1);
ckmin(res, mkp(lx, mid));
ckmin(res, mkp(rx, mid + 1));
if(lx <= rx) r = mid - 1;
else l = mid + 1;
}
int ans = 0;
print(res.fi, '\n');
calc(res.se); ans = val;
if(res.fi == calc(res.se - 1)) plus_(ans, val);
if(res.fi == calc(res.se + 1)) plus_(ans, val);
print(ans, '\n');
}
void init()
{
}
char _ED_;
void mian()
{
debug("%.3f MB\n", abs(&_ST_ - &_ED_) / 1024.0 / 1024);
init(); int T = 1;
for(; T; solve(), T--);
}
#ifdef int
#undef int
#endif
}
int main()
{
uvu::mian(); return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14156kb
input:
6 2 4 1 P 2 H 3 P 4 H 5 H 6 H
output:
2 3
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 16200kb
input:
1000000000 2 4 1 P 6 H 31415926 H 999999998 H 999999999 H 1000000000 P
output:
4 1
result:
ok 2 lines
Test #3:
score: -100
Runtime Error
input:
1000000000 199999 200000 3087 P 3282 H 6407 H 6748 P 9535 P 10098 H 16470 H 22311 H 31045 H 31968 H 32549 H 33037 P 36005 P 36308 H 40284 P 46224 H 52394 P 54922 H 55800 P 56582 H 57548 H 62155 P 63067 P 64237 H 66133 P 71351 P 75667 P 79089 P 79646 P 81144 H 91883 P 96680 H 102797 H 105388 H 105971...