Nura wants to buy k gadgets. She has only sburles 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 integersti, 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 kgadgets. 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 values di 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.

Example

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种商品,可以购买,每种商品只能用美元或英镑二者其一购买

你现在有s元,既不是美元,也不是英镑,需要兑换才能购买

你需要买k种商品

你可以在n天里进行购买,每天的汇率不同,汇率告诉你

问你花费最少多少天,可以完成购买任务。


由于本人太菜这题是我写过的最难的二分 (靠题解才能存活的菜鸡)
(这题没有long long WA到吐)
对天数进行二分,am[maxn]这个对应着在1~maxn中兑换英镑的最小值ida[maxn],最小值对应的天数
am[maxn]这个对应着在1~maxn中兑换美元的最小值idb[maxn] 最小值对应的天数

求出每件物品的最小花费排序选出前k个就行了

这题需要理清思路 仔细思考
 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 200010
int a[maxn],b[maxn],c[maxn],t[maxn];
int am[maxn],bm[maxn],ida[maxn],idb[maxn],id[maxn];
int n,m,k,s;
struct node
{
long long cost;
int idx;
}qu[maxn];
int cmp(node a,node b)
{
return a.cost<b.cost;
}
long long check(int x)
{
long long ans=;
for (int i= ;i<=m ;i++ ){
if (t[i]==) qu[i].cost=(long long)c[i]*(long long)am[x];
else qu[i].cost=(long long)c[i]*(long long)bm[x];
qu[i].idx=i;
}
sort(qu+,qu++m,cmp);
for (int i= ;i<=k ;i++)
ans+=qu[i].cost;
return ans;
}
int main() {
while(scanf("%d%d%d%d",&n,&m,&k,&s)!=EOF){
am[]=bm[]=;
for (int i= ;i<=n ;i++){
scanf("%d",&a[i]);
if (a[i]<am[i-]) {
am[i]=a[i];
ida[i]=i;
}else {
am[i]=am[i-];
ida[i]=ida[i-];
}
}
for (int i= ; i<=n ; i++) {
scanf("%d",&b[i]);
if (b[i]<bm[i-]) {
bm[i]=b[i];
idb[i]=i;
} else {
bm[i]=bm[i-];
idb[i]=idb[i-];
}
}
for (int i= ;i<=m ;i++)
scanf("%d%d",&t[i],&c[i]);
long long r=n,l=,mid,d=-;
while(l<=r) {
mid=(l+r)/;
if (check(mid)<=s) {
r=mid-;
d=mid;
for (int i= ;i<=k ;i++){
id[i]=qu[i].idx;
}
}else l=mid+;
}
printf("%lld\n",d);
if(d==-) continue;
int x=d;
for (int i= ;i<=k ;i++){
printf("%d %d\n",id[i],t[id[i]]==?ida[x]:idb[x]);
}
}
return ;
}

Gadgets for dollars and pounds CodeForces - 609D的更多相关文章

  1. codeforces 609D D. Gadgets for dollars and pounds(二分+贪心)

    题目链接: D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 mega ...

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

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

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

  5. CodeForces 609D Gadgets for dollars and pounds

    二分天数+验证 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm&g ...

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

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

  7. Codeforces 609D 被二分教做人

    传送门:http://codeforces.com/problemset/problem/609/D (如需转载,请注明出处,谢谢O(∩_∩)O) 题意: Nura想买k个小玩意,她手上有 s 个bu ...

  8. Educational Codeforces Round 3

    A. USB Flash Drives 水题,排序即可 ]; int main() { int n,m; scanf("%d%d",&n,&m); ;i<n; ...

  9. 【249】◀▶IEW-Unit14

    Unit 14 Money and Finance 线图写作技巧 1.Model1对应图片分析 The graph contains information about the price in US ...

随机推荐

  1. 好用的Google漏洞爬虫:Google Mass Explorer

    这是一款基于谷歌搜索引擎的自动化爬虫. 爬虫介绍 爬虫大体机制就是: 先进行一次谷歌搜索,将结果解析为特定格式,然后再提供给exp使用. 大家可以尝试使用–help来列出所有参数. 这个项目笔者会持续 ...

  2. 在Swift项目中使用OC,在OC项目中使用Swift

    几天前,我开始新的App的开发了.终于有机会把swift用在实战中了,也学到了之前纯学语法时没有机会获得的知识. 这篇博文中,我就如何使用swift.OC混编做一个介绍. OC中使用Swift 首先, ...

  3. Go学习笔记01-语言

    1.1 变量 Go 是静态类型语言,不能在运行期改变变量类型.使用关键字 var 定义变量,自动初始化为零值.如果提供初始化值,可省略变量类型,由编译器自动推断. var x int var f fl ...

  4. python进阶学习笔记(二)

    1.模块和包的概念 python的解决方案是把同名的模块放到不同的包中 1.1,导入模块 要使用一个模块,我们必须首先导入该模块.Python使用import语句导入一个模块.例如,导入系统自带的模块 ...

  5. 变态的IE

    1.IE7及更早版本, unshift()方法总是返回undefined而不是数组的新长度.2.IE8及之前版本, 在catch语句中捕获的错误对象会被添加到执行环境的变量对象, 而不是catch语句 ...

  6. Spring-Security 自定义Filter完成验证码校验

    Spring-Security的功能主要是由一堆Filter构成过滤器链来实现,每个Filter都会完成自己的一部分工作.我今天要做的是对UsernamePasswordAuthenticationF ...

  7. 初识vue——语法初解

    这次我们按照官网上的教程对vue的语法进行一个初步的了解: 一.声明式渲染 Vue.js的核心是一个允许采用简洁的模板语法来声明式的将数据渲染仅DOM的系统: 1.我们在HelloWorld里面输入下 ...

  8. MongoDB基础介绍安装与使用

    MongoDB已经日益成为流程和主流的数据库了,原因有两个:第一个就是技术优势,第二就是便利性,个人使用部署都很方便. MongoDB的优缺点,以及使用场景 优点: 面向文档存储(自由读高,不需要定义 ...

  9. uva1395 枚举不同区间的最小生成树

    枚举起点,求最小生成树.如果当前不能实现n个点连通,直接不再枚举. AC代码: #include<cstdio> #include<algorithm> using names ...

  10. 安装apache报没有找到VCRUNTIME40.dll错误

    解决办法 在Windows下运行最新版的Apache和php7都需要Visual C++Redistributable 2015,而之前的版本不需要那么高的,这个组件是运行Visual Studio ...