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.
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.
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.
5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2
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;
}