题意:洛谷有中文题意,链接:https://www.luogu.org/problemnew/show/CF1101F
思路:只考虑一辆车,我们可以很简单的使用二分操作对V进行二分找到答案,运行的时间是可以接受的。因为目前车的数目太多了,我们无法直接进行二分操作,所以我们考虑使用的是一辆车一辆车的V,一步一步地提升V。但是考虑到极端地情况下,复杂度依然很高。其实我们发现其实最终的V只取决于其中一辆车,很多车辆有可能不需要考虑,如果将操作均摊下来,时间复杂度并不高。
PS:AC的那一刻,感觉有点funny。
#include <stdio.h>
#include <algorithm>
using namespace std;
#define LL long long
#define MAX 250010
struct node{
int s;
int f;
int c;
int r;
int id;
};
node rr[MAX];
int a[405];
inline bool scan_d(int &num)
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
bool judge(int s,int f,int c,int r,LL v){
LL now=v;
while(s<f){
LL tot=1LL*(a[s+1]-a[s])*c;
if(now<tot){
now=v;
r--;
}
if(r<0||now<tot)
return false;
now-=tot;
s++;
}
return true;
}
int cmp(node a,node b){
return a.id<b.id;
}
int main()
{
int n,x,m,s,f,c,r;
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++){
scanf("%d",&a[i]);
}
LL v=1;
for(int i=1;i<=n;i++){
scan_d(rr[i].s);
scan_d(rr[i].f);
scan_d(rr[i].c);
scan_d(rr[i].r);
rr[i].id=rand();
}
sort(rr+1,rr+1+n,cmp);
for(int i=1;i<=n;i++){
s=rr[i].s;
f=rr[i].f;
c=rr[i].c;
r=rr[i].r;
// printf("%d\n",rr[i].id);
LL st=v;
while(!judge(s,f,c,r,st)){
st=st*2;
}
if(st==v)continue;
LL ed=st;
st=st/2;
while(st<=ed){
LL mid=(st+ed)/2;
if(judge(s,f,c,r,mid)){
v=mid;
ed=mid-1;
}
else
st=mid+1;
}
}
printf("%lld\n",v);
return 0;
}