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

Examples
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

题意:你有n个天数的汇率,现在你有s元人民币,第i天可以随意换a[i]个人民币换1美元,b[i]个人民币换1英镑,但是不可以换了存起来,必须今天用掉;

   下面有m个东西,q表示只能用美元或者英镑买,需要的花费,你需要求最小的天数使得你买任意k个;

思路:前缀最小值,存两个汇率最小的时候,分别在这两天买;

   将美元可以买跟英镑可以买的分开。。算花费最少的人民币可以买到东西;

   二分天数即可;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=; ///数组大小
vector<pair<int,int> >v[];
pair<int,int> mina[N],minb[N];
int check(int x,int k,int s)
{
int a=mina[x].first;
int b=minb[x].first;
int sa=,sb=,si=;
while()
{
if(sa==v[].size()&&sb==v[].size())break;
if(sa==v[].size())
{
if(s<1LL*v[][sb].first*b)
break;
else
s-=v[][sb].first*b,sb++,si++;
}
else if(sb==v[].size())
{
if(s<1LL*v[][sa].first*a)
break;
else
s-=v[][sa].first*a,sa++,si++;
}
else
{
if(1LL*v[][sa].first*a<=1LL*v[][sb].first*b)
{
if(s<1LL*v[][sa].first*a)
break;
else
s-=v[][sa].first*a,sa++,si++;
}
else
{
if(s<1LL*v[][sb].first*b)
break;
else
s-=v[][sb].first*b,sb++,si++;
}
}
}
if(si>=k)return ;
return ;
}
void output(int x,int k,int s)
{
int a=mina[x].first;
int b=minb[x].first;
int sa=,sb=,si=;
while(si<k)
{
if(sa==v[].size())
{
if(s<1LL*v[][sb].first*b)
break;
else
s-=v[][sb].first*b,printf("%d %d\n",v[][sb].second,minb[x].second),sb++,si++;
}
else if(sb==v[].size())
{
if(s<1LL*v[][sa].first*a)
break;
else
s-=v[][sa].first*a,printf("%d %d\n",v[][sa].second,mina[x].second),sa++,si++;
}
else
{
if(1LL*v[][sa].first*a<=1LL*v[][sb].first*b)
{
if(s<1LL*v[][sa].first*a)
break;
else
s-=v[][sa].first*a,printf("%d %d\n",v[][sa].second,mina[x].second),sa++,si++;
}
else
{
if(s<1LL*v[][sb].first*b)
break;
else
s-=v[][sb].first*b,printf("%d %d\n",v[][sb].second,minb[x].second),sb++,si++;
}
}
}
}
int main()
{
int n,k,m,s;
scanf("%d%d%d%d",&n,&m,&k,&s);
mina[]=make_pair(inf,);
minb[]=make_pair(inf,);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
if(x<mina[i-].first)
mina[i]=make_pair(x,i);
else mina[i]=mina[i-]; }
for(int i=;i<=n;i++)
{
int y;
scanf("%d",&y);
if(y<minb[i-].first)
minb[i]=make_pair(y,i);
else minb[i]=minb[i-];
}
for(int i=;i<=m;i++)
{
int t,x;
scanf("%d%d",&t,&x);
v[t].push_back(make_pair(x,i));
}
for(int i=;i<=;i++)
sort(v[i].begin(),v[i].end());
int st=,en=n,ans=-;
while(st<=en)
{
int mid=(st+en)>>;
if(check(mid,k,s))
{
ans=mid;
en=mid-;
}
else
st=mid+;
}
printf("%d\n",ans);
if(ans!=-)
{
output(ans,k,s);
}
return ;
}

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

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

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

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

  5. Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分

    D. Professor GukiZ and Two Arrays 题目连接: http://www.codeforces.com/contest/620/problem/D Description ...

  6. Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)

    题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n  n个数,然后求有多少个区间[l,r] 满足    a[l]+a[r]=max([l, ...

  7. Educational Codeforces Round 61 (Rated for Div. 2)D(二分,模拟,思维)

    #include<bits/stdc++.h>using namespace std;typedef long long ll;int n,k;ll a[200007],b[200007] ...

  8. Educational Codeforces Round 67 (Rated for Div. 2) B题【前缀+二分】【补题ING系列】

    题意:给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t 思路:用二维数组记录前缀,然后二分即可. #include<bits/stdc++.h> using ...

  9. Educational Codeforces Round 80 (Rated for Div. 2)D(二分答案,状压检验)

    这题1<<M为255,可以logN二分答案后,N*M扫一遍表把N行数据转化为一个小于等于255的数字,再255^2检验答案(比扫一遍表复杂度低),复杂度约为N*M*logN #define ...

随机推荐

  1. 安装ES6及HEAD插件

    1.下载相应npm包 es6地址:https://www.elastic.co/downloads/elasticsearch head插件地址:https://github.com/mobz/ela ...

  2. Python爬虫_Selenium与PhantomJS

    Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动化操作,不同是Selenium可以直接运 ...

  3. 前端框架VUE----vue的使用

    一.安装 对于新手来说,强烈建议大家使用<script>引入 二. 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. 三.使用V ...

  4. apiCloud中的API对象

    1.属性 appId apiready = function () { var appId = api.appId; //比如: A6980386445546 var appName = api.ap ...

  5. Python进阶【第五篇】函数式编程及某些特殊函数

    一.函数式编程——Functional Programming 函数式=编程语言定义的函数+数学意义的函数 在计算机的层次上,CPU执行的是加减乘除的指令代码,以及各种条件判断和跳转指令,所以,汇编语 ...

  6. nodejs安装淘宝npm镜像【cnpm】

    安装完nodejs后[自带npm] 如果npm无法使用或需要FQ,可以先安装cnpm,然后使用cnpm install安装模块 安装全局cnpm npm install -g cnpm --regis ...

  7. JVM优化-JVM参数配置

    配置方式: java [options] MainClass [arguments] options - JVM启动参数. 配置多个参数的时候,参数之间使用空格分隔. 参数命名: 常见为 -参数名 参 ...

  8. 01: docker 基本使用

    1.1 docker基础 1.docker与虚拟机比较 2.docker版本 1. 社区版(Community Edition, CE) 2. 企业版(Enterprise Edition, EE) ...

  9. 用C++调用tensorflow在python下训练好的模型(centos7)

    本文主要参考博客https://blog.csdn.net/luoyexuge/article/details/80399265 [1] bazel安装参考:https://blog.csdn.net ...

  10. Codeforces 822D My pretty girl Noora - 线性筛 - 动态规划

    In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlop ...