zoukankan      html  css  js  c++  java
  • zoj 3726 Alice's Print Service【二分】【rmq】

    D - Alice's Print Service

    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.

    InputThe 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=s1 < 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.OutputFor 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

    可以用RMQ 也可以用线段树解决 趁机再学习一下RMQ
    复杂度:     处理以及查询
    1.朴素(即搜索) O(n)-O(n) 
    2.线段树(segment tree) O(n)-O(logn) 
    3.ST(实质是动态规划) O(nlogn)-O(1) 
    参考https://www.cnblogs.com/yoke/p/6949838.html
    http://www.voidcn.com/article/p-kolnqjue-bdw.html

    RMQ代码:
    #include<bits/stdc++.h>
    #define ll long long
    #define fmin(a,b) a>b?b:a
    using namespace std;
    const int MAXN=100009;
    const int MAX_log=18;
    ll s[MAXN],p[MAXN];
    ll dp[MAXN][MAX_log];
    void init(int n)
    {
        for(int i=1;i<=n;i++)dp[i][0]=s[i]*p[i];
        ll k=(ll)(log(n*1.0)/log(2.0));
        for(int j=1;j<=k;j++)
            for(int i=1;i-1+(1<<j)<=n;i++)
                dp[i][j]=fmin(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    }
    ll get(ll l,ll r)
    {
        ll k=(ll)(log((l-r+1)*1.0)/log(2.0));
        return fmin(dp[l][k],dp[r-(1<<k)+1][k]);
    }
    void solve(ll q,ll n)
    {
        ll index=lower_bound(s+1,s+n+1,q)-s;
        if(index>n||s[index]>q)--index;///询问的张数超过s[index]了或者越界要--index 【二分思想】
        ll res=q*p[index];///情况一
        if(index+1<=n)///情况二
        {
            res=fmin(res,get(index+1,n));
        }
        cout<<res<<'
    ';
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,m;
            scanf("%d %d",&n,&m);
            for(int i=1;i<=n;i++)scanf("%lld %lld",&s[i],&p[i]);
            init(n);
            ll tmp;
            for(int i=0;i<m;i++)
            {
                scanf("%lld",&tmp);
                solve(tmp,n);
            }
    //        int rb=lower_bound(num,num+6+1,n)-num;
    //        cout<<"rb num[rb]"<<endl;
    //        cout<<rb<<' '<<num[rb]<<endl;
        }
        return 0;
    }

    线段树代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    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));
        ///分割所求的区间[x,y]为[x,mid]U[mid+1,y]
    }
    int main()
    {
        ll n,m;
        int t;
        scanf("%d",&t);
        while(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 id=lower_bound(a+1,a+n+1,p)-a;///lower_bound 或者find  找不到值的时候会返回右区间的地址
                id--;///求最后一个小于p的下标
                ll ans=p*b[id];
                if(id+1<=n)///如果下标+1<=n
                {
                    ans=min(ans,Q(1,n,id+1,n,1));
                }
                printf("%lld
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    postman环境和全局变量设置语句
    2016 GitHub章鱼猫观察报告之开源统计
    Multiload-ng
    忠告初学者学习Linux系统的8点建议
    真有用?Snap和Flatpak 通吃所有发行版的打包方式。
    教你如何在Kali Linux 环境下设置蜜罐?
    下一代GNU/Linux显示服务Wayland 1.12正式发布
    为 Github 创造 Integration
    简单易懂的crontab设置工具集
    爆料喽!!!开源日志库Logger的剖析分析
  • 原文地址:https://www.cnblogs.com/guanwen769aaaa/p/11419366.html
Copyright © 2011-2022 走看看