CF# Educational Codeforces Round 3 D. Gadgets for dollars and pounds

时间:2022-11-14 23:47:34
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样东西。每样东西只能买一次。
如果可以,输出最小天数,并要求输出方案。
分析:
因为东西一直在那,所以使用相同货币的商品肯定是从便宜到贵买的。
而且,在一定天数内,肯定会选择汇率最低的那一天买。
所以二分天数,然后枚举美元和英镑各买多少个。判断一下。
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
int n, m, k;
LL s;
LL c[][N];
int minValueIndex[][N];
struct Gadget
{
LL value;
int idx;
inline bool operator <(const Gadget &t) const
{
return value < t.value;
}
} arr[][N];
int len[];
LL sum[][N]; inline void Input()
{
n = Getint();
m = Getint();
k = Getint();
s = Getint();
for(int i = ; i < ; i++)
for(int j = ; j <= n; j++) c[i][j] = Getint();
for(int i = ; i <= m; i++)
{
int t = Getint() - ;
int value = Getint();
len[t]++;
arr[t][len[t]].value = value, arr[t][len[t]].idx = i;
}
} inline void Solve()
{
if(m < k)
{
printf("-1\n");
return;
} for(int i = ; i < ; i++)
{
sort(arr[i] + , arr[i] + + len[i]);
for(int j = ; j <= len[i]; j++)
sum[i][j] = sum[i][j - ] + arr[i][j].value; for(int j = ; j <= n; j++)
minValueIndex[i][j] = j;
for(int j = ; j <= n; j++)
if(c[i][j] >= c[i][j - ])
{
minValueIndex[i][j] = minValueIndex[i][j - ];
c[i][j] = c[i][j - ];
}
} int left = , right = n, mid, ret = -, cnt = -;
while(left <= right)
{
mid = (left + right) >> ; LL mc[];
for(int i = ; i < ; i++) mc[i] = c[i][mid];
bool flag = ;
for(int i = max(, k - len[]); i <= min(k, len[]); i++)
if(sum[][i] * mc[] + sum[][k - i] * mc[] <= s)
{
flag = , cnt = i;
break;
} if(flag)
{
ret = mid;
right = mid - ;
}
else left = mid + ;
} printf("%d\n", ret);
if(ret != -)
{
for(int i = ; i <= cnt; i++)
printf("%d %d\n", arr[][i].idx, minValueIndex[][ret]);
for(int i = ; i <= k - cnt; i++)
printf("%d %d\n", arr[][i].idx, minValueIndex[][ret]);
}
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}