Codeforces 609D 贪心+二分

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.

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
分析:英语太烂,理解题意好难。。。看了这篇题解才明白 点击打开链接

题意:一个人手上有s卢布,他要在n天内买m样东西中的k样
样例第二行是多少卢布换一美刀,第三行是多少卢布换一英镑
然后接下来是m样东西,告诉你每样东西用什么货币结账(1是美刀,2是卢布),以及要多少那种外币
要求:最早完成买k样的天数,以及输出方案
输出第几天买完k样商品,然后k行
购买的商品的id,第几天购买的
输出任意答案就可以了
 一种商品只能购买一次,但是一天可以买多种商品 

思路:对于前i天购买东西,肯定是选择在1天内或者2天内买完,就是汇率最高的时候购买。

对于某一天,我们能用O(n)的复杂度来求出当前最低所需要的钱来买k件商品,这里面只要把商品按照购买方式分类,然后排序一遍,之后就很好维护了。

那么对于前i天,很明显这个有单调性,先求出前i天里面两种汇率最大的分别是哪两天,预处理一下,之后只要二分i,就能得到答案了


#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=200009;
int a[maxn],b[maxn],t[maxn],c[maxn];
vector<pair<long long, pair<int, int> > >items;
int n,m,s,k;
bool solve(int day)
{
    items.clear();
	int mina=INF,minb=INF;
	int posa,posb;
	for (int i=1; i<=day; i++) {
		if (a[i]<mina) {
			mina=a[i];
			posa=i;
		}
		if (b[i]<minb) {
			minb=b[i];
			posb=i;
		}
	}
	for (int i=1; i<=m; i++) {
		if (t[i] == 1) items.push_back(make_pair(1ll*mina*c[i], make_pair(i, posa)));
		else items.push_back(make_pair(1ll*minb*c[i], make_pair(i, posb)));
	}

	long long sum=0;
	sort(items.begin(), items.end());
	for (int i=0; i<k; i++)
		sum += items[i].first;
	return s >= sum;
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&k,&s);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=1;i<=m;i++)scanf("%d%d",&t[i],&c[i]);
    int l=0,r=n+1;
    while(l+1<r){
        int mid=(l+r)/2;
        if(solve(mid))r=mid;
        else l=mid;
    }
    if (l == n) {
        printf("-1\n");
        return 0;
	}
	printf("%d\n", r);
	solve(r);
	for (int i=0;i<k; i++) {
		printf("%d %d\n", items[i].second.first, items[i].second.second);
	}
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值