QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#379392 | #8572. Passing Game | ucup-team135# | WA | 1253ms | 14920kb | C++20 | 3.3kb | 2024-04-06 17:24:16 | 2024-04-06 17:24:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define app push_back
#define all(x) (x).begin(),(x).end()
#ifdef LOCAL
#define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
#else
#define debug(...)
#endif
#ifdef LOCAL
#define __int128 long long
#endif // LOCAL
const int inf=1e18;
/**
* Author: Simon Lindholm
* Date: 2017-04-20
* License: CC0
* Source: own work
* Description: Container where you can add lines of the form kx+m, and query ///maximum values at points x.
* Useful for dynamic programming (``convex hull trick'').
* Time: O(\log N)
* Status: stress-tested
*/
#pragma once
#define ll long long
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)
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()) { x->p = inf; return false; }
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);
}
};
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;cin>>t;
while(t--)
{
int n,k;cin>>n>>k;k=min(k,20LL);
vector<array<int,3> > a(n);
for(int i=0;i<n;++i) {cin>>a[i][1];} for(int i=0;i<n;++i) {cin>>a[i][0];} for(int i=0;i<n;++i) {a[i][2]=i;}
sort(all(a),[&](array<int,3> u,array<int,3> v) {return u[1]<v[1];});int pos=0;while(a[pos][2]) {++pos;}
int pos2=0;while(a[pos2][2]!=(n-1)) {++pos2;}
vector<int> dp(n);fill(all(dp),inf);dp[pos]=0;
for(int i=0;i<=k+1;++i)
{
LineContainer l1;
for(int j=pos;j>=0;--j)
{
l1.add(-a[j][0],a[j][0]*a[j][1]+dp[j]);
dp[j]=min(dp[j],l1.query(a[j][1]));
}
LineContainer l2;
for(int j=pos;j<n;++j)
{
l2.add(a[j][0],-a[j][0]*a[j][1]+dp[j]);
dp[j]=min(dp[j],l2.query(a[j][1]));
}
vector<int> dp2=dp;
if(i<=k)
{
LineContainer l1;
for(int j=n-1;j>=0;--j)
{
l1.add(-a[j][0],a[j][0]*a[j][1]+dp[j]);
dp2[j]=min(dp[j],l1.query(a[j][1]));
}
LineContainer l2;
for(int j=0;j<n;++j)
{
l2.add(a[j][0],-a[j][0]*a[j][1]+dp[j]);
dp2[j]=min(dp[j],l2.query(a[j][1]));
}
dp=dp2;
}
}
cout<<dp[pos2]<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3592kb
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: 1223ms
memory: 14920kb
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: 1253ms
memory: 7696kb
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: -100
Wrong Answer
time: 1235ms
memory: 14704kb
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:
44464396812195
result:
wrong answer 1st numbers differ - expected: '40311829457542', found: '44464396812195'