QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#530552 | #9220. Bus Analysis | ucup-team4435# | AC ✓ | 1584ms | 252372kb | C++20 | 9.4kb | 2024-08-24 16:32:27 | 2024-08-24 16:32:31 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
int Bit(int mask, int b) { return (mask >> b) & 1; }
template<class T>
bool ckmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool ckmax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}
// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
--l;
while (r - l > 1) {
T mid = l + (r - l) / 2;
if (predicat(mid)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
return FindFirstTrue(l, r, predicat) - 1;
}
template<typename T>
int normalize(T value, int mod) {
if (value < -mod || value >= 2 * mod) value %= mod;
if (value < 0) value += mod;
if (value >= mod) value -= mod;
return value;
}
template<int mod>
struct static_modular_int {
using mint = static_modular_int<mod>;
int value;
static_modular_int() : value(0) {}
static_modular_int(const mint &x) : value(x.value) {}
template<typename T, typename U = std::enable_if_t<std::is_integral<T>::value>>
static_modular_int(T value) : value(normalize(value, mod)) {}
template<typename T>
mint power(T degree) const {
degree = normalize(degree, mod - 1);
mint prod = 1, a = *this;
for (; degree > 0; degree >>= 1, a *= a)
if (degree & 1)
prod *= a;
return prod;
}
mint inv() const {
return power(-1);
}
mint &operator=(const mint &x) {
value = x.value;
return *this;
}
mint &operator+=(const mint &x) {
value += x.value;
if (value >= mod) value -= mod;
return *this;
}
mint &operator-=(const mint &x) {
value -= x.value;
if (value < 0) value += mod;
return *this;
}
mint &operator*=(const mint &x) {
value = int64_t(value) * x.value % mod;
return *this;
}
mint &operator/=(const mint &x) {
return *this *= x.inv();
}
friend mint operator+(const mint &x, const mint &y) {
return mint(x) += y;
}
friend mint operator-(const mint &x, const mint &y) {
return mint(x) -= y;
}
friend mint operator*(const mint &x, const mint &y) {
return mint(x) *= y;
}
friend mint operator/(const mint &x, const mint &y) {
return mint(x) /= y;
}
mint &operator++() {
++value;
if (value == mod) value = 0;
return *this;
}
mint &operator--() {
--value;
if (value == -1) value = mod - 1;
return *this;
}
mint operator++(int) {
mint prev = *this;
value++;
if (value == mod) value = 0;
return prev;
}
mint operator--(int) {
mint prev = *this;
value--;
if (value == -1) value = mod - 1;
return prev;
}
mint operator-() const {
return mint(0) - *this;
}
bool operator==(const mint &x) const {
return value == x.value;
}
bool operator!=(const mint &x) const {
return value != x.value;
}
bool operator<(const mint &x) const {
return value < x.value;
}
template<typename T>
explicit operator T() {
return value;
}
friend std::istream &operator>>(std::istream &in, mint &x) {
std::string s;
in >> s;
x = 0;
for (const auto c: s)
x = x * 10 + (c - '0');
return in;
}
friend std::ostream &operator<<(std::ostream &out, const mint &x) {
return out << x.value;
}
static int primitive_root() {
if constexpr (mod == 1'000'000'007) return 5;
if constexpr (mod == 998'244'353) return 3;
if constexpr (mod == 786433) return 10;
static int root = -1;
if (root != -1)
return root;
std::vector<int> primes;
int value = mod - 1;
for (int i = 2; i * i <= value; i++)
if (value % i == 0) {
primes.push_back(i);
while (value % i == 0)
value /= i;
}
if (value != 1) primes.push_back(value);
for (int r = 2;; r++) {
bool ok = true;
for (auto p: primes) {
if ((mint(r).power((mod - 1) / p)).value == 1) {
ok = false;
break;
}
}
if (ok) return root = r;
}
}
};
constexpr int MOD = 1'000'000'007;
// constexpr int MOD = 998'244'353;
using mint = static_modular_int<MOD>;
const int INFi = 2e9;
const ll INF = 2e18;
const int LG = 31;
const int N = 1e4;
int n;
vi t;
int jump[N][2];
int tmp[N];
int pos;
const int SZ = 76;
struct State {
ar<int, SZ> dp;
int MakeGood() {
if (pos != -1) {
ckmin(dp[jump[pos][0] - 1], 1);
ckmin(dp[jump[pos][1] - 1], 3);
}
// dp[1] = min(dp[1], 1);
for(int i = 0; i + 1 < SZ; ++i) dp[i + 1] = min(dp[i + 1], dp[i] + 1);
for (int i = SZ - 1; i >= 1; --i) dp[i - 1] = min(dp[i - 1], dp[i]);
int x = dp[0];
for (int i = 1; i < SZ; ++i) dp[i - 1] = dp[i] - x;
dp[SZ - 1] = INFi;
return x;
}
};
bool operator<(const State &a, const State &b) {
return a.dp < b.dp;
}
bool operator==(const State &a, const State &b) {
return a.dp == b.dp;
}
const int M = 1e7;
pair<mint, mint> dp[2][M];
int t_upd[M];
void solve() {
cin >> n;
// n = 1000;
t.resize(n);
rep(i, n) cin >> t[i]; //t[i] = rand() % (2 * n); //cin >> t[i]; //t[i] = i; //cin >> t[i]; //t[i] = i; //cin >> t[i]; //t[i] = i;
// sort(all(t));
// t.resize(unique(all(t)) - t.begin());
n = t.size();
jump[n][0] = jump[n][1] = n;
for (int i = n - 1; i >= 0; --i) {
rep(j, 2) jump[i][j] = jump[i + 1][j];
while (t[jump[i][0] - 1] >= t[i] + 20) jump[i][0]--;
while (t[jump[i][1] - 1] >= t[i] + 75) jump[i][1]--;
}
for (int i = n; i >= 0; --i) {
rep(j, 2) jump[i][j] -= i;
}
int sz = 0;
vector<map<pair<int, int>, ar<pair<int, int>, 2>>> g;
map<State, int> index;
vector<State> states;
auto Add = [&](State s) {
int value = s.MakeGood();
int i = -1;
if (index.contains(s)) {
i = index[s];
} else {
states.push_back(s);
i = index.size();
index[s] = i;
sz++;
assert(sz < M);
g.emplace_back();
}
return make_pair(i, value);
};
pos = 0;
vi nxt;
{
State s;
rep(i, SZ) s.dp[i] = INFi;
s.dp[0] = 0;
pos = -1;
auto [v, _] = Add(s);
dp[0][v].first++;
nxt.push_back(v);
}
int tq = 0;
int who = 0;
for (int i = 0; i < n; ++i) {
who ^= 1;
pos = i;
tq++;
pair<int, int> cur = {jump[i][0], jump[i][1]};
sort(all(nxt));
nxt.resize(unique(all(nxt)) - nxt.begin());
vi nxt2;
nxt2.reserve(nxt.size());
for(auto v : nxt) {
auto val = dp[who^1][v];
if (!val.first && !val.second) continue;
if (!g[v].contains(cur)) {
g[v][cur][0] = Add(states[v]);
{
auto s2 = states[v];
ckmin(s2.dp[0], 0);
g[v][cur][1] = Add(s2);
}
}
ar<pair<int, int>, 2> to = g[v][cur];
rep(q, 2) {
int j = to[q].first;
int value = to[q].second;
if (t_upd[j] < tq) {
t_upd[j] = tq;
dp[who][j] = {0, 0};
}
dp[who][j].first += val.first;
dp[who][j].second += val.second + val.first * value;
nxt2.push_back(j);
}
}
swap(nxt, nxt2);
}
sort(all(nxt));
nxt.resize(unique(all(nxt)) - nxt.begin());
mint ans = 0;
for(auto &i : nxt) ans += dp[who][i].second;
cout << ans * 2 << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(12) << fixed;
int t = 1;
// cin >> t;
rep(i, t) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 16ms
memory: 159972kb
input:
3 1 8 20
output:
14
result:
ok 1 number(s): "14"
Test #2:
score: 0
Accepted
time: 21ms
memory: 160536kb
input:
5 25 45 65 85 1000000000
output:
156
result:
ok 1 number(s): "156"
Test #3:
score: 0
Accepted
time: 142ms
memory: 173764kb
input:
1000 2 7 9 12 14 17 18 21 22 28 29 33 34 35 37 38 44 45 46 50 58 59 63 66 71 72 75 76 77 78 80 81 83 84 87 92 100 101 107 108 109 112 114 116 118 123 124 131 142 143 144 145 148 150 151 152 153 155 157 158 165 167 168 169 171 176 182 185 190 192 198 202 204 205 212 213 223 224 225 226 229 231 240 24...
output:
932594593
result:
ok 1 number(s): "932594593"
Test #4:
score: 0
Accepted
time: 79ms
memory: 173328kb
input:
200 1 2 4 9 10 11 12 15 16 17 18 19 22 24 27 28 33 36 37 39 43 44 45 47 48 49 50 51 52 56 60 61 62 63 68 70 71 72 73 74 78 80 81 84 86 92 93 98 99 100 102 105 107 108 110 111 114 115 116 122 123 125 129 132 133 135 137 138 139 141 142 143 144 149 150 151 152 153 154 155 159 160 165 171 172 174 176 1...
output:
422301620
result:
ok 1 number(s): "422301620"
Test #5:
score: 0
Accepted
time: 20ms
memory: 161224kb
input:
1000 999975020 999975030 999975050 999975056 999975085 999975118 999975207 999975213 999975240 999975307 999975332 999975344 999975356 999975384 999975405 999975468 999975499 999975558 999975559 999975571 999975631 999975664 999975680 999975689 999975731 999975738 999975765 999975831 999975838 99997...
output:
335758758
result:
ok 1 number(s): "335758758"
Test #6:
score: 0
Accepted
time: 11ms
memory: 161032kb
input:
3 1 8 20
output:
14
result:
ok 1 number(s): "14"
Test #7:
score: 0
Accepted
time: 19ms
memory: 159936kb
input:
1000 214 216 381 497 539 645 772 927 1211 1238 1239 1298 1403 1426 1437 1504 1515 1648 1800 1816 1946 2008 2038 2066 2144 2173 2236 2253 2291 2574 2632 2643 2658 2671 2689 2886 2910 2948 3002 3033 3039 3057 3077 3188 3240 3322 3331 3449 3467 3578 3730 3796 3897 3973 4152 4160 4199 4331 4386 4429 465...
output:
877690181
result:
ok 1 number(s): "877690181"
Test #8:
score: 0
Accepted
time: 12ms
memory: 160088kb
input:
1 963837006
output:
2
result:
ok 1 number(s): "2"
Test #9:
score: 0
Accepted
time: 15ms
memory: 160748kb
input:
999 17 234 337 379 449 607 646 703 716 878 887 898 901 942 964 1075 1198 1356 1432 1546 1547 1619 1668 1769 1818 1826 1881 1900 1960 2017 2088 2266 2273 2339 2376 2377 2396 2399 2438 2461 2484 2567 2620 2661 2680 2791 2881 3040 3081 3082 3094 3102 3205 3218 3225 3227 3239 3250 3331 3393 3425 3530 35...
output:
651884156
result:
ok 1 number(s): "651884156"
Test #10:
score: 0
Accepted
time: 480ms
memory: 208096kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...
output:
754964156
result:
ok 1 number(s): "754964156"
Test #11:
score: 0
Accepted
time: 59ms
memory: 166864kb
input:
1000 1 2 5 12 13 16 19 21 24 40 42 50 51 54 60 61 63 65 69 72 83 100 106 117 121 122 132 133 135 144 149 154 156 157 158 159 163 172 187 190 194 195 196 200 204 221 224 225 236 237 238 239 242 248 250 251 257 258 264 269 272 279 284 286 288 294 298 301 303 322 324 326 328 347 348 358 363 367 368 376...
output:
736216287
result:
ok 1 number(s): "736216287"
Test #12:
score: 0
Accepted
time: 20ms
memory: 160800kb
input:
10 2 5 8 10 11 15 20 32 34 48
output:
3806
result:
ok 1 number(s): "3806"
Test #13:
score: 0
Accepted
time: 35ms
memory: 160564kb
input:
1000 3092896 4096697 4935574 5665657 6717242 12350017 13784643 14890236 15103239 16083201 16158442 17406219 18377269 19146618 20149007 20444388 20799716 21703919 22502750 26066931 27425220 27610093 30584293 30942406 33020849 33381138 34889813 35505301 35711460 36618608 37636454 42073851 43834284 441...
output:
423205184
result:
ok 1 number(s): "423205184"
Test #14:
score: 0
Accepted
time: 488ms
memory: 211080kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 100 101 10...
output:
167925937
result:
ok 1 number(s): "167925937"
Test #15:
score: 0
Accepted
time: 20ms
memory: 161368kb
input:
1000 123123123 123123143 123123163 123123183 123123203 123123223 123123243 123123263 123123283 123123303 123123323 123123343 123123363 123123383 123123403 123123423 123123443 123123463 123123483 123123503 123123523 123123543 123123563 123123583 123123603 123123623 123123643 123123663 123123683 12312...
output:
152353232
result:
ok 1 number(s): "152353232"
Test #16:
score: 0
Accepted
time: 24ms
memory: 161452kb
input:
5 25 45 65 85 1000000000
output:
156
result:
ok 1 number(s): "156"
Test #17:
score: 0
Accepted
time: 1300ms
memory: 246128kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 76 77 79 80 81 82 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 103 104 105...
output:
506306982
result:
ok 1 number(s): "506306982"
Test #18:
score: 0
Accepted
time: 26ms
memory: 162308kb
input:
1000 6 15 22 23 29 36 55 56 63 75 84 98 100 107 114 119 124 126 135 150 155 156 158 159 162 168 179 180 193 196 197 202 204 214 215 218 229 242 244 248 251 253 255 265 271 273 286 288 302 306 324 334 368 380 388 409 425 473 476 489 525 526 534 540 554 556 562 566 570 573 574 592 621 636 640 645 653 ...
output:
272405461
result:
ok 1 number(s): "272405461"
Test #19:
score: 0
Accepted
time: 21ms
memory: 161788kb
input:
19 999999810 999999814 999999829 999999838 999999839 999999843 999999849 999999853 999999883 999999889 999999896 999999907 999999911 999999925 999999926 999999946 999999957 999999987 999999993
output:
5337088
result:
ok 1 number(s): "5337088"
Test #20:
score: 0
Accepted
time: 1584ms
memory: 252372kb
input:
999 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 28 29 30 31 32 33 34 35 38 39 40 41 42 43 45 46 47 49 50 51 52 54 55 56 57 59 60 61 62 64 65 66 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 85 86 88 89 90 91 92 93 94 95 96 97 99 100 101 103 105 106 107 108 109 110 111 112 11...
output:
571215215
result:
ok 1 number(s): "571215215"
Test #21:
score: 0
Accepted
time: 413ms
memory: 202512kb
input:
1000 123121000 123121001 123121002 123121005 123121012 123121013 123121018 123121020 123121021 123121024 123121028 123121029 123121031 123121034 123121035 123121036 123121037 123121038 123121043 123121044 123121045 123121046 123121047 123121048 123121050 123121051 123121052 123121055 123121057 12312...
output:
250442238
result:
ok 1 number(s): "250442238"
Extra Test:
score: 0
Extra Test Passed