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

转载于:https://www.cnblogs.com/qscqesze/p/5060202.html

Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心相关推荐

  1. codeforces Educational Codeforces Round 49 (Rated for Div. 2) C题

    刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大.p=a b.就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大.那么题意就很明显了. 但是 ...

  2. (最小生成树)Codeforces Educational Codeforces Round 9 Magic Matrix

    You're given a matrix A of size n × n. Let's call the matrix with nonnegative elements magic if it i ...

  3. Codeforces Educational Codeforces Round 56 (Rated for Div. 2) 1093F. Vasya and Array

    有一个长度为nnn的的数列,aia_iai​的值域只有kkk个元素. 一个数列有一些数字已经填上.现在要求数列连续的数字长度不能超过lll,问所有不同的数列的个数有多少个. 1.考虑所有的数字都没填上 ...

  4. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  5. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  6. Educational Codeforces Round 114 (Rated for Div. 2) D. The Strongest Build 暴力 + bfs

    传送门 文章目录 题意: 思路: 题意: 你有nnn个装备槽,每个槽里面有cic_ici​个力量加成,对于每个槽只能选一个力量加成,现在给你mmm个力量组合[b1,b2,...,bn][b_1,b_2 ...

  7. Educational Codeforces Round 17 E. Radio stations cdq分治 + 树状数组

    传送门 文章目录 题意 思路: 题意 有nnn个电台,对于每个电台iii有三个参数xi,ri,fix_i,r_i,f_ixi​,ri​,fi​,分别指他们的坐标.作用半径.频率.如果两个电台频率差值在 ...

  8. Educational Codeforces Round 16 C. Magic Odd Square 矩阵构造

    传送门 文章目录 题意: 思路: 题意: 给你一个奇数nnn,让你构造一个n∗nn*nn∗n的矩阵,矩阵的每个位置依次填上[1,n∗n]之内的数[1,n*n]之内的数[1,n∗n]之内的数,满足每行. ...

  9. Educational Codeforces Round 32 G. Xor-MST 01tire + 分治 + Boruvka

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn序列aaa,每两个点之间的边权为ai⊕aja_i\oplus a_jai​⊕aj​,问你最小生成树的权值是多少. n≤2e5,ai< ...

最新文章

  1. 小程序-云开发部署流程(步骤二)
  2. 【pmcaff】麦肯锡:预测2025年将出现的12大颠覆技术【图】
  3. Shanghai Barcamp
  4. 学习大数据需要的基础
  5. NYOJ --277 车牌号
  6. 拒绝卡顿——在WPF中使用多线程更新UI
  7. Centos之故障排除
  8. a:active在ios上无效解决方法
  9. python批量检测域名和url能否打开
  10. html图片显示详情,纯CSS鼠标经过图片视差弹出层显示详情链接按钮特效代码.html...
  11. win10电脑连接手机热点时所出现的问题。
  12. Seata 极简入门
  13. 教你如何在3分钟安装ps笔刷——黎乙丙
  14. 树莓派花生壳卸载教程
  15. c语言 对字符串按长度大小排序
  16. Metasploit Framework Handbook
  17. 软件体系结构张友生第三版期末复习
  18. Messager for sch_zhj@hotmail.com
  19. mysql修改库存量_mysql update 库存问题
  20. Android_标题栏左上角返回上一级

热门文章

  1. 最受推荐的 9本全栈开发书籍,助web前端开发学习
  2. java 全局返回码设计_服务返回码的设计
  3. mysql追溯历史性能问题_【踩坑記錄】記一次MySQL主從復制延遲的坑
  4. angular6 设置全局变量_Angularjs 设置全局变量的方法总结
  5. Redux 中 combineReducers实现原理
  6. c语言程序与实验系统,C/C ++程序设计学习与实验软件系统v2019 最新版下载_云间下载...
  7. java 注解学习_JAVA注解学习
  8. 单片机两个IO口控制三个LED灯
  9. JSON和JS数据类型转化
  10. 【github】git 使用命令大全