QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#378349 | #8572. Passing Game | ucup-team2894# | RE | 1231ms | 99700kb | C++20 | 3.5kb | 2024-04-06 11:44:40 | 2024-04-06 11:44:41 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<(b);++i)
using namespace std;
using pii = pair<int,int>;
using ll = long long;
using pll = pair<ll,ll>;
using vi = vector<int>;
using ld = long double;
mt19937_64 rnd(1);
#define all(x) (x).begin(), (x).end()
const int maxn = 3e5+10, inf = 1e9+100;
const ll linf = 1e18+100;
const int mod = 998244353;
const ld eps = 1e-9;
const ld PI = acos(-1.L);
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
k = -k, m = -m;
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return -l.k * x - l.m;
}
};
ll x[maxn], s[maxn];
ll dp[31][maxn];
void solvee(){
int n,k;
cin >> n >> k;
k = min(k, 30);
for(int i=0;i<n;i++)cin >> x[i];
for(int i=0;i<n;i++)cin >> s[i];
int stx = x[0], tarx = x[n-1];
map<ll,ll> sss;
for(int i=0;i<n;i++){
if(!sss.count(x[i]))sss[x[i]]=s[i];
else sss[x[i]] = min(sss[x[i]], s[i]);
}
n = sss.size();
{
int i = 0;
for(auto [xv,sv] : sss) {
x[i] = xv, s[i] = sv;
i++;
}
}
int st, tar;
for(int i=0;i<n;i++){
if(x[i]==stx)st = i;
if(x[i]==tarx)tar = i;
}
// cerr << st << " " << tar << endl;
for(int j=0;j<=k;j++)for(int i=0;i<n;i++)dp[j][i] = linf;
dp[0][st] = 0;
for(int ind=0;ind<=k;ind++){
dp[ind][st] = 0;
// advance same side
LineContainer cvx;
// right
cvx.add(s[st],dp[ind][st]-x[st]*s[st]);
for(int i=st+1;i<n;i++){
dp[ind][i] = min(dp[ind][i], cvx.query(x[i]));
cvx.add(s[i],dp[ind][i]-x[i]*s[i]);
}
// left
cvx.clear();
cvx.add(-s[st],dp[ind][st]+x[st]*s[st]);
for(int i=st-1;i>=0;i--){
dp[ind][i] = min(dp[ind][i], cvx.query(x[i]));
cvx.add(-s[i],dp[ind][i]+x[i]*s[i]);
}
if(ind==k)break;
// switch to other side
// right -> left
cvx.clear();
for(int i=st+1;i<n;i++){
cvx.add(-s[i],dp[ind][i]+x[i]*s[i]);
}
for(int i=st-1;i>=0;i--){
dp[ind+1][i] = min(dp[ind+1][i],cvx.query(x[i]));
}
// left -> right
cvx.clear();
for(int i=st-1;i>=0;i--){
cvx.add(s[i],dp[ind][i]-x[i]*s[i]);
// cerr << "LR LINE " << s[i] << " " << dp[ind][i]-x[i]*s[i] << endl;
// cerr << cvx.query(6);
}
for(int i=st+1;i<n;i++){
dp[ind+1][i] = min(dp[ind+1][i],cvx.query(x[i]));
}
}
// for(int i=0;i<=k;i++){
// for(int j=0;j<n;j++)cerr << dp[i][j] << " \n"[j==n-1];
// }
ll ans = linf;
for(int i=0;i<=k;i++)ans=min(ans, dp[i][tar]);
cout << ans << "\n";
}
void solve(){
int tc;
cin >> tc;
while(tc--){
solvee();
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout<<fixed;
cout.precision(20);
cerr << fixed;
cerr.precision(5);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3568kb
input:
2 4 2 3 2 1 6 3 1 1 3 2 0 1 2 1 2
output:
7 1
result:
ok 2 number(s): "7 1"
Test #2:
score: 0
Accepted
time: 1181ms
memory: 99656kb
input:
1 300000 204334 809492393 304618667 173130445 377106790 364888630 949045125 622060683 557772818 216607577 848817467 862855568 507840723 120816645 639713488 741781998 682531787 685261161 601686403 355792373 162819930 710057718 234560726 998604853 678957602 485413982 855985802 109303681 979706626 4822...
output:
31313390701066
result:
ok 1 number(s): "31313390701066"
Test #3:
score: 0
Accepted
time: 1200ms
memory: 35728kb
input:
3 100000 65460 217141764 710454586 789075415 24849107 685675008 839804815 638763480 327755609 43827967 390187172 301370841 622696676 598237196 232099091 211987715 416876077 572665966 73382836 520033984 808399404 752832432 341795744 434460344 535426588 136624537 997406768 297342165 558882675 26863877...
output:
70635841128944 47230361360721 59110547802683
result:
ok 3 number(s): "70635841128944 47230361360721 59110547802683"
Test #4:
score: 0
Accepted
time: 1171ms
memory: 99672kb
input:
1 300000 101975 207258305 525434317 528778163 645316642 562113679 143398489 9114413 669854123 106324041 841914487 21419012 308025536 689200225 263298218 39377353 860366080 24610184 43404209 529054797 902238799 422737070 484129934 967667618 953541323 338625285 115085955 363490839 998893783 877857789 ...
output:
40311829457542
result:
ok 1 number(s): "40311829457542"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3724kb
input:
18 17 0 500000000 499999997 500000010 499999965 500000118 499999609 500001291 499995739 500014064 499953589 500153157 499494579 501667889 494495965 518163316 440061055 697798520 197798520 59938945 18163316 5504035 1667889 505421 153157 46411 14064 4261 1291 391 118 35 10 3 1 17 1 500000000 499999997...
output:
15506866876 14901483521 14901483521 14596599454 14596599454 14327495899 14327495899 14069829018 14069829018 13814295534 13814295534 13559037370 13559037370 13303815695 13303815695 13061698660 13061698660 13061698660
result:
ok 18 numbers
Test #6:
score: 0
Accepted
time: 1231ms
memory: 99700kb
input:
1 300000 108311 562733523 631963246 813519221 169288073 117366252 527887631 958029417 190367625 583075950 97173270 896048212 131308006 37400006 90012864 923307076 279518077 383193505 134227338 223315078 230865002 126513901 226753074 767298214 632275980 328061909 158106292 275417458 205909246 6796599...
output:
22530954433990
result:
ok 1 number(s): "22530954433990"
Test #7:
score: -100
Runtime Error
input:
6000 50 9 134170081 235040736 405102476 567954026 351391400 872095141 116079799 935104039 779623552 857355273 642352783 394389351 10847154 403727586 104520573 266338849 213345479 719882827 836874283 973992590 698097800 821674140 408510849 363267881 10753022 371526254 123631785 246871721 505280626 29...