CF# Educational Codeforces Round 3 D. 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.

Sample test(s)
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样东西,规定一定要用美元或者英镑买,且价格一定,每天对美元和英镑的汇率都会给出。
问在n天内能否该买m样东西。每样东西只能买一次。
如果可以,输出最小天数,并要求输出方案。
分析:
因为东西一直在那,所以使用相同货币的商品肯定是从便宜到贵买的。
而且,在一定天数内,肯定会选择汇率最低的那一天买。
所以二分天数,然后枚举美元和英镑各买多少个。判断一下。
  1 /**
  2 Create By yzx - stupidboy
  3 */
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cmath>
  8 #include <deque>
  9 #include <vector>
 10 #include <queue>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <map>
 14 #include <set>
 15 #include <ctime>
 16 #include <iomanip>
 17 using namespace std;
 18 typedef long long LL;
 19 typedef double DB;
 20 #define MIT (2147483647)
 21 #define INF (1000000001)
 22 #define MLL (1000000000000000001LL)
 23 #define sz(x) ((int) (x).size())
 24 #define clr(x, y) memset(x, y, sizeof(x))
 25 #define puf push_front
 26 #define pub push_back
 27 #define pof pop_front
 28 #define pob pop_back
 29 #define mk make_pair
 30 
 31 inline int Getint()
 32 {
 33     int Ret = 0;
 34     char Ch = ' ';
 35     bool Flag = 0;
 36     while(!(Ch >= '0' && Ch <= '9'))
 37     {
 38         if(Ch == '-') Flag ^= 1;
 39         Ch = getchar();
 40     }
 41     while(Ch >= '0' && Ch <= '9')
 42     {
 43         Ret = Ret * 10 + Ch - '0';
 44         Ch = getchar();
 45     }
 46     return Flag ? -Ret : Ret;
 47 }
 48 
 49 const int N = 200010;
 50 int n, m, k;
 51 LL s;
 52 LL c[2][N];
 53 int minValueIndex[2][N];
 54 struct Gadget
 55 {
 56     LL value;
 57     int idx;
 58     inline bool operator <(const Gadget &t) const
 59     {
 60         return value < t.value;
 61     }
 62 } arr[2][N];
 63 int len[2];
 64 LL sum[2][N];
 65 
 66 inline void Input()
 67 {
 68     n = Getint();
 69     m = Getint();
 70     k = Getint();
 71     s = Getint();
 72     for(int i = 0; i < 2; i++)
 73         for(int j = 1; j <= n; j++) c[i][j] = Getint();
 74     for(int i = 1; i <= m; i++)
 75     {
 76         int t = Getint() - 1;
 77         int value = Getint();
 78         len[t]++;
 79         arr[t][len[t]].value = value, arr[t][len[t]].idx = i;
 80     }
 81 }
 82 
 83 inline void Solve()
 84 {
 85     if(m < k)
 86     {
 87         printf("-1\n");
 88         return;
 89     }
 90     
 91     for(int i = 0; i < 2; i++)
 92     {
 93         sort(arr[i] + 1, arr[i] + 1 + len[i]);
 94         for(int j = 1; j <= len[i]; j++)
 95             sum[i][j] = sum[i][j - 1] + arr[i][j].value;
 96         
 97         for(int j = 1; j <= n; j++)
 98             minValueIndex[i][j] = j;
 99         for(int j = 2; j <= n; j++)
100             if(c[i][j] >= c[i][j - 1])
101             {
102                 minValueIndex[i][j] = minValueIndex[i][j - 1];
103                 c[i][j] = c[i][j - 1];
104             }
105     }
106     
107     int left = 1, right = n, mid, ret = -1, cnt = -1;
108     while(left <= right)
109     {
110         mid = (left + right) >> 1;
111         
112         LL mc[2];
113         for(int i = 0; i < 2; i++) mc[i] = c[i][mid];
114         bool flag = 0;
115         for(int i = max(0, k - len[1]); i <= min(k, len[0]); i++)
116             if(sum[0][i] * mc[0] + sum[1][k - i] * mc[1] <= s)
117             {
118                 flag = 1, cnt = i;
119                 break;
120             }
121         
122         if(flag)
123         {
124             ret = mid;
125             right = mid - 1;
126         }
127         else left = mid + 1;
128     }
129     
130     printf("%d\n", ret);
131     if(ret != -1)
132     {
133         for(int i = 1; i <= cnt; i++)
134             printf("%d %d\n", arr[0][i].idx, minValueIndex[0][ret]);
135         for(int i = 1; i <= k - cnt; i++)
136             printf("%d %d\n", arr[1][i].idx, minValueIndex[1][ret]);
137     }
138 }
139 
140 int main()
141 {
142     freopen("a.in", "r", stdin);
143     Input();
144     Solve();
145     return 0;
146 }
View Code

 



posted @ 2015-12-22 22:00  yanzx6  阅读(208)  评论(0编辑  收藏  举报