zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分+前缀

    D. Gadgets for dollars and pounds
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.

    Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.

    Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.

    Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.

    Input

    First line contains four integers n, m, k, s (1 ≤ n ≤ 2·105, 1 ≤ k ≤ m ≤ 2·105, 1 ≤ s ≤ 109) — number of days, total number and required number of gadgets, number of burles Nura has.

    Second line contains n integers ai (1 ≤ ai ≤ 106) — the cost of one dollar in burles on i-th day.

    Third line contains n integers bi (1 ≤ bi ≤ 106) — the cost of one pound in burles on i-th day.

    Each of the next m lines contains two integers ti, ci (1 ≤ ti ≤ 2, 1 ≤ ci ≤ 106) — type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.

    Output

    If Nura can't buy k gadgets print the only line with the number -1.

    Otherwise the first line should contain integer d — the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di — the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n.

    In case there are multiple possible solutions, print any of them.

    Examples
    input
    5 4 2 2
    1 2 3 2 1
    3 2 1 2 3
    1 1
    2 1
    1 2
    2 2
    output
    3
    1 1
    2 3
    input
    4 3 2 200
    69 70 71 72
    104 105 106 107
    1 1
    2 2
    1 2
    output
    -1
    input
    4 3 1 1000000000
    900000 910000 940000 990000
    990000 999000 999900 999990
    1 87654
    2 76543
    1 65432
    output
    -1

    题意:你有n个天数的汇率,现在你有s元人民币,第i天可以随意换a[i]个人民币换1美元,b[i]个人民币换1英镑,但是不可以换了存起来,必须今天用掉;

       下面有m个东西,q表示只能用美元或者英镑买,需要的花费,你需要求最小的天数使得你买任意k个;

    思路:前缀最小值,存两个汇率最小的时候,分别在这两天买;

       将美元可以买跟英镑可以买的分开。。算花费最少的人民币可以买到东西;

       二分天数即可;

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=2e5+10,M=1e6+10,inf=1e9+10;
    const ll INF=1e18+10,mod=2147493647;
    
    ///数组大小
    vector<pair<int,int> >v[3];
    pair<int,int> mina[N],minb[N];
    int check(int x,int k,int s)
    {
        int a=mina[x].first;
        int b=minb[x].first;
        int sa=0,sb=0,si=0;
        while(1)
        {
            if(sa==v[1].size()&&sb==v[2].size())break;
            if(sa==v[1].size())
            {
                if(s<1LL*v[2][sb].first*b)
                    break;
                else
                    s-=v[2][sb].first*b,sb++,si++;
            }
            else if(sb==v[2].size())
            {
                if(s<1LL*v[1][sa].first*a)
                    break;
                else
                    s-=v[1][sa].first*a,sa++,si++;
            }
            else
            {
                if(1LL*v[1][sa].first*a<=1LL*v[2][sb].first*b)
                {
                    if(s<1LL*v[1][sa].first*a)
                        break;
                    else
                        s-=v[1][sa].first*a,sa++,si++;
                }
                else
                {
                    if(s<1LL*v[2][sb].first*b)
                        break;
                    else
                        s-=v[2][sb].first*b,sb++,si++;
                }
            }
        }
        if(si>=k)return 1;
        return 0;
    }
    void output(int x,int k,int s)
    {
        int a=mina[x].first;
        int b=minb[x].first;
        int sa=0,sb=0,si=0;
        while(si<k)
        {
            if(sa==v[1].size())
            {
                if(s<1LL*v[2][sb].first*b)
                    break;
                else
                    s-=v[2][sb].first*b,printf("%d %d
    ",v[2][sb].second,minb[x].second),sb++,si++;
            }
            else if(sb==v[2].size())
            {
                if(s<1LL*v[1][sa].first*a)
                    break;
                else
                    s-=v[1][sa].first*a,printf("%d %d
    ",v[1][sa].second,mina[x].second),sa++,si++;
            }
            else
            {
                if(1LL*v[1][sa].first*a<=1LL*v[2][sb].first*b)
                {
                    if(s<1LL*v[1][sa].first*a)
                        break;
                    else
                        s-=v[1][sa].first*a,printf("%d %d
    ",v[1][sa].second,mina[x].second),sa++,si++;
                }
                else
                {
                    if(s<1LL*v[2][sb].first*b)
                        break;
                    else
                        s-=v[2][sb].first*b,printf("%d %d
    ",v[2][sb].second,minb[x].second),sb++,si++;
                }
            }
        }
    }
    int main()
    {
        int n,k,m,s;
        scanf("%d%d%d%d",&n,&m,&k,&s);
        mina[0]=make_pair(inf,0);
        minb[0]=make_pair(inf,0);
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            if(x<mina[i-1].first)
                mina[i]=make_pair(x,i);
            else mina[i]=mina[i-1];
    
        }
        for(int i=1;i<=n;i++)
        {
            int y;
            scanf("%d",&y);
             if(y<minb[i-1].first)
                minb[i]=make_pair(y,i);
            else minb[i]=minb[i-1];
        }
        for(int i=1;i<=m;i++)
        {
            int t,x;
            scanf("%d%d",&t,&x);
            v[t].push_back(make_pair(x,i));
        }
        for(int i=1;i<=2;i++)
            sort(v[i].begin(),v[i].end());
        int st=1,en=n,ans=-1;
        while(st<=en)
        {
            int mid=(st+en)>>1;
            if(check(mid,k,s))
            {
                ans=mid;
                en=mid-1;
            }
            else
                st=mid+1;
        }
        printf("%d
    ",ans);
        if(ans!=-1)
        {
            output(ans,k,s);
        }
        return 0;
    }
  • 相关阅读:
    Java 递归、尾递归、非递归、栈 处理 三角数问题
    vmware虚拟机迁移导致的eth0消失问题
    c语言输入输出
    改动虚拟机镜像的rootpassword
    面试题之变态跳台阶
    努力是种病:放慢节奏,才能快速奔跑(转)
    做技术到底可以做到哪种地步-技术为什么越走越窄 (转)
    wpf 客户端【JDAgent桌面助手】开发详解(三) 瀑布流效果实现与UI虚拟化优化大数据显示
    我为什么写博客(转)
    入行必读:互联网行业薪酬等级!看看你值多少钱?(转)
  • 原文地址:https://www.cnblogs.com/jhz033/p/6790335.html
Copyright © 2011-2022 走看看