CodeForces - 609D Gadgets for dollars and pounds(二分)

9 篇文章 0 订阅

Gadgets for dollars and pounds

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.

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
3
1 1
2 3


题意:这哥们只有卢布,但是商品只按照美刀或者英镑卖,给出两行n个值,分别代表第i天多少卢布可以兑换一美刀(英镑),

下面m个商品,1代表用美刀,2代表用英镑,后一个数是花费.

思路:谨以此题纪念自己改了一上午的各种bug.假如我们想在前i天买全所有东西,那么我们肯定会在美刀和英镑汇率最低的那两天买,而且买齐所有的东西,二分天数,然后算出每个商品需要多少卢布,排个序,从小到大买k个即可.

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 2e+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

struct node
{
	int type;
	ll p;
	ll cost;
	int pos;
}c[maxn];

struct minh
{
	int day;
	ll h;
}a[maxn],b[maxn];

int n,m,k;
ll s;
vector<int> ans_[3];

bool cmp(node x,node y)
{
	return x.cost< y.cost;
}

int main()
{
	cin>>n>>m>>k>>s;
	
	mem(c,0);
	
	a[0].h = b[0].h = ff;
	ll tmp;
	for(int i = 1;i<= n;i++)
	{
		scanf("%lld",&tmp);
		if(tmp< a[i-1].h)//预处理汇率最低的那天 
		{
			a[i].h = tmp;
			a[i].day = i;
		}
		else
			a[i] = a[i-1];
	}
	for(int i = 1;i<= n;i++)
	{
		scanf("%lld",&tmp);
		if(tmp< b[i-1].h)
		{
			b[i].h = tmp;
			b[i].day = i;
		}
		else
			b[i] = b[i-1];
	}
	
	for(int i = 1;i<= m;i++)
		scanf("%d %lld",&c[i].type,&c[i].p),c[i].pos = i;
	
	int l = 1,r = n;
	int ans = ff;
	int ansd1,ansd2;
	
	while(l<= r)
	{
		ll sum = 0,num = 0;
		int mid = (l+r)>>1;
		ll tmp1 = a[mid].h,tmp2 = b[mid].h;
		
		for(int i = 1;i<= m;i++)//处理m个商品的值多少卢布 
		{
			if(c[i].type == 1)
				c[i].cost = c[i].p*tmp1;
			else
				c[i].cost = c[i].p*tmp2;
		}
		
		sort(c+1,c+m+1,cmp);
		
		for(int i = 1;i<= k;i++)//买前k个 
		{
			sum+= c[i].cost;
			if(sum<= s)
				num++;
			else
				break;
		}
		
		if(num>= k)
		{
			if(mid< ans)//更新答案 
			{
				ans = mid;
				ansd1 = a[mid].day;
				ansd2 = b[mid].day;
				ans_[1].clear();
				ans_[2].clear();
				for(int i = 1;i<= k;i++)
					ans_[c[i].type].push_back(c[i].pos);
			}	
		}
		if(num< k)
			l = mid+1;
		else
			r = mid-1;
	}
	
	if(ans< ff)//打印答案 
	{
		cout<<ans<<endl;
		int cnt = 0;
		int k = ans_[1].size();
		for(int i = 0;i< k;i++)
			printf("%d %d\n",ans_[1][i],ansd1);
		k = ans_[2].size();
		for(int i = 0;i< k;i++)
			printf("%d %d\n",ans_[2][i],ansd2);
	}
	else
		cout<<-1<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值