Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds(二分 + 贪心)

本文介绍了一种算法,用于解决在给定的美元和英镑汇率变化、玩具价格及预算条件下,如何快速确定能购买指定数量玩具的最早日期的问题。通过维护前缀最小汇率和使用二分搜索,结合两种玩具的数量管理,最终实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

N2×105dollarpound,M105,12
kM,s109,,

分析:

,
,,,min,,
,,,,
,,O(klogn)

代码:

//
//  Created by TaoSama on 2015-12-20
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 2e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

typedef long long LL;
typedef pair<LL, int> P;

int n, m, k, s;
P a[N], b[N];
P one[N], two[N];
int cnt1, cnt2, ans, ans1, ans2;

bool check(int x) {
    LL dollar = a[x].first, pound = b[x].first;
    for(int i = 0; i <= k && i <= cnt1; ++i) {
        if(k - i > cnt2) continue;
        LL sum = one[i].first * dollar + two[k - i].first * pound;
        if(sum <= s) {
            ans = i;
            ans1 = a[x].second;
            ans2 = b[x].second;
            return true;
        }
    }
    return false;
}

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    while(scanf("%d%d%d%d", &n, &m, &k, &s) == 4) {
        a[0].first = b[0].first = INF;
        for(int i = 1; i <= n; ++i) {
            scanf("%I64d", &a[i].first);
            a[i].second = a[i - 1].second;
            if(a[i].first < a[i - 1].first)
                a[i].second = i;
            else a[i].first = a[i - 1].first;
        }
        for(int i = 1; i <= n; ++i) {
            scanf("%I64d", &b[i].first);
            b[i].second = b[i - 1].second;
            if(b[i].first < b[i - 1].first)
                b[i].second = i;
            else b[i].first = b[i - 1].first;
        }

        cnt1 = cnt2 = 0;
        for(int i = 1; i <= m; ++i) {
            int x, y; scanf("%d%d", &x, &y);
            if(x == 1) one[++cnt1] = P(y, i);
            else two[++cnt2] = P(y, i);
        }
        sort(one + 1, one + 1 + cnt1);
        sort(two + 1, two + 1 + cnt2);
        for(int i = 1; i <= cnt1; ++i)
            one[i].first += one[i - 1].first;
        for(int i = 1; i <= cnt2; ++i)
            two[i].first += two[i - 1].first;

        int l = 1, r = n;
        while(l <= r) {
            int m = l + r >> 1;
            if(check(m)) r = m - 1;
            else l = m + 1;
        }

        if(l == n + 1) puts("-1");
        else {
            printf("%d\n", l);
            for(int i = 1; i <= ans; ++i)
                printf("%d %d\n", one[i].second, ans1);
            for(int i = 1; i <= k - ans; ++i)
                printf("%d %d\n", two[i].second, ans2);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值