QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#854645 | #140. Palembang Bridges | makrav | 22 | 28ms | 8120kb | C++20 | 3.5kb | 2025-01-12 04:02:36 | 2025-01-12 04:02:36 |
Judging History
answer
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define sc second
#define int ll
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
void shuf(vector<T>& a) {
for (int i = 1; i < sz(a); i++) swap(a[i], a[rnd() % (i + 1)]);
}
struct WW {
set<pair<int, int>> st;
set<pair<int, int>>::iterator it;
int sm = 0, siz = 0;
WW() = default;
void add(pair<int, int> x, pair<int, int> y) {
if (st.empty()) {
st.insert(x); st.insert(y); it = st.begin();
sm = abs(x.second - y.second);
siz += 2;
return;
}
if (min(x, y) < *it && max(x, y) > *it) {
sm +=max(x, y).second- min(x, y).second;
pair<int, int> val = *it;
st.insert(x); st.insert(y); it = st.find(val);
} else if (min(x, y) > *it) {
sm += x.second + y.second - 2 * (*it).second;
pair<int, int> val = *it;
st.insert(x); st.insert(y); it = st.find(val);
it++;
int dlt = (*it).second - val.second;
sm -= 2 * dlt;
} else {
sm += 2 * (*it).second - x.second - y.second;
pair<int, int> val = *it;
st.insert(x); st.insert(y); it = st.find(val);
it--;
int dlt = val.second - (*it).second;
sm -= 2 * dlt;
}
siz += 2;
}
};
void solve() {
int k, n; cin >> k >> n;
vector<pair<int, int>> lol;
int ans = 0;
for (int i = 0; i < n; i++) {
char r1, r2;
int p1, p2;
cin >> r1 >> p1 >> r2 >> p2;
if (r1 == r2) {
ans += abs(p1 - p2);
continue;
}
ans++;
lol.pb({p1, p2});
}
if (k == 1) {
vector<int> ap;
for (auto u : lol) {
ap.pb(u.first); ap.pb(u.second);
}
sort(all(ap));
if (!ap.empty()) {
int mp = ap[sz(ap) / 2];
for (int j = 0; j < sz(ap); j++) ans += abs(ap[j] - mp);
}
cout << ans << '\n';
return;
}
sort(all(lol), [&](pair<int, int> x, pair<int, int> y) {
return x.first + x.second < y.first + y.second;
});
vector<pair<int, int>> pp;
for (int i = 0; i < sz(lol); i++) {
pp.pb({lol[i].first, 2 * i}); pp.pb({lol[i].second, 2 * i + 1});
}
sort(all(pp));
vector<array<int, 2>> pos(sz(lol));
for (int i = 0; i < sz(pp); i++) {
pos[pp[i].second / 2][pp[i].second % 2] = i;
}
vector<int> cp(sz(lol) + 1), cs(sz(lol) + 1);
WW pr, sf;
for (int j = 0; j < sz(lol); j++) {
pr.add({pos[j][0], lol[j].first}, {pos[j][1], lol[j].second});
cp[j + 1] = pr.sm;
}
for (int j = sz(lol) - 1; j >= 0; j--) {
sf.add({pos[j][0], lol[j].first}, {pos[j][1], lol[j].second});
cs[sz(lol) - j] = sf.sm;
}
int dop = 1e18;
for (int ppp = 0; ppp <= sz(lol); ppp++) dop = min(dop, cp[ppp] + cs[sz(lol) - ppp]);
cout << ans + dop << '\n';
}
signed main() {
int tt = 1;
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cin >> tt;
#else
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
while (tt--) {
solve();
}
return 0;
}
詳細信息
Subtask #1:
score: 8
Accepted
Test #1:
score: 8
Accepted
time: 0ms
memory: 3708kb
input:
1 1 B 426311872 B 741424667
output:
315112795
result:
ok single line: '315112795'
Test #2:
score: 8
Accepted
time: 1ms
memory: 3680kb
input:
1 1000 A 1000000000 B 1000000000 B 1000000000 A 1000000000 A 500000000 B 500000000 A 1000000000 B 1000000000 B 0 A 0 A 500000000 B 500000000 B 0 A 0 A 1000000000 B 1000000000 A 500000000 B 500000000 A 1000000000 B 1000000000 B 1000000000 A 1000000000 A 0 B 0 B 0 A 0 B 0 A 0 A 500000000 B 500000000 B...
output:
659000001000
result:
ok single line: '659000001000'
Test #3:
score: 8
Accepted
time: 1ms
memory: 3652kb
input:
1 1000 A 500000001 B 500000000 A 500000002 B 500000003 A 500000005 B 500000004 A 1 B 0 B 500000007 A 500000006 A 500000009 B 500000008 B 500000010 A 500000011 B 1000000000 A 999999999 A 3 B 2 B 499999988 A 499999989 A 999999998 B 999999997 B 4 A 5 B 7 A 6 A 9 B 8 A 10 B 11 B 999999996 A 999999995 A ...
output:
649999819818
result:
ok single line: '649999819818'
Test #4:
score: 8
Accepted
time: 0ms
memory: 3812kb
input:
1 4 B 90 B 72 A 68 A 90 A 15 A 42 A 45 A 15
output:
97
result:
ok single line: '97'
Test #5:
score: 8
Accepted
time: 1ms
memory: 3644kb
input:
1 1000 A 0 B 1 A 1 B 0 A 0 B 1 B 0 A 0 B 1 B 1 B 0 A 0 A 1 B 1 A 1 B 1 A 1 B 0 A 0 B 1 A 0 B 0 B 1 A 1 B 0 A 0 B 0 A 0 A 1 B 0 A 1 B 0 B 1 A 0 B 0 A 0 A 1 B 1 A 1 B 1 B 0 A 1 B 0 A 1 A 0 B 1 B 0 A 1 A 1 B 0 A 1 B 1 B 1 A 0 B 1 A 0 B 1 A 0 A 1 B 0 A 0 B 1 A 0 B 1 A 0 B 0 B 0 A 0 A 1 B 1 B 0 A 1 B 0 A...
output:
1969
result:
ok single line: '1969'
Test #6:
score: 8
Accepted
time: 1ms
memory: 3648kb
input:
1 1000 A 598246 B 85766 B 457924 A 841963 B 107690 A 39924 B 814042 A 328853 B 518897 A 548739 B 633776 A 181927 A 985484 B 773931 A 698975 B 526021 A 846547 B 258759 B 778661 A 181322 B 131489 A 151052 A 669218 B 314136 B 562707 A 887236 B 47953 A 880429 A 251615 B 488724 B 734021 A 487209 B 492935...
output:
497401257
result:
ok single line: '497401257'
Test #7:
score: 8
Accepted
time: 1ms
memory: 3772kb
input:
1 967 B 209071192 A 810324333 A 603292190 B 990286906 B 76624835 A 1764783 B 38843365 A 66481975 B 506328820 A 854781128 B 601820095 A 20961640 B 953337305 A 762377474 B 472547211 A 248665021 A 908090176 B 277738137 B 523090586 A 193277212 B 824206700 A 342446509 B 845969410 A 191939894 A 472429676 ...
output:
470796680092
result:
ok single line: '470796680092'
Test #8:
score: 8
Accepted
time: 0ms
memory: 3736kb
input:
1 1000 B 0 A 1000000000 B 0 A 1000000000 B 1000000000 A 0 A 1000000000 B 0 B 0 A 1000000000 B 0 A 1000000000 A 0 B 1000000000 B 0 A 1000000000 B 0 A 1000000000 B 0 A 1000000000 B 0 A 1000000000 A 1000000000 B 0 B 0 A 1000000000 B 0 A 1000000000 A 1000000000 B 0 B 1000000000 A 0 B 1000000000 A 0 B 0 ...
output:
1000000001000
result:
ok single line: '1000000001000'
Test #9:
score: 8
Accepted
time: 1ms
memory: 3716kb
input:
1 1000 A 126148 B 138629 A 1956796 B 1961512 A 2467612 B 2976963 B 3231056 A 3187470 B 4485092 A 4805418 A 5815768 B 5012466 B 6709826 A 6631652 A 7384525 B 7982443 A 8702501 B 8083053 A 9750494 B 9414146 B 10511913 A 10300242 A 11414967 B 11792128 B 12453829 A 12271791 A 13801488 B 13139841 B 14570...
output:
499992449655
result:
ok single line: '499992449655'
Test #10:
score: 8
Accepted
time: 0ms
memory: 3708kb
input:
1 1000 B 1000000000 A 0 B 1 A 999999999 A 999999998 B 2 A 999999997 B 3 A 999999996 B 4 B 5 A 999999995 B 6 A 999999994 A 7 B 999999993 B 999999992 A 8 A 9 B 999999991 A 999999990 B 10 A 11 B 999999989 B 12 A 999999988 A 13 B 999999987 B 14 A 999999986 A 999999985 B 15 B 999999984 A 16 B 17 A 999999...
output:
999999002000
result:
ok single line: '999999002000'
Test #11:
score: 8
Accepted
time: 1ms
memory: 3912kb
input:
1 1000 A 639022834 B 639022825 B 164875881 A 164875889 A 345973409 B 345973410 B 650643813 A 650643816 A 218804569 B 218804573 B 60886574 A 60886583 B 829040885 A 829040890 B 340211580 A 340211573 B 783059185 A 783059177 B 994262398 A 994262399 B 427304481 A 427304486 A 373191501 B 373191501 A 65190...
output:
498415265474
result:
ok single line: '498415265474'
Subtask #2:
score: 14
Accepted
Dependency #1:
100%
Accepted
Test #12:
score: 14
Accepted
time: 7ms
memory: 7996kb
input:
1 100000 B 0 A 0 A 1 B 0 B 0 A 0 A 1 B 0 B 1 A 0 B 1 A 1 B 1 A 0 A 0 B 0 B 1 A 0 B 1 A 0 B 1 A 0 B 0 A 1 B 0 A 0 B 0 A 1 A 0 B 1 B 1 A 0 A 1 B 1 B 0 A 0 A 0 B 0 A 1 B 1 B 1 A 0 B 0 A 1 A 0 B 0 A 0 B 1 B 1 A 0 A 0 B 1 A 0 B 0 A 1 B 0 A 1 B 1 A 1 B 0 B 1 A 0 A 1 B 0 A 0 B 0 B 0 A 1 B 1 A 0 A 0 B 1 A 0...
output:
199907
result:
ok single line: '199907'
Test #13:
score: 14
Accepted
time: 20ms
memory: 7948kb
input:
1 100000 A 999999999 B 1000000000 A 1 B 0 B 3 A 2 A 4 B 5 A 999999998 B 999999997 A 500000000 B 500000001 B 999999995 A 999999996 B 999999993 A 999999994 A 500000002 B 500000003 A 500000005 B 500000004 B 500000007 A 500000006 A 999999991 B 999999992 A 7 B 6 A 999999989 B 999999990 B 499999993 A 4999...
output:
66818736664678
result:
ok single line: '66818736664678'
Test #14:
score: 14
Accepted
time: 28ms
memory: 7948kb
input:
1 100000 A 999933197 B 922319427 B 358973258 A 110896122 B 790706905 A 700578466 A 261833979 B 174780238 B 527377624 A 790157498 A 712098182 B 434767355 B 649365576 A 222614576 B 524821299 A 224370351 B 634735585 A 55865839 A 423764322 B 340889518 A 366912717 B 836451240 A 284261689 B 409476293 B 55...
output:
50064192028779
result:
ok single line: '50064192028779'
Test #15:
score: 14
Accepted
time: 8ms
memory: 7460kb
input:
1 69170 A 4068 B 355 A 9929 B 3341 A 4251 B 9270 B 5374 A 2023 A 3681 B 3440 B 6376 A 5305 B 4997 A 5759 A 3317 B 8305 A 8373 B 5214 A 1547 B 2435 A 4626 B 5646 B 248 A 1475 A 6965 B 4751 B 3227 A 6900 B 9360 A 9365 A 9541 B 9544 A 8973 B 4135 A 4153 B 4360 A 8220 B 2175 A 5226 B 798 B 3407 A 7817 B...
output:
345319577
result:
ok single line: '345319577'
Test #16:
score: 14
Accepted
time: 19ms
memory: 7412kb
input:
1 67413 B 457424278 A 935666529 A 681939979 B 928741595 A 239479026 B 115368163 A 958803056 B 199062010 A 486156065 B 570726489 B 548364515 A 298119844 A 898952041 B 85864523 A 547883826 B 982228879 B 859912859 A 399166419 B 264228446 A 288037768 A 367765591 B 463758282 B 798194145 A 176040722 A 814...
output:
33756488959590
result:
ok single line: '33756488959590'
Test #17:
score: 14
Accepted
time: 12ms
memory: 8072kb
input:
1 100000 B 0 A 1000000000 A 1000000000 B 0 A 0 B 1000000000 B 0 A 1000000000 A 0 B 1000000000 A 0 B 1000000000 B 0 A 1000000000 B 1000000000 A 0 B 0 A 1000000000 A 0 B 1000000000 A 1000000000 B 0 B 1000000000 A 0 B 0 A 1000000000 A 1000000000 B 0 A 1000000000 B 0 B 0 A 1000000000 A 1000000000 B 0 A ...
output:
100000000100000
result:
ok single line: '100000000100000'
Test #18:
score: 14
Accepted
time: 11ms
memory: 7936kb
input:
1 100000 B 4010 A 1623 A 17430 B 10348 A 21931 B 24331 B 32800 A 32334 A 46932 B 46932 B 50584 A 51555 B 67804 A 65286 B 74388 A 78905 A 89581 B 87977 B 92190 A 99330 A 103837 B 109596 A 110712 B 118878 B 129571 A 129684 B 135403 A 135955 B 142981 A 141340 A 157619 B 156022 A 160751 B 160032 B 17041...
output:
50000000498063
result:
ok single line: '50000000498063'
Test #19:
score: 14
Accepted
time: 22ms
memory: 8120kb
input:
1 100000 A 1000000000 B 0 A 1 B 999999999 B 999999998 A 2 B 999999997 A 3 A 999999996 B 4 A 999999995 B 5 A 6 B 999999994 B 999999993 A 7 B 8 A 999999992 B 9 A 999999991 A 10 B 999999990 A 11 B 999999989 A 999999988 B 12 B 999999987 A 13 B 999999986 A 14 B 999999985 A 15 A 16 B 999999984 A 999999983...
output:
99990000200000
result:
ok single line: '99990000200000'
Test #20:
score: 14
Accepted
time: 27ms
memory: 7932kb
input:
1 100000 B 720785899 A 720785904 B 287750440 A 287750448 B 76945024 A 76945019 B 841992577 A 841992581 B 391280439 A 391280443 B 427213907 A 427213908 A 675103196 B 675103193 B 294688240 A 294688246 B 776975834 A 776975844 B 731042985 A 731042989 B 982066740 A 982066750 B 834618250 A 834618241 B 368...
output:
49895210266053
result:
ok single line: '49895210266053'
Test #21:
score: 14
Accepted
time: 17ms
memory: 8068kb
input:
1 100000 A 1000000000 B 1000000000 B 0 A 0 A 1000000000 B 1000000000 A 500000000 B 500000000 B 1000000000 A 1000000000 A 500000000 B 500000000 B 1000000000 A 1000000000 B 500000000 A 500000000 B 1000000000 A 1000000000 B 1000000000 A 1000000000 A 0 B 0 B 0 A 0 B 500000000 A 500000000 B 1000000000 A ...
output:
66715000100000
result:
ok single line: '66715000100000'
Subtask #3:
score: 0
Wrong Answer
Test #22:
score: 9
Accepted
time: 0ms
memory: 3656kb
input:
2 1 B 822190955 B 309099167
output:
513091788
result:
ok single line: '513091788'
Test #23:
score: 0
Wrong Answer
time: 0ms
memory: 3684kb
input:
2 100 A 699479271 B 699479270 B 999432953 A 999432956 A 657124443 B 657124444 B 928721383 A 928721383 B 809218485 A 809218487 B 220719335 A 220719338 B 93242937 A 93242943 A 566737518 B 566737523 A 751211620 B 751211614 B 799605314 A 799605321 A 634052514 B 634052513 B 681923789 A 681923799 B 377862...
output:
22424494975
result:
wrong answer 1st lines differ - expected: '22963819679', found: '22424494975'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%