比赛时暴力枚举,TEL。
然后尝试用滑动窗口优化,然后发现无法优化。
赛后才知道可以用优先队列来优化。
好可惜呀。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll TA,TB;
ll x;
ll N,M;
struct dd
{
ll t,v,d;
ll st,ed;
dd(ll a,ll b,ll c):t(a),v(b),d(c)
{
st=t+v;
if(t+2*v>=x&&t+2*v<=x+TB) ed=t+((x+TB-t)/(2*v))*2*v+v;
else ed=st;
}
bool operator < (const dd& rhs) const
{
return ed<rhs.ed;
}
};
vector<dd>vec;
struct cmp
{
bool operator () (dd& a,dd& b)
{
return a.st>b.st;
}
};
int main()
{
while(scanf("%lld %lld",&TA,&TB)!=EOF)
{
ll sum=0;
vec.clear();
scanf("%lld %lld %lld",&x,&N,&M);
ll t,v,d;
for(ll i=0;i<N;i++)
{
scanf("%lld %lld %lld",&t,&v,&d);
if(t+v>=x&&t+v<=x+TB)
{
vec.push_back(dd(t+v,v,d));
sum+=d;
}
}
for(ll i=0;i<M;i++)
{
scanf("%lld %lld %lld",&t,&v,&d);
vec.push_back(dd(t,v,d));
sum+=d;
}
sort(vec.begin(),vec.end());
ll ANS=0;
ll ans=0;
priority_queue<dd,vector<dd>,cmp>q;
ll st,ed,k;
k=0;
for(unsigned int i=0;i<vec.size();i++)
{
ed=vec[i].ed;
st=ed-TA;
while(k<vec.size()&&vec[k].ed<=ed)
{
q.push(vec[k]);
ans+=vec[k].d;
k++;
}
while(!q.empty())
{
dd temp=q.top();
if(temp.st<st)
{
ans-=temp.d;
q.pop();
}
else break;
}
ANS=max(ANS,ans);
}
printf("%d\n",sum-ANS);
}
return 0;
}