Codeforces 609D 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 valuesdi 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天,M个物品,现在要买K个物品,此时有S金币。

但是这个人不能用金币去买物品,必须兑换等价的货币才行。

对应第二行输入是第i天能够换取的第一种货币的汇率。

对应第三行输入时第i天能够换取的第二种货币的汇率。

再接下来M行,每个物品有两个元素,第一个数如果是1就表示这个物品需要用第一种货币去购买,如果是2就表示这个物品需要第二种货币区购买.

问最少可以用多少天购买到不同种类的K个物品。


思路:


1、如果我们用x天能够购买到K个物品,那么用x+1天也一定可以,所以这里包含一个单调性,那么我们二分天数。


2、对应在这些天内,我们贪心的找到兑换A货币最便宜的那天,以及兑换B货币最便宜的那天。

那么我们再求出每种物品对应在最便宜那天去购买需要的金币数量。

对其排序贪心购买即可。

如果是可行方案,就更新答案。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll __int64
struct node
{
    ll ty,cost,pos;
    ll ned;
}c[350000];
struct node2
{
    ll id,day;
}ans[350000];
ll a[350000];
ll b[350000];
ll n,m,k,s;
ll cmp(node a,node b)
{
    return a.ned<b.ned;
}
ll Slove(ll mid)
{
    ll daya=-1,dayb=-1;
    ll mina=0x3f3f3f3f;
    ll minb=0x3f3f3f3f;
    for(ll i=1;i<=mid;i++)
    {
        if(a[i]<mina)
        {
            mina=a[i];
            daya=i;
        }
        if(b[i]<minb)
        {
            minb=b[i];
            dayb=i;
        }
    }
    for(ll i=1;i<=m;i++)
    {
        if(c[i].ty==1)c[i].ned=mina*c[i].cost;
        else c[i].ned=minb*c[i].cost;
    }
    sort(c+1,c+1+m,cmp);
    ll sum=0;
    for(ll i=1;i<=k;i++)
    {
        sum+=c[i].ned;
    }
    if(sum<=s)
    {
        for(ll i=1;i<=k;i++)
        {
            ans[i].id=c[i].pos;
            if(c[i].ty==1)ans[i].day=daya;
            else ans[i].day=dayb;
        }
        return 1;
    }
    else return 0;
}
int main()
{
    while(~scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&s))
    {
        for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]);
        for(ll i=1;i<=n;i++)scanf("%I64d",&b[i]);
        for(ll i=1;i<=m;i++)scanf("%I64d%I64d",&c[i].ty,&c[i].cost),c[i].pos=i;
        ll l=1;
        ll r=n;
        ll anss=-1;
        while(r-l>=0)
        {
            ll mid=(l+r)/2;
            if(Slove(mid)==1)
            {
                anss=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%I64d\n",anss);
        if(anss>0)
        {
            for(ll i=1;i<=k;i++)
            {
                printf("%I64d %I64d\n",ans[i].id,ans[i].day);
            }
        }
    }
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值