QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#395092 | #2697. Exhausting Errands | kevinyang# | AC ✓ | 2ms | 3924kb | C++20 | 8.8kb | 2024-04-21 03:57:23 | 2024-04-21 03:57:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
/* Macros {{{ */
/* A lot of this is from some of Benq's submissions
[https://codeforces.com/profile/Benq]
Ugly af to the eyes, but with vim fold its barable
Hopefully c++20 concepts can make all this stuff must cleaner */
/* Basics {{{ */
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define mp make_pair
#define fi first
#define se second
#define arr array
#define ve vector
using vi = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpi = vector<pi>;
using vpll = vector<pll>;
using vpld = vector<pld>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vvld = vector<vld>;
using vvpi = vector<vpi>;
using vvpll = vector<vpll>;
using vvpld = vector<vpld>;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz size()
#define rsz(a) resize(a)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define For(i, a, b) for (int i = a; i < b; ++i)
#define Rof(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define rep(a) For(_, 0, a)
#define each(a, x) for (auto &a : x)
#define reach(a, x) for (auto a = x.rbegin(); a != x.rend(); ++a)
template <typename T, typename U>
inline void cmin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void cmax(T &x, U y) {
if (x < y) x = y;
}
/*}}}*/
/* IO {{{ */
/* Template Macros {{{ */
#define tcT template <class T
#define tcTU tcT, class U
#define tcTUU tcT, class... U
/*}}}*/
inline namespace Helpers { /*{{{*/
tcT, class = void > struct is_iterable : false_type {};
tcT > struct is_iterable<
T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>>
: true_type {};
tcT > constexpr bool is_iterable_v = is_iterable<T>::value;
tcT, class = void > struct is_readable : false_type {};
tcT > struct is_readable<T, typename std::enable_if_t<is_same_v<
decltype(cin >> declval<T &>()), istream &>>>
: true_type {};
tcT > constexpr bool is_readable_v = is_readable<T>::value;
tcT, class = void > struct is_printable : false_type {};
tcT > struct is_printable<T, typename std::enable_if_t<is_same_v<
decltype(cout << declval<T>()), ostream &>>>
: true_type {};
tcT > constexpr bool is_printable_v = is_printable<T>::value;
} /* namespace Helpers */
/*}}}*/
inline namespace Input { /*{{{*/
tcT > constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;
tcTUU > void re(T &t, U &...u);
tcTU > void re(pair<T, U> &p); /* pairs */
/* re: read{{{ */
tcT > typename enable_if<is_readable_v<T>, void>::type re(T &x) {
cin >> x;
} /* default */
tcT > typename enable_if<needs_input_v<T>, void>::type re(
T &i); // vectors, arrays, etc...
tcTU > void re(pair<T, U> &p) { re(p.fi, p.se); } // pairs
tcT > typename enable_if<needs_input_v<T>, void>::type re(T &i) {
each(x, i) re(x);
}
tcTUU > void re(T &t, U &...u) {
re(t);
re(u...);
} /* read multiple}}} */
/* rv: resize and read vectors{{{ */
void rv(size_t) {}
tcTUU > void rv(size_t N, ve<T> &t, U &...u);
template <class... U>
void rv(size_t, size_t N2, U &...u);
tcTUU > void rv(size_t N, ve<T> &t, U &...u) {
t.rsz(N);
re(t);
rv(N, u...);
}
template <class... U>
void rv(size_t, size_t N2, U &...u) {
rv(N2, u...);
} /*}}}*/
/* dumb shortcuts to read in ints{{{ */
void decrement() {} /* subtract one from each */
tcTUU > void decrement(T &t, U &...u) {
--t;
decrement(u...);
}
#define ints(...) \
int __VA_ARGS__; \
re(__VA_ARGS__);
#define int1(...) \
ints(__VA_ARGS__); \
decrement(__VA_ARGS__); /*}}}*/
} /* namespace Input */
/*}}}*/
inline namespace ToString { /*{{{*/
tcT > constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;
/* ts: string representation to print */
tcT > typename enable_if<is_printable_v<T>, str>::type ts(T v) {
stringstream ss;
ss << fixed << setprecision(15) << v;
return ss.str();
} /* default */
tcT > str bit_vec(T t) { /* bit vector to string */
str res = "{";
For(i, 0, t.sz) res += ts(t[i]);
res += "}";
return res;
}
str ts(ve<bool> v) { return bit_vec(v); }
template <size_t SZ>
str ts(bitset<SZ> b) {
return bit_vec(b);
} /* bit vector */
tcTU > str ts(pair<T, U> p); /* pairs */
tcT > typename enable_if<needs_output_v<T>, str>::type ts(
T v); /* vectors, arrays */
tcTU > str ts(pair<T, U> p) { return "(" + ts(p.fi) + ", " + ts(p.se) + ")"; }
tcT > typename enable_if<is_iterable_v<T>, str>::type ts_sep(T v, str sep) {
/* convert container to string w/ separator sep */
bool fst = 1;
str res = "";
for (const auto &x : v) {
if (!fst) res += sep;
fst = 0;
res += ts(x);
}
return res;
}
tcT > typename enable_if<needs_output_v<T>, str>::type ts(T v) {
return "{" + ts_sep(v, ", ") + "}";
}
/* for nested DS */
template <int, class T>
typename enable_if<!needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {
return {ts(v)};
}
template <int lev, class T>
typename enable_if<needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {
if (lev == 0 || !v.sz) return {ts(v)};
ve<str> res;
for (const auto &t : v) {
if (res.sz) res.back() += ",";
ve<str> tmp = ts_lev<lev - 1>(t);
res.insert(end(res), all(tmp));
}
For(i, 0, res.sz) {
str bef = " ";
if (i == 0) bef = "{";
res[i] = bef + res[i];
}
res.back() += "}";
return res;
}
} /* namespace ToString */
/*}}}*/
inline namespace Output { /*{{{*/
template <class T>
void pr_sep(ostream &os, str, const T &t) {
os << ts(t);
}
template <class T, class... U>
void pr_sep(ostream &os, str sep, const T &t, const U &...u) {
pr_sep(os, sep, t);
os << sep;
pr_sep(os, sep, u...);
}
/* print w/ no spaces */
template <class... T>
void pr(const T &...t) {
pr_sep(cout, "", t...);
}
/* print w/ spaces, end with newline */
void ps() { cout << "\n"; }
template <class... T>
void ps(const T &...t) {
pr_sep(cout, " ", t...);
ps();
}
/* debug to cerr */
template <class... T>
void dbg_out(const T &...t) {
pr_sep(cerr, " | ", t...);
cerr << endl;
}
void loc_info(int line, str names) {
cerr << "Line(" << line << ") -> [" << names << "]: ";
}
template <int lev, class T>
void dbgl_out(const T &t) {
cerr << "\n\n" << ts_sep(ts_lev<lev>(t), "\n") << "\n" << endl;
}
} /* namespace Output */
/*}}}}}}}}}*/
int len, n;
vpi houses;
vpi left_houses, right_houses;
void solve() {
re(len, n), rv(n, houses);
int mv=INT_MAX, Mv=0;
for(auto [l, r] : houses) {
cmin(mv, l), cmin(mv, r), cmax(Mv, l), cmax(Mv, r);
if(l < r) right_houses.pb({l, r});
else left_houses.pb({r, l});
}
if(left_houses.sz == 0 || right_houses.sz == 0) { ps(Mv - mv); return; }
sort(all(left_houses)), sort(all(right_houses));
int opt = Mv-mv;
vpi opt1;
for(int i=0; i<left_houses.sz; ++i) {
auto [l, r] = left_houses[i];
while(i+1 < left_houses.sz && left_houses[i+1].fi < r) {
cmax(r, left_houses[i+1].se);
++i;
}
opt1.pb({l, r});
}
vi dp1(opt1.sz+1, 0);
for(int i=opt1.sz-1; i>=0; --i) {
auto [l, r] = opt1[i];
dp1[i] = min(2*(r-l) + dp1[i+1], Mv-l);
}
cmin(opt, dp1[0]);
for(int i=0; i<opt1.sz; ++i) {
auto [l, r] = opt1[i];
cmin(opt, (r-mv) + dp1[i+1]);
}
vpi opt2;
for(int i=0; i<right_houses.sz; ++i) {
auto [l, r] = right_houses[i];
while(i+1 < right_houses.sz && right_houses[i+1].fi < r) {
cmax(r, right_houses[i+1].se);
++i;
}
opt2.pb({l, r});
}
vi dp2(opt2.sz+1, 0);
for(int i=opt2.sz-1; i>=0; --i) {
auto [l, r] = opt2[i];
dp2[i] = min(2*(r-l) + dp2[i+1], Mv-l);
}
cmin(opt, dp2[0]);
for(int i=0; i<opt2.sz; ++i) {
auto [l, r] = opt2[i];
cmin(opt, (r-mv) + dp2[i+1]);
}
ps(opt + (Mv - mv));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
/* cout << fixed << setprecision(6); */
int t = 1;
// cin >> t;
for (int i = 0; i < t; i++) solve();
return 0;
// you should actually read the stuff at the bottom
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
input:
100 2 1 100 100 1
output:
198
result:
ok single line: '198'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3844kb
input:
100 6 10 6 20 15 50 54 100 98 92 87 90 89
output:
102
result:
ok single line: '102'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3536kb
input:
100 5 12 5 20 15 16 70 72 65 90 87
output:
117
result:
ok single line: '117'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3844kb
input:
100 3 2 99 3 100 2 1
output:
100
result:
ok single line: '100'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
100 5 1 50 12 11 22 21 32 31 42 41
output:
57
result:
ok single line: '57'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
100 6 50 51 53 48 46 56 60 42 37 65 71 31
output:
74
result:
ok single line: '74'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
100 5 20 25 25 5 5 1 60 70 100 90
output:
129
result:
ok single line: '129'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
1000 5 300 100 800 810 850 880 860 859 820 819
output:
830
result:
ok single line: '830'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
100 4 10 90 40 30 38 32 36 34
output:
100
result:
ok single line: '100'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
100 7 30 35 50 56 80 82 76 72 60 56 32 28 26 25
output:
80
result:
ok single line: '80'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3912kb
input:
100 1000 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6...
output:
10
result:
ok single line: '10'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
1000 195 30 40 915 925 145 155 815 825 310 320 710 720 885 895 245 255 795 805 690 700 675 685 805 815 135 145 200 210 100 110 185 195 905 915 495 505 280 290 265 275 865 875 350 360 560 570 655 665 500 510 440 450 535 545 255 265 680 690 445 455 705 715 980 990 340 350 25 35 125 135 275 285 845 855...
output:
980
result:
ok single line: '980'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
1000 195 40 30 915 925 145 155 815 825 310 320 710 720 885 895 245 255 795 805 690 700 685 675 805 815 135 145 200 210 100 110 185 195 905 915 495 505 280 290 265 275 875 865 350 360 560 570 655 665 500 510 440 450 535 545 255 265 680 690 445 455 715 705 980 990 340 350 25 35 125 135 275 285 845 855...
output:
1340
result:
ok single line: '1340'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
100 10 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9
output:
4
result:
ok single line: '4'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
100 10 6 5 7 5 8 5 9 5 7 6 8 6 9 6 8 7 9 7 9 8
output:
4
result:
ok single line: '4'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3880kb
input:
100 20 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9 6 5 7 5 8 5 9 5 7 6 8 6 9 6 8 7 9 7 9 8
output:
8
result:
ok single line: '8'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
100 20 15 16 15 17 15 18 15 19 16 17 16 18 16 19 17 18 17 19 18 19 6 5 7 5 8 5 9 5 7 6 8 6 9 6 8 7 9 7 9 8
output:
18
result:
ok single line: '18'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
100 20 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9 16 15 17 15 18 15 19 15 17 16 18 16 19 16 18 17 19 17 19 18
output:
18
result:
ok single line: '18'
Test #19:
score: 0
Accepted
time: 2ms
memory: 3800kb
input:
1000000000 10000 209348967 209348968 229937124 229937125 444539823 444539824 834193586 834193587 753724297 753724298 69731199 69731200 847194618 847194619 182537687 182537686 547230986 547230985 625700633 625700632 694773911 694773910 673553370 673553371 548011928 548011929 575352319 575352320 79658...
output:
999870208
result:
ok single line: '999870208'
Test #20:
score: 0
Accepted
time: 2ms
memory: 3800kb
input:
1000000000 10000 316337267 316337328 601315045 601315059 532181349 532181372 414591349 414591433 466115096 466115058 975372982 975372998 295310234 295310169 159781558 159781467 223845529 223845508 264790491 264790530 670386459 670386498 361735296 361735274 706122398 706122376 485946770 485946834 894...
output:
1000196604
result:
ok single line: '1000196604'
Test #21:
score: 0
Accepted
time: 0ms
memory: 3908kb
input:
10000 1000 1068 1060 1000 1009 1021 1014 1079 1088 1045 1052 1029 1037 1033 1027 1017 1015 1049 1046 1067 1071 1083 1089 1047 1056 1049 1052 1097 1106 1047 1048 1014 1006 1093 1095 1020 1027 1091 1094 1083 1076 1050 1056 1004 1013 1071 1064 1041 1039 1069 1075 1082 1087 1040 1035 1021 1013 1004 1008...
output:
224
result:
ok single line: '224'
Test #22:
score: 0
Accepted
time: 1ms
memory: 3924kb
input:
100000000 1000 20421147 20421146 55048636 55048635 73963675 73963674 4816305 4816304 76848739 76848738 97540466 97540465 89999401 89999402 41106139 41106138 88221372 88221373 41337588 41337587 5376965 5376966 88580691 88580692 15869586 15869585 81562089 81562088 62150475 62150476 48744989 48744990 1...
output:
99892329
result:
ok single line: '99892329'
Test #23:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
100000000 2000 14701499 14701498 33945296 33945297 88932681 88932682 23027294 23027293 75633747 75633746 53592756 53592755 27274503 27274504 3151839 3151840 65721952 65721951 60878217 60878216 82306884 82306885 20181216 20181215 55409757 55409758 86431508 86431507 91101189 91101190 62329363 62329364...
output:
99702793
result:
ok single line: '99702793'
Test #24:
score: 0
Accepted
time: 1ms
memory: 3748kb
input:
100000000 4000 79628643 79628644 79215193 79215194 40936090 40936091 15553634 15553635 50633493 50633494 87511539 87511540 61051247 61051246 98294975 98294974 68747010 68747011 34921612 34921611 99394195 99394196 48706506 48706505 33166543 33166542 26008861 26008862 15846417 15846418 829920 829919 7...
output:
99867443
result:
ok single line: '99867443'
Test #25:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
1000 98 10 990 15 985 20 980 25 975 30 970 35 965 40 960 45 955 50 950 55 945 60 940 65 935 70 930 75 925 80 920 85 915 90 910 95 905 100 900 105 895 110 890 115 885 120 880 125 875 130 870 135 865 140 860 145 855 150 850 155 845 160 840 165 835 170 830 175 825 180 820 185 815 190 810 195 805 200 80...
output:
980
result:
ok single line: '980'
Test #26:
score: 0
Accepted
time: 0ms
memory: 3844kb
input:
1000 98 10 990 15 985 20 980 25 975 30 970 35 965 40 960 45 955 50 950 55 945 60 940 65 935 70 930 75 925 80 920 85 915 90 910 95 905 100 900 105 895 110 890 115 885 120 880 125 875 130 870 135 865 140 860 145 855 150 850 155 845 160 840 165 835 170 830 175 825 180 820 185 815 190 810 195 805 200 80...
output:
1200
result:
ok single line: '1200'
Test #27:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
1000 92 50 40 960 950 50 950 55 945 60 940 65 935 70 930 75 925 80 920 85 915 90 910 95 905 100 900 105 895 110 890 115 885 120 880 125 875 130 870 135 865 140 860 145 855 150 850 155 845 160 840 165 835 170 830 175 825 180 820 185 815 190 810 195 805 200 800 205 795 210 790 215 785 220 780 225 775 ...
output:
940
result:
ok single line: '940'
Test #28:
score: 0
Accepted
time: 0ms
memory: 3876kb
input:
1000 92 50 40 960 950 50 950 55 945 60 940 65 935 70 930 75 925 80 920 85 915 90 910 95 905 100 900 105 895 110 890 115 885 120 880 125 875 130 870 135 865 140 860 145 855 150 850 155 845 160 840 165 835 170 830 175 825 180 820 185 815 190 810 195 805 200 800 205 795 210 790 215 785 220 780 225 775 ...
output:
1160
result:
ok single line: '1160'
Test #29:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
100 12 60 56 58 52 42 43 52 78 53 91 95 64 16 19 45 24 65 97 92 84 79 66 39 53
output:
142
result:
ok single line: '142'
Test #30:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
10000 10 4147 4146 7371 7370 3410 3411 9793 9792 6589 6588 6897 6898 8034 8033 4478 4477 1558 1557 824 825
output:
8974
result:
ok single line: '8974'
Test #31:
score: 0
Accepted
time: 0ms
memory: 3844kb
input:
100 10 16 15 11 10 23 22 49 48 34 33 32 31 58 59 19 18 84 85 50 51
output:
80
result:
ok single line: '80'
Test #32:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
100 7 16 15 11 10 23 22 49 48 34 33 32 31 58 59
output:
50
result:
ok single line: '50'
Test #33:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
10 6 1 4 3 5 6 7 2 1 9 4 8 5
output:
14
result:
ok single line: '14'
Test #34:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
100 3 11 50 50 49 36 35
output:
42
result:
ok single line: '42'
Test #35:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
1000000000 1000 106800000 106000000 100000000 100900000 102100000 101400000 107900000 108800000 104500000 105200000 102900000 103700000 103300000 102700000 101700000 101500000 104900000 104600000 106700000 107100000 108300000 108900000 104700000 105600000 104900000 105200000 109700000 110600000 1047...
output:
22400000
result:
ok single line: '22400000'
Test #36:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
1000000000 12 600000000 560000000 580000000 520000000 420000000 430000000 520000000 780000000 530000000 910000000 950000000 640000000 160000000 190000000 450000000 240000000 650000000 970000000 920000000 840000000 790000000 660000000 390000000 530000000
output:
1420000000
result:
ok single line: '1420000000'
Test #37:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
100000000 10 41474147 41464146 73717371 73707370 34103410 34113411 97939793 97929792 65896589 65886588 68976897 68986898 80348034 80338033 44784478 44774477 15581558 15571557 8240824 8250825
output:
89748974
result:
ok single line: '89748974'
Test #38:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
1000000000 10 524574766 467829835 788760015 749931008 683561222 688458529 490326602 466294773 275555931 196523978 440746441 464654142 548562021 630916391 411231136 387178580 821334137 849367674 87887936 100019122
output:
1023963217
result:
ok single line: '1023963217'
Test #39:
score: 0
Accepted
time: 2ms
memory: 3864kb
input:
1000000 10000 355032 355580 184745 184959 87644 88493 83931 84638 513415 514343 557120 557346 477755 478077 116652 115805 220660 220989 747101 746839 674063 674752 459344 458556 718257 717393 854296 854755 334181 333409 445981 446979 140393 139994 66404 65570 111308 111692 956080 955110 146575 14704...
output:
1997935
result:
ok single line: '1997935'
Test #40:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
150 9 40 38 30 29 25 10 8 1 50 60 68 64 89 88 128 110 150 149
output:
169
result:
ok single line: '169'
Test #41:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
1150 9 40 38 30 29 25 10 8 1 100 900 1068 1064 1089 1088 1128 1110 1150 1149
output:
1226
result:
ok single line: '1226'
Test #42:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
1150 5 40 38 30 29 25 10 8 1 100 900
output:
929
result:
ok single line: '929'
Test #43:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
1150 5 100 900 1068 1064 1089 1088 1128 1110 1150 1149
output:
1097
result:
ok single line: '1097'