【Educational Codeforces Round 3 D】【二分答案 贪心排序】Gadgets for dollars and pounds m个物品n天价格买k个的最早天数

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


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=2e5+10,M=0,Z=1e9+7,ms63=0x3f3f3f3f;
int n,m,k,s,x;
int f[2][N],d[2][N];
pair<LL,int>p[2][N];
int h[2],t[2];LL v[2];
bool check(int top)
{
	LL tmp=0;
	h[0]=h[1]=0;
	for(int i=1;i<=k;++i)
	{
		for(int o=0;o<2;++o)v[o]=p[o][h[o]].first*f[o][top];
		if(v[0]<v[1]){tmp+=v[0];++h[0];}
		else {tmp+=v[1];++h[1];}
		if(tmp>s)return 0;
	}
	return 1;
}
void print(int top)
{
	printf("%d\n",top);
	h[0]=h[1]=0;
	for(int i=1;i<=k;++i)
	{
		for(int o=0;o<2;++o)v[o]=p[o][h[o]].first*f[o][top];
		if(v[0]<v[1]){printf("%d %d\n",p[0][h[0]].second,d[0][top]);++h[0];}
		else {printf("%d %d\n",p[1][h[1]].second,d[1][top]);++h[1];}
	}
}
void solve()
{
	int l=1;
	int r=n+1;
	while(l<r)
	{
		int mid=(l+r)>>1;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	if(l==n+1)puts("-1");
	else print(l);
}
int main()
{
	while(~scanf("%d%d%d%d",&n,&m,&k,&s))
	{
		for(int o=0;o<2;++o)
		{
			f[o][0]=2e9;
			for(int i=1;i<=n;++i)
			{
				scanf("%d",&x);
				f[o][i]=f[o][i-1];
				d[o][i]=d[o][i-1];
				if(x<f[o][i])
				{
					f[o][i]=x;
					d[o][i]=i;
				}
			}
		}
		t[0]=t[1]=0;
		for(int i=1;i<=m;++i)
		{
			int o,val;
			scanf("%d%d",&o,&val);--o;
			p[o][t[o]++]=MP(val,i);
		}
		p[0][t[0]].first=p[1][t[1]].first=2e9;
		sort(p[0],p[0]+t[0]);
		sort(p[1],p[1]+t[1]);
		solve();
	}
	return 0;
}
/*
【trick&&吐槽】
1,读题读不懂,也要继续沉下心来读题。
否则如果连题意都搞错了,最后是必然要玩完的。
2,保证代码的对称性和结构性是十分好的代码风格,然而千万不要把变量写错QwQ
3,注意排序的时候不要写=号,可能会造成死循环

【题意】
有n(2e5)天时间,初始有s(1e9)元人民币。
每天,我们可以把人民币,根据当天的汇率,兑换成美元或者英镑,

一共有m个物品,而我们需要购买其中的至少k个(1<=k<=m)。
对于每个物品,有两个属性——(对应的货币类型,对应的货币价格)。
货币类型的范围是1或2,表示美元或英镑。货币价格的范围是1e6。

想问你,最早的可以购买k个物品的时刻是?

【类型】
二分答案+贪心

【分析】
首先,我们发现,如果第i天可以购买k个物品,那么第i+1天也一定可以。
也就是说——答案是满足单调性的。于是我们二分答案。

二分答案之后,需要做的就是验证可行性。

首先,我们需要找到前i天的人民币兑换 英镑|美元 的最低价,并找到其对应是哪一天。
这些可以通过预处理得到。

然后,我们对于所有的物品,把其映射对应到其所需要花费的人民币,再把这个按照升序排序。
然后我们贪花费最小的k个,看看所花费的钱是否可以在s范围内即可。

这个时间复杂度其实是有点高的。
我们可以把物品分成两部分,按照它们所对应的货币种类,初始就排好序。
之后就可以类似于归并排序,在O(nlogn)的时间复杂度内AC啦。

【时间复杂度&&优化】
O(nlognlogn)->O(nlogn)


*/


你可能感兴趣的:(codeforces,贪心,二分,题库-CF,有趣排序)