CodeForces - 609D Gadgets for dollars and pounds —— 二分 预处理

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

Copy

5 4 2 2
1 2 3 2 1
3 2 1 2 3
1 1
2 1
1 2
2 2

output

Copy

3
1 1
2 3

input

Copy

4 3 2 200
69 70 71 72
104 105 106 107
1 1
2 2
1 2

output

Copy

-1

input

Copy

4 3 1 1000000000
900000 910000 940000 990000
990000 999000 999900 999990
1 87654
2 76543
1 65432

output

Copy

-1

题意:

一共有n天,m种物品,物品分为两种,一种只支持美元支付,另一种只支持英镑支付,给你s元人民币和每一天人民币兑换两种外币的汇率,问买k个物品需要的最小天数(k个物品是哪种的都行),每天剩余的美元和英镑不能留到第二天用。

思路:

我们买物品一定会在当天和之前汇率最高的时候买,这样最便宜,所以要预处理出来每天及其以前汇率的最大值,然后二分天数,看在这天的时候能否买到k个。

在进行判断的时候,直接把每样物品兑换成人民币价格,取前k个,看人民币是否足够

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
int n,m,k;
ll s;
int dol[200010],pou[200010];
int minnd[200010],minnp[200010];
int day1[200010],day2[200010];
vector<struct node>a;
struct node
{
	int ty;
	ll w;
	int id;
}zz;
struct node gad[200010];
bool cmp1(const node &a,const node &b)
{
	return a.w<b.w;
}
bool cmp2(const node &a,const node &b)
{
	return a.id>b.id;
}
bool check(int x)
{
	vector<struct node>v;
	for(int i=1;i<=m;i++)
	{
		zz=gad[i];
		if(zz.ty==1)
		zz.w*=minnd[x];
		else
		zz.w*=minnp[x];
		v.push_back(zz);
	}
	sort(v.begin(),v.end(),cmp1);
	ll sum=0;
	for(int i=0;i<k;i++)
	sum+=v[i].w;
	if(sum>s)
	return false;
	else
	{
		a.clear();
		for(int i=0;i<k;i++)
		a.push_back(v[i]);
		return true;
	}
}
int main()
{
	scanf("%d%d%d%lld",&n,&m,&k,&s);
	minnp[0]=minnd[0]=inf;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&dol[i]);
		if(dol[i]<minnd[i-1])
		{
			minnd[i]=dol[i];
			day1[i]=i;
		}
		else
		{
			minnd[i]=minnd[i-1];
			day1[i]=day1[i-1];
		}
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&pou[i]);
		if(pou[i]<minnp[i-1])
		{
			minnp[i]=pou[i];
			day2[i]=i;
		}
		else
		{
			minnp[i]=minnp[i-1];
			day2[i]=day2[i-1];
		}
	}
	for(int i=1;i<=m;i++)
	{
		gad[i].id=i;
		scanf("%d%lld",&gad[i].ty,&gad[i].w);
	}
	int l=1,r=n;
	int ans=-1;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(check(mid))
		{
			ans=mid;
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	printf("%d\n",ans);
	if(ans==-1)
		return 0;
	sort(a.begin(),a.end(),cmp2);
	for(int i=a.size()-1;i>=0;i--)
	{
		if(a[i].ty==1)
		{
			printf("%d %d\n",a[i].id,day1[ans]);
		}
		else
		{
			printf("%d %d\n",a[i].id,day2[ans]);
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值