二分&Gadgets for dollars and pounds

本文深入探讨了大数据开发领域的关键技术,包括Hadoop、Spark、Flink等,并结合实际案例阐述了这些技术在数据处理、分析和存储方面的应用。通过详细解析数据流处理流程和系统架构,为读者提供了一套全面的大数据开发解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

J - Gadgets for dollars and pounds
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 klines 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.

Sample Input

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;

给出该货币每天兑换美元和英镑的汇率,求购到满足需要的礼品数的最小天数


思路:用数组保存下美元和英镑每天的汇率,便于而时找出若干天内的最小汇率,把美元和英镑分开处理(本人被坑叙旧的出来的。。),之后二分猜测即可

下面上代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn=200005;

int n,m,k,s;
int d[maxn],p[maxn];
struct node
{
    int t,c;
} a[maxn],b[maxn];
int nd=0,np=0;

bool cmp(node a,node b)
{
    return a.c<b.c;
}

LL fun(int x)
{
    LL sum=0;
    int i,id,ip;
    for(i=id=ip=1; i<=k; i++)
    {                                         //确保每次选择最小花费
        if((id<=nd&&ip>np)||(id<=nd&&ip<=np&&a[id].c*d[x]<b[ip].c*p[x]))
            sum+=(LL)a[id++].c*d[x];
        else
            sum+=(LL)b[ip++].c*p[x];
    }
    return sum;
}

int main()
{
    //FIN;
    d[0]=p[0]=INF;
    int cd,cp;
    scanf("%d%d%d%d",&n,&m,&k,&s);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&d[i]);
        d[i]=min(d[i],d[i-1]);
    }
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&p[i]);
        p[i]=min(p[i],p[i-1]);
    }
    int t;
    LL c;
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d",&t,&c);
        if(t==1)
        {
            a[++nd].c=c;
            a[nd].t=i;
        }
        else
        {
            b[++np].c=c;
            b[np].t=i;
        }
    }
    sort(a+1,a+nd+1,cmp);
    sort(b+1,b+np+1,cmp);
    if(fun(n)>(LL)s) printf("-1\n");
    else
    {
        int lf=1,rg=n,mid;
        while(rg>lf)
        {
            mid=(rg+lf)>>1;
            if(fun(mid)<=(LL)s)
                rg=mid;
            else lf=mid+1;
        }
        printf("%d\n",lf);
        int cd=1,cp=1,i,id,ip;
        while(d[cd]>d[lf])            //找到美元最小汇率一天
            ++cd;
        while(p[cp]>p[lf])
            ++cp;
        for(i=id=ip=1; i<=k; i++)    //输出的顺序不一定,只要对即可,尝试用多种输出试一下
        {
            if((id<=nd&&ip>np)||(id<=nd&&ip<=np&&a[id].c*d[lf]<b[ip].c*p[lf]))
                printf("%d %d\n",a[id++].t,cd);
            else
                printf("%d %d\n",b[ip++].t,cp);
        }
        return 0;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值