QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#644817 | #2541. Coins and Boxes | proven# | WA | 34ms | 15376kb | C++20 | 1.7kb | 2024-10-16 15:32:33 | 2024-10-16 15:32:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define PII pair<int, int>
const int N = 4e5 + 5;
const int mod = 1e9 + 7;
int n, top, b[N], c[N], a[N], dp[N];
vector<PII> tr;
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> b[i], a[++top] = b[i];
for (int i = 1; i <= n; i++)
cin >> c[i], c[i] = max(c[i], b[i]), a[++top] = c[i];
sort(a + 1, a + 1 + top);
top = unique(a + 1, a + 1 + top) - a - 1;
for (int i = 1; i <= n; i++)
{
b[i] = lower_bound(a + 1, a + 1 + top, b[i]) - a;
c[i] = lower_bound(a + 1, a + 1 + top, c[i]) - a;
}
tr.push_back({0, 0});
for (int i = 1; i <= n; i++)
tr.push_back({b[i], c[i]});
sort(tr.begin(), tr.end(), [&](PII a, PII b)
{
if(a.first!=b.first)
return a.first<b.first;
return a.second>b.second; });
fill(dp + 1, dp + 1 + n, 1e18);
dp[0] = 0;
int mxr = 0, pre = 0;
for (int i = 1; i <= n; i++)
{
if (tr[i].second <= mxr)
dp[i] = dp[pre] + a[tr[i].first] - a[tr[pre].first];
else
{
dp[i] = dp[i - 1] + 2 * a[tr[i].second] - 2 * a[tr[i].first] + a[tr[i].first] - a[tr[i - 1].first];
pre = i;
}
mxr = max(mxr, tr[i].second);
}
int ans = dp[n], mx = a[top];
for (int i = 1; i <= n; i++)
{
ans = min(ans, dp[i - 1] + 2 * mx - 2 * a[tr[i].first] + a[tr[i].first] - a[tr[i - 1].first]);
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9812kb
input:
4 1 6 7 12 3 5 10 11
output:
21
result:
ok answer is '21'
Test #2:
score: 0
Accepted
time: 0ms
memory: 9820kb
input:
2 1 2 1 1000000000
output:
1999999998
result:
ok answer is '1999999998'
Test #3:
score: -100
Wrong Answer
time: 34ms
memory: 15376kb
input:
100000 967 3246 9492 10300 15195 16650 26911 54855 83695 112841 125511 137160 153051 155859 177924 187843 214838 219388 247276 249612 250188 253873 257830 261805 281312 297030 298332 325904 333218 339683 374111 387794 396645 403705 426710 436137 463368 481801 501933 509267 511332 515225 515629 51686...
output:
1999994467
result:
wrong answer expected '1722240547', found '1999994467'