Alice's Print Service(二分+RMQ|线段树)

Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money.
For example, the price when printing less than 100 pages is 20 cents per page, but when printing not less than 100 pages, you just need to pay only 10 cents per page. It's easy to figure out that if you want to print 99 pages, the best choice is to print an extra blank page so that the money you need to pay is 100 × 10 cents instead of 99 × 20 cents.
Now given the description of pricing strategy and some queries, your task is to figure out the best ways to complete those queries in order to save money.

Input

The first line contains an integer T (≈ 10) which is the number of test cases. Then T cases follow.
Each case contains 3 lines. The first line contains two integers n, m (0 < n, m ≤ 10 5 ). The second line contains 2n integers s 1, p 1 , s 2, p 2 , ..., s n, p n (0=s 1 < s 2 < ... < s n ≤ 10 9 , 10 9 ≥ p 1 ≥ p 2 ≥ ... ≥ p n ≥ 0).. The price when printing no less than s i but less than s i+1 pages is p i cents per page (for i=1..n-1). The price when printing no less than s n pages is p n cents per page. The third line containing m integers q 1 .. q m (0 ≤ q i ≤ 10 9 ) are the queries.

Output

For each query q i, you should output the minimum amount of money (in cents) to pay if you want to print q i pages, one output in one line.

Sample Input

1
2 3
0 20 100 10
0 99 100

Sample Output

0
1000
1000

给出不同范围内的价格,求最少需要花多少钱

对于每一个询问,二分找到其所在区间,然后比较打印正好的页数的价格和多于要求页数的价格(即区间端点),求一个最小值

暴力妥妥的超时,然后套上了线段树,WA了好多次

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f3f3f3fLL;
const int N=1e5+20;
ll a[N],b[N],T[4*N+50];
void pushup(int k)
{
    T[k]=min(T[k*2],T[k*2+1]);
}
void build(int l,int r,int k)
{
    if(l==r)
    {
        T[k]=a[l]*b[l];
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
    pushup(k);
}
ll Q(int L,int R,int x,int y,int k)
{
    if(L==x&&R==y)///要找到准确的对应的区间
        return T[k];
    int mid=(L+R)/2;
    if(y<=mid)
    {
        return Q(L,mid,x,y,k*2);///需要查询的区间都在同一侧,可以直接返回
    }
    else if(x>mid)
    {
        return Q(mid+1,R,x,y,k*2+1);
    }
    else
        return min(Q(L,mid,x,mid,k*2),Q(mid+1,R,mid+1,y,k*2+1));///将需要查询的区间由mid分割成两部分
}
int main()
{
    ll n,m;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(T,inf,sizeof(T));
        scanf("%lld%lld",&n,&m);
        for(int i=1; i<=n; i++)
            scanf("%lld%lld",&a[i],&b[i]);
        build(1,n,1);
        ll p;
        while(m--)
        {
            scanf("%lld",&p);
            ll ans;
            if(p>=a[n])
                ans=p*b[n];
            else if(p==0)
                ans=0;
            else
            {
                ll id=lower_bound(a+1,a+n+1,p)-a;
//                cout<<id<<'\n';
                if(id>=1&&id<=n)
                {
                    if(a[id]>p)
                        id--;
//                cout<<ans<<'\n';
                    ans=min(b[id]*p,Q(1,n,id+1,n,1));
                }
                else
                    ans=p*b[n];
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}

RMQ 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,m;
ll a[N],b[N];
ll f[N][20];///f[i][j]表示从i位起2^j个数中的最大值,即【i,i+(1<<j)-1】
void ST_prework()
{
    for(int i=1; i<=n; i++)
        f[i][0]=a[i]*b[i];
    ll imax=(ll)(log(n*1.0)/log(2.0));
    for(int i=1; i<=imax; i++)
    {
        for(int j=1; j+(1<<i)-1<=n; j++) ///右端点为j+(1<<i)-1,-1是因为要包含j自己
            f[j][i]=min(f[j][i-1],f[j+(1<<(i-1))][i-1]);
    }
}
ll ST_query(ll l,ll r)///求(l,r)中的最大值
{
    ll k=(ll)(log((l-r+1)*1.0)/log(2.0));///区间长度r-l+1
    return min(f[l][k],f[r-(1<<k)+1][k]);///第1个区间:[l,l+(1<<k)-1];第2个区间:(r,(1<<k)+1~r)
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            scanf("%lld%lld",&a[i],&b[i]);
        ST_prework();
//        cout<<a[1]<<' '<<a[2]<<' '<<b[1]<<' '<<b[2]<<'\n';
        while(m--)
        {
            ll q,ans;
            scanf("%lld",&q);
            ll id=lower_bound(a+1,a+n+1,q)-a;
//            cout<<id<<'\n';
            if(id>n||a[id]>q)
                id--;
            ans=q*b[id];
//            cout<<ans<<'\n';
            if(id<n)
                ans=min(ans,ST_query(id+1,n));
            cout<<ans<<'\n';
        }
    }
    return 0;
}

 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
6539次浏览 63人参与
# 你的实习产出是真实的还是包装的? #
1304次浏览 32人参与
# MiniMax求职进展汇总 #
23225次浏览 302人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7077次浏览 37人参与
# 简历第一个项目做什么 #
31329次浏览 315人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186521次浏览 1115人参与
# 巨人网络春招 #
11213次浏览 223人参与
# 研究所笔面经互助 #
118787次浏览 577人参与
# 面试紧张时你会有什么表现? #
30416次浏览 188人参与
# 简历中的项目经历要怎么写? #
309572次浏览 4163人参与
# 职能管理面试记录 #
10722次浏览 59人参与
# AI时代,哪些岗位最容易被淘汰 #
62757次浏览 749人参与
# 网易游戏笔试 #
6374次浏览 83人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6993次浏览 154人参与
# 腾讯音乐求职进展汇总 #
160437次浏览 1107人参与
# 从哪些方向判断这个offer值不值得去? #
56712次浏览 357人参与
# 正在春招的你,也参与了去年秋招吗? #
362741次浏览 2632人参与
# 你怎么看待AI面试 #
179417次浏览 1182人参与
# 小红书求职进展汇总 #
226905次浏览 1357人参与
# 你的房租占工资的比例是多少? #
92146次浏览 896人参与
# 校招笔试 #
467654次浏览 2954人参与
# 经纬恒润求职进展汇总 #
155713次浏览 1085人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务