QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#380613 | #8572. Passing Game | ucup-team1134# | WA | 2ms | 8260kb | C++23 | 4.0kb | 2024-04-07 06:41:51 | 2024-04-07 06:41:51 |
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);
}
};
pair<ll,int> 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<MAX;i++){
dp[i]=mp(INF,0);
}
dp[stt]=mp(0,0);
for(int t=0;t<=K;t++){
bool upd=false;
vector<ll> neL(N,INF),neR(N,INF);
{
LineContainerMIN cht;
for(int i=0;i<N;i++){
neR[i]=dp[i].fi;
auto [x,v,iii]=S[i];
if(si(cht)){
if(chmin(neR[i],cht.query(x))) cht.add(v,v*(-x)+neR[i]);
}else{
if(neR[i]!=INF&&neR[i]<dp[goo].fi&&(neR[i]<dp[i].fi||dp[i].se==t)) cht.add(v,v*(-x)+neR[i]);
}
}
}
{
LineContainerMIN cht;
for(int i=N-1;i>=0;i--){
neL[i]=dp[i].fi;
auto [x,v,iii]=S[i];
if(si(cht)){
if(chmin(neL[i],cht.query(-x))) cht.add(v,v*x+neL[i]);
}else{
if(neL[i]!=INF&&neL[i]<dp[goo].fi&&(neL[i]<dp[i].fi||dp[i].se==t)) cht.add(v,v*x+neL[i]);
}
}
}
for(int i=0;i<N;i++){
if(min(neL[i],neR[i])<dp[goo].fi&&chmin(dp[i],mp(min(neL[i],neR[i]),t+1))) upd=true;
}
if(!upd) break;
}
cout<<dp[goo].fi<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 8260kb
input:
2 4 2 3 2 1 6 3 1 1 3 2 0 1 2 1 2
output:
9 1
result:
wrong answer 1st numbers differ - expected: '7', found: '9'