啥都不会啊!怎么办啊!

Fitz

慢慢来生活总会好起来的!!!

Gadgets for dollars and pounds CodeForces - 609D

Nura wants to buy k gadgets. She has only sburles 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 integersti, 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 kgadgets. 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天里进行购买,每天的汇率不同,汇率告诉你

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



由于本人太菜这题是我写过的最难的二分 (靠题解才能存活的菜鸡)
(这题没有long long WA到吐)
对天数进行二分,am[maxn]这个对应着在1~maxn中兑换英镑的最小值ida[maxn],最小值对应的天数
am[maxn]这个对应着在1~maxn中兑换美元的最小值idb[maxn] 最小值对应的天数

求出每件物品的最小花费排序选出前k个就行了


这题需要理清思路 仔细思考

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 #define maxn 200010
 8 int a[maxn],b[maxn],c[maxn],t[maxn];
 9 int am[maxn],bm[maxn],ida[maxn],idb[maxn],id[maxn];
10 int n,m,k,s;
11 struct node
12 {
13     long long cost;
14     int idx;
15 }qu[maxn];
16 int cmp(node a,node b)
17 {
18     return a.cost<b.cost;
19 }
20 long long check(int x)
21 {
22     long long ans=0;
23     for (int i=1 ;i<=m ;i++ ){
24         if (t[i]==1) qu[i].cost=(long long)c[i]*(long long)am[x];
25         else qu[i].cost=(long long)c[i]*(long long)bm[x];
26         qu[i].idx=i;
27     }
28     sort(qu+1,qu+1+m,cmp);
29     for (int i=1 ;i<=k ;i++)
30         ans+=qu[i].cost;
31     return ans;
32 }
33 int main() {
34     while(scanf("%d%d%d%d",&n,&m,&k,&s)!=EOF){
35         am[0]=bm[0]=100000000;
36         for (int i=1 ;i<=n ;i++){
37             scanf("%d",&a[i]);
38             if (a[i]<am[i-1]) {
39                 am[i]=a[i];
40                 ida[i]=i;
41             }else {
42                 am[i]=am[i-1];
43                 ida[i]=ida[i-1];
44             }
45         }
46         for (int i=1 ; i<=n ; i++) {
47             scanf("%d",&b[i]);
48             if (b[i]<bm[i-1]) {
49                 bm[i]=b[i];
50                 idb[i]=i;
51             } else {
52                 bm[i]=bm[i-1];
53                 idb[i]=idb[i-1];
54             }
55         }
56         for (int i=1 ;i<=m ;i++)
57             scanf("%d%d",&t[i],&c[i]);
58         long long r=n,l=0,mid,d=-1;
59         while(l<=r) {
60             mid=(l+r)/2;
61             if (check(mid)<=s) {
62                 r=mid-1;
63                 d=mid;
64                 for (int i=1 ;i<=k ;i++){
65                     id[i]=qu[i].idx;
66                 }
67             }else l=mid+1;
68         }
69         printf("%lld\n",d);
70         if(d==-1) continue;
71         int x=d;
72         for (int i=1 ;i<=k ;i++){
73             printf("%d %d\n",id[i],t[id[i]]==1?ida[x]:idb[x]);
74         }
75     }
76     return 0;
77 }

 





posted @ 2018-03-06 22:14  Fitz~  阅读(283)  评论(0编辑  收藏  举报