D. Gadgets for dollars and pounds

题目连接:

http://www.codeforces.com/contest/609/problem/C

Description

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 Input

5 4 2 2

1 2 3 2 1

3 2 1 2 3

1 1

2 1

1 2

2 2

Sample Output

3

1 1

2 3

Hint

题意

有n天,一共有m个物品,你需要买k个,你身上有s元人民币。每一天,人民币兑换美元的价格为a[i],兑换英镑的价格为b[i]。

购买第i种物品,必须要用c[i]块t[i]货币来购买。然后问你最少多少天之内,可以买够t个物品,并且输出哪一天买哪一个物品。

题解:

二分答案。二分天数,很显然我们在这一个范围内,我们在这个范围内兑换美元和英镑最便宜的时候购买,物品则是兑换钱的时候买,然后check花费是否大于s就好了。

代码

#include<bits/stdc++.h>
using namespace std;
inline long long read()
{
long long x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
#define maxn 200005
int n,m,k;
int s;
int Mia[maxn],Mib[maxn];
int a[maxn],b[maxn];
long long suma[maxn],sumb[maxn];
int pm1[maxn],pm2[maxn];
vector<pair<int,int> > c1,c2;
int check(int mid)
{
long long mi = 1e18;
int top = min(k,(int)c1.size()-1);
for(int i=0;i<=top;i++)
{
int i1 = i;
int i2 = k-i;
if(i2>=c2.size())continue;
mi = min(mi,1LL*suma[i1]*Mia[mid]+1LL*sumb[i2]*Mib[mid]);
} if(mi>s)return 0;
return 1;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&s);
Mia[0]=1e18;Mib[0]=1e18;
for(int i=1;i<=n;i++)
{
a[i]=read();
Mia[i]=min(a[i],Mia[i-1]);
if(a[i]>=Mia[i-1])pm1[i]=pm1[i-1];
else pm1[i]=i;
}
for(int i=1;i<=n;i++)
{
b[i]=read();
Mib[i]=min(b[i],Mib[i-1]);
if(b[i]>=Mib[i-1])pm2[i]=pm2[i-1];
else pm2[i]=i;
}
Mia[0]=0;Mib[0]=0;
for(int i=1;i<=m;i++)
{
int t,c;
t=read(),c=read();
if(t==1)c1.push_back(make_pair(c,i));
else c2.push_back(make_pair(c,i));
}
c1.push_back(make_pair(0,0));
c2.push_back(make_pair(0,0));
sort(c1.begin(),c1.end());
sort(c2.begin(),c2.end());
for(int i=1;i<c1.size();i++)
suma[i]=c1[i].first+suma[i-1];
for(int i=0;i<c2.size();i++)
sumb[i]=c2[i].first+sumb[i-1];
int l = 1,r = n+1;
while(l<=r)
{
int mid = (l+r)/2;
if(check(mid))r=mid-1;
else l=mid+1;
}
if(l>=n+1)return puts("-1");
printf("%d\n",l);
long long mi = 1e18;
int top = min(k,(int)c1.size()-1);
for(int i=0;i<=top;i++)
{
int i1 = i;
int i2 = k-i;
if(i2>=c2.size())continue;
mi = min(mi,1LL*suma[i1]*Mia[l]+1LL*sumb[i2]*Mib[l]);
}
for(int i=0;i<=top;i++)
{
int i1 = i;
int i2 = k-i;
if(i2>=c2.size())continue;
if(suma[i1]*Mia[l]+sumb[i2]*Mib[l]==mi)
{
for(int j=1;j<=i1;j++)
printf("%d %d\n",c1[j].second,pm1[l]);
for(int j=1;j<=i2;j++)
printf("%d %d\n",c2[j].second,pm2[l]);
return 0;
}
}
}

Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心的更多相关文章

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

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

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

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

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

  5. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  7. Gadgets for dollars and pounds CodeForces - 609D

    Nura wants to buy k gadgets. She has only sburles for that. She can buy each gadget for dollars or f ...

  8. codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers

    题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...

  9. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

随机推荐

  1. 基于OSGI.Net的图形界面系统

    在2013年的十月份有幸接触了osgi.net和iopenworks的创始人,了解和学习的插件式开发,开始了后台数据的处理生涯. 第一个有图形界面的系统——智能农业的环境监测系统,其实在这个系统中所有 ...

  2. MATLAB 通过二进制读写文件

    这几天在做信息隐藏方面的应用,在读写文本文件时耗费许久,故特别的上网学习一二,这里给出一常用读写,其他的都类似. 很多时候,我们都要将一个.txt以二进制方式读出来,操作后在恢复成.txt文本. ma ...

  3. Basic Sorting Algorithms

    *稳定指原本数列中相同的元素的相对前后位置在排序后不会被打乱 快速排序(n*lgn 不稳定):数组中随机选取一个数x(这里选择最后一个),将数组按比x大的和x小的分成两部分,再对剩余两部分重复这个算法 ...

  4. Red Hat Linux认证

    想系统的学习一下Linux,了解了一些关于Red Hat Linux认证的信息.整理如下. 当前比较常见的是RHCE认证,即Red Hat Certified Engineer.最高级别的是RHCA ...

  5. 一起刷LeetCode5-Longest Palindromic Substring

    发现自己原来掌握的一下算法,都忘掉了,啊啊啊 ----------------------------------------------------------------------------- ...

  6. Android中的颜色值RGB对照表表

    Android中颜色值是通过红(Red).绿(Green).蓝 (Blue)三原色,以及一个透明度(Alpha)值来表示的,颜色值总是以井号(#)开头,接下来就是Alpha-Red-Green-Blu ...

  7. Mapreduce执行过程分析(基于Hadoop2.4)——(三)

    4.4 Reduce类 4.4.1 Reduce介绍 整完了Map,接下来就是Reduce了.YarnChild.main()—>ReduceTask.run().ReduceTask.run方 ...

  8. NServiceBus教程-持久化

    NServiceBus的各种特性需要持久性.其中有超时.传奇和订阅存储. 四个持久化技术在NServiceBus在使用: RavenDB nHibernate 内存中 MSMQ 读到安装Raven D ...

  9. Python程序的混淆和加密

    混淆 为了增加代码阅读的难度, 源代码的混淆非常必要, 一个在线的Python代码混淆网站. 如果你觉得有用, 可以购买离线版本.同时需要注意的是, 这个混淆其实还是被很多人怀疑的, 因为即使混淆了, ...

  10. php--opp--2.什么是类,什么是对象,类和对象这间的关系

    类的概念:类是具有相同属性和服务的一组对象的集合.它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和服务两个主要部分.在面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属 ...