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.ios
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.c++
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.app
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.ide
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.ui
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.url
If Nura can't buy k gadgets print the only line with the number -1.spa
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
二分,截至x天能夠買到k個,x+1天也能夠。
不能存dollar和pound,須要O(n)預處理,直到第i天爲止兩種貨幣匯率最佳的天數編號。
判斷的取前k小和s比較,O(nlogn)
總複雜度O(nlog^2n)
一個優化先快排之後,二分的時候用兩個指針歸併,O(k)
總複雜度O( (n+k) * log n )
(通過仔細比較,cin,cout << scanf,printf; 同步等等關掉,性能仍是差1倍以上。 endl比'\n'慢不少。
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+1; int n, m, k, s; int a[N], b[N]; int ar[N], br[N]; int t[N], c[N]; ll f[N], r[N]; bool P(int x) { for(int i = 0; i < m; i++){ f[i] = (ll)c[i]*(t[i] == 1?a[ar[x]]:b[br[x]]); } nth_element(f,f+k,f+m); return accumulate(f,f+k,0LL) <= s; } //#define LOCAL int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif ios::sync_with_stdio(false); cin.tie(nullptr); cin>>n>>m>>k>>s; int i; for(i = 0; i < n; i++) cin>>a[i]; for(i = 0; i < n; i++) cin>>b[i]; for(i = 1; i < n; i++) { ar[i] = min(i,ar[i-1],[](int i,int j){ return a[i] < a[j]; }); br[i] = min(i,br[i-1],[](int i,int j){ return b[i] < b[j]; }); } for(i = 0; i < m; i++) cin>>t[i]>>c[i]; int lb = 0, ub = n-1, md; if(!P(ub)){ puts("-1"); return 0; } while(lb < ub){ md = (lb+ub)>>1; P(md) ? ub = md : lb = md+1; } for(int i = 0; i < m; i++){ f[i] = (ll)c[i]*(t[i] == 1?a[ar[lb]]:b[br[lb]]); r[i] = i; } nth_element(r,r+k,r+m,[](int i,int j){ return f[i] < f[j]; }); cout<<lb+1<<'\n'; for(i = 0; i < k; i++){ cout<<r[i]+1<<' '<<(t[r[i]] == 1 ? ar[lb] : br[lb])+1<<'\n'; } return 0; }