(CodeForces - 609D)Gadgets for dollars and pounds

(CodeForces - 609D)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天在m件商品中购买k件。现在一共有s个卢比,对于每一天可以用a[i]个卢比换一个美元,用b[i]个卢比换一块英镑,对于每一件商品只能用它规定的货币购买,1代表美元,2代表英镑,问至少需要多少天才能买到k件商品,如果不能购买到k件就输出-1.

思路:如果x天能买到k件商品,那么x+1天也可以,这里就有一个单调性,所以我们可以二分购买的天数。至于怎么判断这个天数是否合理:我们可以处理出1 - x天里面,只能用美元购买的商品所需最少的卢比的那天和只能用英镑购买的商品所需最少的卢比的那天,用这两天来买这k件商品,看所用的卢比是否超过s。

#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn=200005;
int n,m,k,s;
LL a[maxn],b[maxn];

struct node1
{
    int id,type;
    LL pay,cost;//pay为支付的外币价格,cost为实际的卢比价格 
}c[maxn];

bool cmp(node1 a,node1 b)
{
    return a.cost<b.cost;
}

struct node2
{
    int id,day;
}ans[maxn];

bool check(int x)
{
    int daya,dayb;
    LL mina=INF,minb=INF;
    for(int i=1;i<=x;i++)
    {
        if(a[i]<mina) 
        {
            mina=a[i];
            daya=i;
        }
        if(b[i]<minb)
        {
            minb=b[i];
            dayb=i;
        }
    }
    for(int i=1;i<=m;i++)
    {
        if(c[i].type==1) c[i].cost=c[i].pay*mina;
        else c[i].cost=c[i].pay*minb;
    }
    sort(c+1,c+1+m,cmp);
    LL sum=0;
    for(int i=1;i<=k;i++) sum+=c[i].cost;
    if(sum<=s)
    {
        for(int i=1;i<=k;i++)
        {
            ans[i].id=c[i].id;
            if(c[i].type==1) ans[i].day=daya;
            else ans[i].day=dayb;
        }
        return true;
    }
    return false;
}

int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&k,&s))
    {
        for(int i=1;i<=n;i++) scanf("%lld",a+i);
        for(int i=1;i<=n;i++) scanf("%lld",b+i);
        for(int i=1;i<=m;i++) 
        {
            scanf("%d%lld",&c[i].type,&c[i].pay);
            c[i].id=i;
        }
        int lo=1,hi=n,mid;
        int sumday=-1;
        while(lo<=hi)
        {
            mid=(lo+hi)>>1;
            if(check(mid))
            {
                sumday=mid;
                hi=mid-1;
            }
            else lo=mid+1;
        }
        printf("%d\n",sumday);
        if(sumday>0)
        {
            for(int i=1;i<=k;i++)
                printf("%d %d\n",ans[i].id,ans[i].day);
        }
    }
}
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值