zoukankan      html  css  js  c++  java
  • [CF767D] Cartons of milk

    [CF767D] Cartons of milk - 二分

    Description

    Olya每天喝k瓶牛奶.她家里有n瓶牛奶,超市里有m瓶牛奶.每瓶牛奶有一个保质期,0表示当天,1表示第二天,以此类推.如果一瓶牛奶过了保质期,Olya要把它扔掉.为了不扔掉任何一瓶牛奶,求出她能够从商店里购买的最大牛奶的瓶数,并且按任意顺序输出购买的牛奶的编号(在输入顺序当中出现的位置).如果她连家里的牛奶都喝不完,输出-1.

    Solution

    二分出买多少瓶(显然会买保质期最长的),然后暴力检验即可

    注意边界情况

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    
    const int N = 1e6 + 5;
    
    int n, m, k, a[N], b[N], id[N];
    
    bool check(int pos)
    {
        int p = 1, q = pos, t = 0;
        while (p <= n || q <= m)
        {
            int cnt = 0;
            while ((p <= n || q <= m) && cnt < k)
            {
                if (q > m || (p <= n && a[p] < b[q]))
                {
                    if (a[p] < t)
                        return false;
                    p++;
                }
                else
                {
                    if (b[q] < t)
                        return false;
                    q++;
                }
                cnt++;
            }
            ++t;
        }
        return true;
    }
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        cin >> n >> m >> k;
    
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        for (int i = 1; i <= m; i++)
            cin >> b[i];
    
        sort(a + 1, a + n + 1);
    
        vector<pair<int, int>> tvec;
        for (int i = 1; i <= m; i++)
            tvec.push_back({b[i], i});
        sort(tvec.begin(), tvec.end());
        for (int i = 1; i <= m; i++)
            tie(b[i], id[i]) = tvec[i - 1];
    
        int l = 1, r = m;
        while (l < r)
        {
            int mid = (l + r) / 2;
            if (check(mid))
                r = mid;
            else
                l = mid + 1;
        }
        if (check(l) == 0)
            ++l;
        if (check(l) == 0)
            ++l;
        if (l > m + 1)
            cout << -1 << endl;
        else
        {
            cout << m - l + 1 << endl;
            for (int i = l; i <= m; i++)
                cout << id[i] << " ";
            cout << endl;
        }
    }
    
  • 相关阅读:
    过滤器
    HTTP会话的使用和管理
    Servlet技术(下)
    Servlet技术(上)
    Web运作原理
    Tomcat简介
    Java Web应用
    java io
    【node.js】node.js安装yarn报错:Could not create the Java Virtual Machine
    【微信小程序】微信小程序页面url传参,使用JSON.parse报错Unexpected end of JSON input
  • 原文地址:https://www.cnblogs.com/mollnn/p/14663883.html
Copyright © 2011-2022 走看看