QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#428079 | #8772. House Deconstruction | ucup-team004# | WA | 320ms | 11480kb | C++20 | 6.6kb | 2024-06-01 17:19:34 | 2024-06-01 17:19:35 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf = 1E18;
template<class T>
constexpr T power(T a, i64 b) {
T res {1};
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr i64 mul(i64 a, i64 b, i64 p) {
i64 res = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template<i64 P>
struct MInt {
i64 x;
constexpr MInt() : x {0} {}
constexpr MInt(i64 x) : x {norm(x % getMod())} {}
static i64 Mod;
constexpr static i64 getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(i64 Mod_) {
Mod = Mod_;
}
constexpr i64 norm(i64 x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr i64 val() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
if (getMod() < (1ULL << 31)) {
x = x * rhs.x % int(getMod());
} else {
x = mul(x, rhs.x, getMod());
}
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
friend constexpr bool operator<(MInt lhs, MInt rhs) {
return lhs.val() < rhs.val();
}
};
template<>
i64 MInt<0>::Mod = 998244353;
constexpr int P = 998244353;
using Z = MInt<P>;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int x, n, m;
std::cin >> x >> n >> m;
const int N = n + m;
std::vector<int> p(N), d(N);
std::vector<char> t(N);
for (int i = 0; i < N; i++) {
std::cin >> p[i] >> t[i];
}
for (int i = 1; i < N; i++) {
d[i] = p[i] - p[i - 1];
}
d[0] = p[0] + x - p[N - 1];
auto work = [&](int k, int tp = 0) {
// std::cerr << "--- work " << k << "\n";
int c = k;
std::vector<std::pair<int, int>> seg;
if (tp) {
seg.reserve(m);
}
i64 res = 0;
int lo = 0, hi = 0;
std::deque<i64> del(n + 1);
del[0] = inf;
for (int i = 0; i < N; i++) {
res += 1LL * d[i] * std::abs(lo - c);
int v = std::min(n, std::max(0, c));
assert(v >= lo);
// std::cerr << "v : " << v << " d : " << d[i] << "\n";
del[v - lo] += 2 * d[i];
int cur = -d[i];
while (lo < n && cur < 0) {
int t = std::min<i64>(-cur, del[0]);
del[0] -= t;
cur += t;
if (cur == 0) {
break;
}
res += cur;
lo++;
del.pop_front();
}
// for (int j = 0; j <= n; j++) {
// dp[j] += 1LL * d[i] * std::abs(j - c);
// }
if (t[i] == 'P') {
c++;
} else {
// std::cerr << "dp: ";
// for (int j = 0; j <= n; j++) {
// std::cerr << dp[j] << " \n"[j == n];
// }
if (hi < lo) {
hi = lo;
}
while (hi < n && del[hi - lo] == 0) {
hi++;
}
del.push_front(0);
if (tp) {
seg.emplace_back(lo, hi);
}
// std::cerr << lo << " " << hi << " " << res << "\n";
// for (int j = n; j > 0; j--) {
// if (dp[j - 1] < dp[j]) {
// dp[j] = dp[j - 1];
// g[j] = g[j - 1];
// } else if (dp[j - 1] == dp[j]) {
// g[j] += g[j - 1];
// }
// }
}
}
i64 cur = 0;
for (int i = lo; i < n; i++) {
cur += del[i - lo];
res += cur;
}
if (!tp) {
return std::make_pair(res, Z(0));
}
std::vector<Z> g(n + 1, 1);
for (auto [l, r] : seg) {
if (r < n) {
g[r + 1] = g[r];
}
for (int i = r; i > l; i--) {
g[i] += g[i - 1];
}
}
return std::make_pair(res, g[n]);
};
int lo = -n, hi = n;
while (lo < hi) {
int m = lo + (hi - lo) / 2;
if (work(m).first < work(m + 1).first) {
hi = m;
} else {
lo = m + 1;
}
}
i64 ans = work(lo).first;
Z ways = 0;
for (int i = lo; ; i--) {
auto [a, b] = work(i, 1);
if (a != ans) {
break;
}
ways += b;
}
for (int i = lo + 1; ; i++) {
auto [a, b] = work(i, 1);
if (a != ans) {
break;
}
ways += b;
}
std::cout << ans << "\n";
std::cout << ways << "\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3568kb
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: 3568kb
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: 0
Accepted
time: 320ms
memory: 11480kb
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...
output:
262753966206 1
result:
ok 2 lines
Test #4:
score: -100
Wrong Answer
time: 234ms
memory: 10528kb
input:
1000000000 150000 200000 3087 P 3282 H 6407 H 6748 H 9535 H 10098 H 16470 H 22311 H 31968 H 32549 H 33037 P 36005 P 40284 H 52394 P 54922 H 55800 H 56582 H 57548 H 62155 P 63067 P 64237 H 66133 P 71351 P 75667 H 79089 P 79646 P 81144 H 91883 P 96680 H 102797 H 105388 H 105971 P 106057 P 107807 P 116...
output:
1212175277 1
result:
wrong answer 2nd lines differ - expected: '8', found: '1'