Gadgets for dollars and pounds CodeForces - 609D (二分)

Gadgets for dollars and pounds

CodeForces - 609D


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.

Example
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
有m种商品,可以购买,每种商品只能用美元或英镑二者其一购买

   你现在有s元,既不是美元,也不是英镑,需要兑换才能购买

   你需要买k种商品

   你可以在n天里进行购买,每天的汇率不同,汇率告诉你

   问你花费最少多少天,可以完成购买任务。

code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 200010;
int a[maxn],b[maxn],c[maxn],t[maxn];
int am[maxn],bm[maxn],ida[maxn],idb[maxn],id[maxn];
int n,m,k,s;
struct node{
    ll cost;
    int idx;
}qu[maxn];

int cmp(node a,node b){
    return a.cost < b.cost;
}
ll check(int x){
    ll ans = 0;
    for(int i = 1; i <= m; i++){
        if(t[i] == 1) qu[i].cost = (ll)c[i] * (ll)am[x];
        else qu[i].cost = (ll)c[i] * (ll)bm[x];
        qu[i].idx = i;//保存下每个物品的编号
    }
    sort(qu+1,qu+1+m,cmp);//排序后选出前k个求和保证花费最小
    for(int i = 1; i <= k; i++){
        ans += qu[i].cost;
    }
    return ans;
}
int main(){
    scanf("%d%d%d%d",&n,&m,&k,&s);
    am[0] = bm[0] = 100000000;
    for(int i = 1; i <= n; i++){
        scanf("%d",&a[i]);
        if(a[i] < am[i-1]){
            am[i] = a[i];
            ida[i] = i;
        }
        else{
            am[i] = am[i-1];
            ida[i] = ida[i-1];
        }
    }
    for(int i = 1; i <= n; i++){
        scanf("%d",&b[i]);
        if(b[i] < bm[i-1]){
            bm[i] = b[i];
            idb[i] = i;
        }
        else{
            bm[i] = bm[i-1];
            idb[i] = idb[i-1];
        }
    }
    //对于英镑的美元的汇率只储存越来越小的和其对应天数,即如果下一天汇率变大,那就还是存那个小的那一天的也就是都在那一天买
    //这样就可以保证汇率递减,天数递增,使得有序
    for(int i = 1; i <= m; i++){
        scanf("%d%d",&t[i],&c[i]);
    }
    ll r = n,l = 1,mid,d = -1;
    while(l <= r){
        mid = (l + r) / 2;//二分出天数,根据我们储存的汇率的特点,这一天一定是当前的最小汇率,因为再往前的话汇率只可能不变或变大
                          //因此check函数中算每个物品花费的钱时,汇率都是乘的这一天的,保证了花费最小
        if(check(mid) <= s){//如果花费可以小于s说明可以天数再靠前一点(即花费再大一点)
            r = mid - 1;//所以向左移动
            d = mid;//保存下满足条件的天数
            for(int i = 1; i <= k; i++){//保存下所买的物品编号
                id[i] = qu[i].idx;
            }
        }
        else{//否则右移
            l = mid + 1;
        }
    }
    printf("%lld\n",d);
    int x = d;
    if(d != -1){
        for(int i = 1; i <= k; i++){
            printf("%d %d\n",id[i],t[id[i]] == 1 ? ida[x] : idb[x]);//打印出物品编号,和对应物品花费最小汇率的对应天数
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值