Educational Codeforces Round 3D:http://codeforces.com/contest/609/problem/D
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
4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2
-1
4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432
-1
大致思路:
贪心:在1~x天内找兑换美元所需最少布勒cd及天数dd,兑换英镑随需最少布勒cp及天数dp,每次挑花费最少的物品在dd天或dp天购买,若能购买k个,则max(dd,dp)即为最小天数;若不能购买,则1~x天内不能买够k个物品
二分:只用贪心确定最少的天数会超时,注意到1~x天内兑换美元或英镑所需的布勒数不会增加,所以可以处理出1~x天内兑换美元或英镑所需的布勒数并用二分搜索
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN=200005; int n,m,k,s,nd,np; int mnd[MAXN],mnp[MAXN];//mnd[i]表示到第1~i天中兑换美元所需最低布勒,mnp[i]表示1~i天中兑换英镑所需最低布勒 struct Node { int i,c; bool operator< (const Node& a) const { return c<a.c||(c==a.c&&i<a.i); } }d[MAXN],p[MAXN];//d[i]表示d[i].i只能用美元买,价格是d[i].c;p[i]表示p[i].i只能用英镑买,价格是d[i].c bool judge(long long cd,long long cp) { long long tmp=0; int i=0,id=0,ip=0; for(;i<k;++i) {//每次买物品时,总是挑花费最少的 if((id<nd&&ip==np)||(id<nd&&ip<np&&cd*d[id].c<cp*p[ip].c)) tmp+=cd*d[id++].c; else tmp+=cp*p[ip++].c; } return tmp<=s; } int main() { int i,t,c,l,r,mid,dd,dp,id,ip; long long cd,cp; scanf("%d%d%d%d",&n,&m,&k,&s); mnd[0]=mnp[0]=0x3f3f3f3f; nd=np=0; for(i=1;i<=n;++i) { scanf("%d",mnd+i); mnd[i]=min(mnd[i],mnd[i-1]);//更新最小所需布勒 } for(i=1;i<=n;++i) { scanf("%d",mnp+i); mnp[i]=min(mnp[i],mnp[i-1]);//更新最小所需布勒 } for(i=1;i<=m;++i) { scanf("%d%d",&t,&c); if(t==1) { d[nd].i=i; d[nd++].c=c; } else { p[np].i=i; p[np++].c=c; } } sort(d,d+nd); sort(p,p+np); if(!judge(mnd[n],mnp[n]))//判断能都买够k个物品 printf("-1\n"); else { l=1; r=n; while(l<r) {//二分查找最小的天数 mid=(l+r)>>1; if(judge(mnd[mid],mnp[mid])) r=mid; else l=mid+1; } printf("%d\n",l); dd=dp=1; while(mnd[dd]>mnd[l])//找到兑换美元所需最少布勒的天数 ++dd; while(mnp[dp]>mnp[l])//找到兑换英镑所需最少布勒的天数 ++dp; cd=mnd[dd];//兑换美元所需最少布勒 cp=mnp[dp];//兑换英镑所需最少布勒 for(i=id=ip=0;i<k;++i) { if((id<nd&&ip==np)||(id<nd&&ip<np&&cd*d[id].c<cp*p[ip].c)) printf("%d %d\n",d[id++].i,dd); else printf("%d %d\n",p[ip++].i,dp); } } return 0; }