QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#380631 | #8572. Passing Game | ucup-team1134# | TL | 1ms | 3880kb | C++23 | 3.5kb | 2024-04-07 06:58:53 | 2024-04-07 06:58:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005;
const ll INF=1LL<<61;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//dynamic CHT MIN
/**
* 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
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 LineContainerMIN : 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*=-1;m*=-1;
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 dp[MAX];
int main(){
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
int Q;cin>>Q;
while(Q--){
ll N,K;cin>>N>>K;
chmin(K,70LL);
vector<array<ll,3>> S(N);
for(int i=0;i<N;i++) cin>>S[i][0];
for(int i=0;i<N;i++) cin>>S[i][1];
for(int i=0;i<N;i++) S[i][2]=i;
sort(all(S));
int stt=-1,goo=-1;
for(int i=0;i<N;i++){
if(S[i][2]==0){
stt=i;
}
if(S[i][2]==N-1){
goo=i;
}
}
for(int i=0;i<=N;i++){
dp[i]=INF;
}
dp[stt]=0;
for(int t=0;t<=K;t++){
vector<ll> neL(N,INF),neR(N,INF);
{
LineContainerMIN cht;
for(int i=0;i<N;i++){
auto [x,v,iii]=S[i];
if(si(cht)) chmin(neR[i],cht.query(x));
chmin(neR[i],dp[i]);
if(neR[i]!=INF) cht.add(v,v*(-x)+neR[i]);
}
}
{
LineContainerMIN cht;
for(int i=N-1;i>=0;i--){
auto [x,v,iii]=S[i];
if(si(cht)) chmin(neL[i],cht.query(-x));
chmin(neL[i],dp[i]);
if(neL[i]!=INF) cht.add(v,v*x+neL[i]);
}
}
for(int i=0;i<N;i++){
chmin(dp[i],min(neL[i],neR[i]));
}
}
cout<<dp[goo]<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3880kb
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: -100
Time Limit Exceeded
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...