思路:首先找到一个最近的小于等于当前纸张的价格,然后找一个办法快速统计后面的价格中最小值就好了。
感觉这题cf出过阿。
找到位置,由于存在单调性,采用二分即可。要注意一下二分找不到的位置。
比较推荐的常数小的是思维的维护后缀最小值。
献上我池巨的代码:维护后缀最小值O(1)查询比较即可。
讲一下其他方法,维护一段序列的最小值并查询,可以st表可以线段树,试了一下st表。但是二分卡了边界。(这就去更一下二分的文章)
原来wa了8发的原因:找不到的时候l=n+1;虽然取了边界,l--,但是我取的st边界pos=l+1,仍然是0;导致wa。果然还是思维的方式更不容易出错
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
LL n,m;
LL a[maxn],s[maxn],q[maxn];
LL f[maxn][40];
int main(void)
{
LL n,m;n=read();m=read();
///for(LL i=0;i<=n+10;i++) s[i]=1e18;
for(int i=1;i<=n;i++) {
s[i]=read();
q[i]=read();
a[i]=s[i]*q[i];
}
LL t=log(n)/log(2)+1;
for(int i=1;i<=n;i++) f[i][0]=a[i];
for(int j=1;j<t;j++)
for(int i=1;i<=n-(1<<j)+1;i++)
f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
while(m--){
LL x;x=read();
if(n==1){
printf("%lld\n",x*q[1]);
continue;
}
/// LL l=lower_bound(s+1,s+1+n,x)-s;
LL l=upper_bound(s+1,s+1+n,x)-s-1;
LL res=x*q[l];
if(l==0) res=1e18;
LL pos=l+1;
if(pos<=n){
LL k=log(n-pos+1)/log(2);
LL ans2=min(f[pos][k],f[n-(1<<k)+1][k]);
printf("%lld\n",min(ans2,res));
}
else printf("%lld\n",res);
/// debug(ans2);
/// debug(res);
/// LL ans2=q[l]*x;
/// debug(ans1);debug(ans2);
}
return 0;
}