题目链接:

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

题意:

给了n天时间,每天的美元和英镑兑换这种货币的汇率,然后给了m种商品和s个这种货币,问买k种商品最少需要多少天;而且商品只能按美元或者英镑来买;

思路:

二分最少的天数,然后贪心要买的k件商品,(提前分好类和计算好前x个需要的英镑或者美元数),暴力枚举1类型和2类型的各多少个,换算成这种货币后看是否能买下来;
还有就是找到最小天数后再找一遍1类型和2类型多少个,在哪天买的自然是在汇率最小的那一天买啊; AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+;
typedef long long ll;
int n,m,k,s,ma[N],mb[N],ans,a[N],b[N],type,co;
ll suma[N],sumb[N];
struct node
{
friend bool operator< (node x,node y)
{
return x.num<y.num;
}
int num,pos;
};
node fa[N],fb[N];
int cnta=,cntb=;
int check(int x)
{
ll sum=;
for(int i=;i<cnta&&i<=k;i++)
{
if(k < i+cntb)
{
sum = suma[i]*(ll)a[ma[x]]+sumb[k-i]*(ll)b[mb[x]];//按汇率最少的时候算需要多少这种货币;
if(sum <= s)return ;
}
}
return ;
}
int bi()
{
int l = ,r = n,mid;
while(l <= r)
{
mid = (l+r)>>;
if(check(mid))l = mid+;//return 1表示全都不符合天数需要往后才能找到更小的汇率;
else r = mid-;
}
return l;//l-1表示的是最后一个不满足的天数,那么l天就是第一个满足的天数,也是最小的;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&s);
a[] = 1e6+,b[] = 1e6+;
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
if(a[ma[i-]] > a[i])ma[i] = i;//ma[i]记录的是前i天美元汇率最小的那天;mb[i]也是;
else ma[i] = ma[i-];
}
for(int i = ;i <= n;i++)
{
scanf("%d",&b[i]);
if(b[mb[i-]] > b[i])mb[i] = i;
else mb[i] = mb[i-];
}
for(int i = ;i <= m;i++)
{
scanf("%d%d",&type,&co);
if(type==)//分类
{
fa[cnta].num = co;
fa[cnta].pos = i;
cnta++;
}
else
{
fb[cntb].num = co;
fb[cntb].pos = i;
cntb++;
}
}
sort(fa+,fa+cnta);//分类后排序,方便后面贪心;
sort(fb+,fb+cntb);
suma[] = ;
sumb[] = ;
for(int i = ;i < cnta;i++)
{
suma[i] = suma[i-]+(ll)fa[i].num;//suma[i]表示前i个需要美元买的商品一共需要多少美元;
}
for(int i = ;i < cntb;i++)
{
sumb[i] = sumb[i-]+(ll)fb[i].num;
}
int fs = bi();
if(fs > n)printf("-1\n");
else
{
printf("%d\n",fs);
ll sum=;
for(int i=;i<cnta&&i<=k;i++)
{
if(k < i+cntb)
{
sum = suma[i]*(ll)a[ma[fs]]+sumb[k-i]*(ll)b[mb[fs]];
if(sum <= s)
{
ans = i;
break;
}
}
}
for(int i=;i<=ans;i++)
{
printf("%d %d\n",fa[i].pos,ma[fs]);
}
for(int i=;i<=k-ans;i++)
{
printf("%d %d\n",fb[i].pos,mb[fs]);
}
}
return ;
}

codeforces 609D D. Gadgets for dollars and pounds(二分+贪心)的更多相关文章

  1. Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心

    D. Gadgets for dollars and pounds 题目连接: http://www.codeforces.com/contest/609/problem/C Description ...

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

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

  5. CodeForces 609D Gadgets for dollars and pounds

    二分天数+验证 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm&g ...

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

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

  7. Codeforces 609D 被二分教做人

    传送门:http://codeforces.com/problemset/problem/609/D (如需转载,请注明出处,谢谢O(∩_∩)O) 题意: Nura想买k个小玩意,她手上有 s 个bu ...

  8. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  9. Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心

    地址:http://codeforces.com/problemset/problem/712/C 题目: C. Memory and De-Evolution time limit per test ...

随机推荐

  1. 配置fio支持rbd測试引擎

    fio的rbd測试引擎能够非常方便的对rbd进行測试.以下示范怎样安装fio支持rbd引擎. git clone git://git.kernel.dk/fio.git $ cd fio $ ./co ...

  2. C#中判断某个值是否存在于枚举

    我有一个枚举类型: #region -酒的种类- public enum WineType { 白酒 = 3, 葡萄酒 = 4, 洋酒 = 5, 老年陈酒 = 16, 啤酒 = 17 } #endre ...

  3. API自动化测试利器——Postman

    自从开始做API开发之后,我就在寻找合适的API测试工具.一开始不是很想用Chrome扩展,用的WizTools的工具,后来试过一次Postman之后就停不下来了,还买了付费的Jetpacks.推出T ...

  4. EasyPlayer_Android RTSP安卓播放器直播画面卡在第一帧问题修复

    最近发现某些Android安卓手机在运行EasyPlayer播放视频时,会停留在第一帧画面,虽然有码率预示着接收端没有问题,但是画面却卡着不动. 一般来讲,这个现象有三种原因导致: 没有接收到视频帧; ...

  5. word操作

  6. Double Check Locking 双检查锁机制

    方法保证了多线程并发下的线程安全性.这里在声明变量时使用了volatile关键字来保证其线程间的可见性:在同步代码块中使用二次检查,以保证其不被重复实例化.集合其二者,这种实现方式既保证了其高效性,也 ...

  7. Netty聊天室-源码

    目录 Netty聊天室 源码工程 写在前面 [百万级流量 聊天室实战]: [分布式 聊天室] [Spring +Netty]: [Netty 原理] 死磕 系列 [提升篇]: [内力大增篇]: 疯狂创 ...

  8. 前端mvc组合框架

    1. jquery 2. underscore 3. backbone 4. reactjs 5. seajs

  9. 2017-2018-1 20179209《Linux内核原理与分析》第五周作业

    一.实验:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 环境说明 实验环境为 Ubuntu16.10 和 实验楼环境. 选择39号系统调用实验.39号系统调用为mkdir系统调用. ...

  10. 【题解】数字组合(NTT+组合 滑稽)

    [题解]数字组合(NTT+组合 滑稽) 今天实践一下谢总讲的宰牛刀233,滑稽. \((1+x)(1+x)(1+x)\)的\(x^2\)系数就代表了有三个一快钱硬币构成的两块钱的方案数量. 很好理解, ...