CodeForces - 609D (二分!二分!)

╰半夏微凉° 2022-05-29 10:59 316阅读 0赞

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 a**i (1 ≤ a**i ≤ 106) — the cost of one dollar in burles on i-th day.

Third line contains n integers b**i (1 ≤ b**i ≤ 106) — the cost of one pound in burles on i-th day.

Each of the next m lines contains two integers t**i, c**i (1 ≤ t**i ≤ 2, 1 ≤ c**i ≤ 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 q**i, d**i — the number of gadget and the day gadget should be bought. All values q**i should be different, but the values d**i 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

  1. 5 4 2 2
  2. 1 2 3 2 1
  3. 3 2 1 2 3
  4. 1 1
  5. 2 1
  6. 1 2
  7. 2 2

output

  1. 3
  2. 1 1
  3. 2 3

input

Copy

  1. 4 3 2 200
  2. 69 70 71 72
  3. 104 105 106 107
  4. 1 1
  5. 2 2
  6. 1 2

output

  1. -1

input

Copy

  1. 4 3 1 1000000000
  2. 900000 910000 940000 990000
  3. 990000 999000 999900 999990
  4. 1 87654
  5. 2 76543
  6. 1 65432

output

  1. -1

思路:为什么会是二分呢? 这个题首先要能想到要想判断在某一天能将k件商品买齐,那么就选从第一天到这一天美元和英镑汇率最小的两天,然后将所有商品以这两个汇率转化成burle, 求出和之后判断是否可以。但是这个题这样做n*k复杂度肯定超时。再考虑一下,如果某一天能够将K个商品买齐,那么这一天之后的那几天也一定能买齐。所以这个分界线可以用二分求出。

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<cmath>
  5. using namespace std;
  6. const int maxn=2e5+10;
  7. const int INF=0x3f3f3f3f;
  8. typedef long long ll;
  9. int n, m, k;
  10. ll s;
  11. ll cost[maxn][2], ans[maxn][2];
  12. struct node{
  13. ll co;
  14. int id;
  15. bool operator <(const node &tmp) const{
  16. return co<tmp.co;
  17. }
  18. }c[maxn];//用来存转化之后的价格
  19. struct node2{
  20. ll r;
  21. int id;
  22. node2(ll _r=INF, int _id=0): r(_r), id(_id){}
  23. }rateA[maxn], rateB[maxn];//汇率
  24. bool check(int t){
  25. ll sum=0;
  26. for(int i=1; i<=m; i++)//转化
  27. {
  28. if(cost[i][0]==1)
  29. {
  30. c[i].co=cost[i][1]*rateA[t].r;
  31. c[i].id=i;
  32. }
  33. else{
  34. c[i].co=cost[i][1]*rateB[t].r;
  35. c[i].id=i;
  36. }
  37. }
  38. sort(c+1, c+1+m);
  39. for(int i=1; i<=k; i++)
  40. {
  41. sum+=c[i].co;
  42. ans[i][0]=c[i].id;
  43. ans[i][1]=cost[c[i].id][0]==1 ? rateA[t].id : rateB[t].id;
  44. }
  45. if(sum>s)
  46. return false;
  47. else
  48. return true;
  49. }
  50. int main(){
  51. scanf("%d%d%d%I64d", &n, &m, &k, &s);
  52. for(int i=1; i<=n; i++)
  53. {
  54. scanf("%I64d", &rateA[i].r);
  55. rateA[i].id=i;
  56. if(rateA[i].r>=rateA[i-1].r)
  57. rateA[i]=rateA[i-1];
  58. }
  59. for(int i=1; i<=n; i++)
  60. {
  61. scanf("%I64d", &rateB[i].r);
  62. rateB[i].id=i;
  63. if(rateB[i].r>=rateB[i-1].r)
  64. rateB[i]=rateB[i-1];
  65. }
  66. for(int i=1; i<=m; i++)
  67. {
  68. scanf("%I64d%I64d", &cost[i][0], &cost[i][1]);
  69. }
  70. int l=1, r=n;
  71. while(l<=r){
  72. int mid=(l+r)>>1;
  73. if(check(mid))
  74. r=mid-1;
  75. else
  76. l=mid+1;
  77. }
  78. if(l==n+1)
  79. printf("-1\n");
  80. else
  81. {
  82. check(l);
  83. printf("%d\n", max(rateA[l].id, rateB[l].id));
  84. for(int i=1; i<=k; i++)
  85. printf("%I64d %I64d\n", ans[i][0], ans[i][1]);
  86. }
  87. return 0;
  88. }

发表评论

表情:
评论列表 (有 0 条评论,316人围观)

还没有评论,来说两句吧...

相关阅读

    相关 codeforces 567D

    满足二分的条件,如果当前的断点不符合条件,那么后面的一定不符合条件,如果当前的符合条件,那么后面的可能还有符合条件的。 二分的过程中对断点进行排序,判断每个区间能放多少

    相关 codeforces 159 D(几何二分)

    [传送门][Link 1] 题意:给你n个点,问与x轴相切,并且包含这n个点的圆的最小半径是多少。 思路:真是做的的怀疑人生。思路是首先判断点是否在一边。 如果在一边一定