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. C语言文件操作相关函数

    在实际应用中,我们往往需要对文件进行操作,下面我将介绍C语言的一些关于操作文件的函数. 一.计算机文件 计算机文件是以计算机硬盘为载体存储在计算机上的信息集合,是存储在某种长期储存设备上的一段数据流. ...

  2. hdu1162(最小生成树 prim or kruscal)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 意义:给出一些点,用线问使所有点直接或间接连通,需要多长: 思路:裸最小生成树: 法1: pri ...

  3. 三、jQuery--jQuery插件--jQuery插件——Validation Plugin

    简介: 客户端验证:现代网站填写表单时,几乎一定会采用的方式. 优点:1.可以减少服务器压力 2.缩短用户等待时间和提升用户体验 jQuery有很多表单验证插件:https://plugins.jqu ...

  4. nginx 配一个简单的静态文件服务器 和一个虚似机

    下面是个图片服务器: server { listen ; server_name img.xxx.xxx.com; root /data/site/img.xxx.xxx.com; access_lo ...

  5. My97DatePicker日期范围限制

    1.动态时间范围限制: 可以通过系统给出的动态变量,如%y(当前年),%M(当前月)等来限制日期范围,还可以通过{}进行表达式运算,如:{%d+1}:表示明天. 格式 说明 %y  当前年 %M  当 ...

  6. execl一个工作薄中有几个个工作表,将这几个个工作表分别保存到不同execl文件中

    用宏运行: Sub QEJebel()    Dim sh As Worksheet    Dim Pa As String    Pa = ThisWorkbook.Path    For Each ...

  7. PHP面试题集

    汗~~做了一下网络上的php题目,不知不觉做到现在.....把答案贴出来如果有问题请欢迎补充和指正 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分)   $a = da ...

  8. .NET生成带Logo的二维码

    使用ThoughtWorks.QRCode生成,利用这个库来生成带Logo的二维码(就是中间嵌了一个图片的二维码),直接见代码: HttpContext context = HttpContext.C ...

  9. 【翻译一】java-并发

    Lesson: Concurrency Computer users take it for granted that their systems can do more than one thing ...

  10. javascript中的true和false

    今天遇到一个问题,执行下面的代码返回true还是false?请说明理由 console.log([] == ![]) 在浏览器中运行了一下,发现结果是true.为什么会这样呢?于是查找了相关的资料. ...