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. 安装Birt方法

    安装BIRT 方法: 博客地址:http://www.mamicode.com/info-detail-850588.html 注意:在 Install new Software 中输入地址:http ...

  2. Swift - LineChart绘制折线图

    LineChart,就使用Core Graphics和QuartzCore框架中的CAShapeLayer绘制.这样执行效率明显比堆砌UIView的方法效率高--占用资源少,执行快. 看看CALaye ...

  3. NYOJ题目1162数字

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr8AAAJ/CAIAAAD+kywNAAAgAElEQVR4nO3dO1LjzN4H4G8T5CyE2A ...

  4. RabbitMQ驱动简单例子

    using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collections.Generic; ...

  5. c++11的初始化

    c++11 中类型初始更加方便 比如     vector<int> vec = {1,2,3}; vector<int> vec{1,2,3}; map<string, ...

  6. 《Spring 3.x 企业应用开发实战》目录

    图书信息:陈雄华 林开雄 编著 ISBN 978-7-121-15213-9 概述: 第1章:对Spring框架进行宏观性的概述,力图使读者建立起对Spring整体性的认识. 第2章:通过一个简单的例 ...

  7. CentOS下用yum配置php+mysql+apache(LAMP)

    #安装需要的包,有依赖关系,自动帮你解决 yum install httpd mysql mysql-server php php-gd php-mbstring php-mysql #启动httpd ...

  8. 檢查RAC狀態

    1.使用srvctl工具檢查RAC當前配置和狀態 $ srvctl config database -h Displays the configuration for the database. Us ...

  9. COALESCE NVL NVL2 DECODE

    1 COALESCE 語法:COALESCE(expr1, expr2, ..., exprn) n>=2 作用:COALESCE returns the first non-null expr ...

  10. mysql_multi启动数据库

    1.初始化数据库 在$mysql_base目录下,新增加存放data的文件夹,用mysql_install_db命令执行初始化 [root@ora11g scripts]# ./mysql_insta ...