codeforces 609D Gadgets for dollars and pounds

本文探讨了一个关于在不同汇率下购买特定数量商品的算法问题。主人公Nura需在有限的资金内,选择最佳购买时机来获取所需数量的商品。文章通过二分查找等算法解决了这一问题,并给出了具体的实现代码。
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 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 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.

Sample test(s)
input
5 4 2 2
1 2 3 2 1
3 2 1 2 3
1 1
2 1
1 2
2 2
output
3
1 1
2 3
input
4 3 2 200
69 70 71 72
104 105 106 107
1 1
2 2
1 2
output
-1
input
4 3 1 1000000000
900000 910000 940000 990000
990000 999000 999900 999990
1 87654
2 76543
1 65432
output
-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;
}

 

转载于:https://www.cnblogs.com/jerryRey/p/5071508.html

下载方式:https://pan.quark.cn/s/a4b39357ea24 布线问题(分支限界算法)是计算机科学和电子工程领域中一个广为人知的议题,它主要探讨如何在印刷电路板上定位两个节点间最短的连接路径。 在这一议题中,电路板被构建为一个包含 n×m 个方格的矩阵,每个方格能够被界定为可通行或不可通行,其核心任务是定位从初始点到最终点的最短路径。 分支限界算法是处理布线问题的一种常用策略。 该算法与回溯法有相似之处,但存在差异,分支限界法仅需获取满足约束条件的一个最优路径,并按照广度优先或最小成本优先的原则来探索解空间树。 树 T 被构建为子集树或排列树,在探索过程中,每个节点仅被赋予一次成为扩展节点的机会,且会一次性生成其全部子节点。 针对布线问题的解决,队列式分支限界法可以被采用。 从起始位置 a 出发,将其设定为首个扩展节点,并将与该扩展节点相邻且可通行的方格加入至活跃节点队列中,将这些方格标记为 1,即从起始方格 a 到这些方格的距离为 1。 随后,从活跃节点队列中提取队首节点作为下一个扩展节点,并将与当前扩展节点相邻且未标记的方格标记为 2,随后将这些方格存入活跃节点队列。 这一过程将持续进行,直至算法探测到目标方格 b 或活跃节点队列为空。 在实现上述算法时,必须定义一个类 Position 来表征电路板上方格的位置,其成员 row 和 col 分别指示方格所在的行和列。 在方格位置上,布线能够沿右、下、左、上四个方向展开。 这四个方向的移动分别被记为 0、1、2、3。 下述表格中,offset[i].row 和 offset[i].col(i=0,1,2,3)分别提供了沿这四个方向前进 1 步相对于当前方格的相对位移。 在 Java 编程语言中,可以使用二维数组...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值