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 ;
}

CF# Educational Codeforces Round 3 D. Gadgets for dollars and pounds的更多相关文章

  1. Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心

    D. Gadgets for dollars and pounds 题目连接: http://www.codeforces.com/contest/609/problem/C Description ...

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

  3. CodeForce---Educational Codeforces Round 3 D. Gadgets for dollars and pounds 正题

    对于这题笔者无解,只有手抄一份正解过来了: 基本思想就是 : 二分答案,对于第x天,计算它最少的花费f(x),<=s就是可行的,这是一个单调的函数,所以可以二分. 对于f(x)的计算,我用了nl ...

  4. CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

    题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...

  5. CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种

    题目链接:http://codeforces.com/problemset/problem/609/E 大致就是有一棵树,对于每一条边,询问包含这条边,最小的一个生成树的权值. 做法就是先求一次最小生 ...

  6. CF# Educational Codeforces Round 3 F. Frogs and mosquitoes

    F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input stan ...

  7. CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  8. CF# Educational Codeforces Round 3 C. Load Balancing

    C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. CF# Educational Codeforces Round 3 B. The Best Gift

    B. The Best Gift time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

随机推荐

  1. jquery.validation.js 表单验证

    jquery.validation.js 表单验证   官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuer ...

  2. Eclipse 安装SVN

    地址:http://wenku.baidu.com/link?url=ntQy2-1CjlNyUpO0-4uhROrc9jCo12Yifh7MkPULmY_dCybl6SEH99SxYxEbZQEiW ...

  3. Jmeter 中通过(_time函数)获取10位时间戳的方法

    meter的__time函数作用是取当前时间的时间戳,默认取的时间精确到了毫秒级别,所以获取的时间戳默认是13位的.  下图为取10位的时间戳的函数表达式(时间精确到秒) 

  4. 磁盘空间占满inode结点没用完 并删除了文件但是释放不了

    lsof  |grep delete lsof(list system open file )可显示系统打开的文件,以root身份运行. 很多时候文件正在被占用,即使删除了,也无法释放空间,只有停 了 ...

  5. Bitmap在Java中的实现和应用

    >>40亿数据排序问题 给定一个最多包含40亿个随机排列的32位整数的顺序文件,找出一个不在文件中的32位整数(在文件中至少缺失这样一个数——为什么?).在具有足够内存的情况下,如何解决该 ...

  6. Install PIL on mac osX10.9

    follow this instruction: http://blog.csdn.net/liushuaikobe/article/details/8729652 and if you encoun ...

  7. <转> jsp:include 乱码问题解决

    jsp include页面出现乱码问题的几种通用解决方法: 1.当jsp include动态文件时(jsp文件)可以在被include的jsp文件头部加上代码: <%@ page languag ...

  8. Oralce sysaux WRH$_ACTIVE_SESSION_HISTORY清理

    In this Document Symptoms Cause Solution References Symptoms sysaux表空間的WRH$_ACTIVE_SESSION_HISTORY表變 ...

  9. 6-03使用SQL语句一次型向表中插入多行数据

    通过将现有表中的数据添加到已存在的表中: INSERT INTO <表名><列名> SELECT<列名> FROM<源表名> 将UserInfo的数据添 ...

  10. 为什么接口类型可以直接new?

    Runnable rn = new Runnable() { public void run() { } }; 实际相当于,jdk会自动生成一个匿名内部类,完成职责: class Anomymous ...