A. 打印收费(思维+二分+后缀最小/st/线段树)

62 篇文章 2 订阅
40 篇文章 0 订阅

思路:首先找到一个最近的小于等于当前纸张的价格,然后找一个办法快速统计后面的价格中最小值就好了。

感觉这题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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值