• Codeforces Round #398 D题Cartons of milk(二分,贪心)解题报告


    D. Cartons of milk
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Olya likes milk very much. She drinks k cartons of milk each day if she has at least k and drinks all of them if she doesn't. But there's an issue — expiration dates. Each carton has a date after which you can't drink it (you still can drink it exactly at the date written on the carton). Due to this, if Olya's fridge contains a carton past its expiry date, she throws it away.

    Olya hates throwing out cartons, so when she drinks a carton, she chooses the one which expires the fastest. It's easy to understand that this strategy minimizes the amount of cartons thrown out and lets her avoid it if it's even possible.

    Milk. Best before: 20.02.2017.

    The main issue Olya has is the one of buying new cartons. Currently, there are n cartons of milk in Olya's fridge, for each one an expiration date is known (how soon does it expire, measured in days). In the shop that Olya visited there are m cartons, and the expiration date is known for each of those cartons as well.

    Find the maximum number of cartons Olya can buy so that she wouldn't have to throw away any cartons. Assume that Olya drank no cartons today.

    Input

    In the first line there are three integers nmk (1 ≤ n, m ≤ 106, 1 ≤ k ≤ n + m) — the amount of cartons in Olya's fridge, the amount of cartons in the shop and the number of cartons Olya drinks each day.

    In the second line there are n integers f1, f2, ..., fn (0 ≤ fi ≤ 107) — expiration dates of the cartons in Olya's fridge. The expiration date is expressed by the number of days the drinking of this carton can be delayed. For example, a 0 expiration date means it must be drunk today, 1 — no later than tomorrow, etc.

    In the third line there are m integers s1, s2, ..., sm (0 ≤ si ≤ 107) — expiration dates of the cartons in the shop in a similar format.

    Output

    If there's no way for Olya to drink the cartons she already has in her fridge, print -1.

    Otherwise, in the first line print the maximum number x of cartons which Olya can buy so that she wouldn't have to throw a carton away. The next line should contain exactly x integers — the numbers of the cartons that should be bought (cartons are numbered in an order in which they are written in the input, starting with 1). Numbers should not repeat, but can be in arbitrary order. If there are multiple correct answers, print any of them.

    Examples
    input
    3 6 2
    1 0 1
    2 0 2 0 0 2
    output
    3
    1 2 3
    input
    3 1 2
    0 0 0
    1
    output
    -1
    input
    2 1 2
    0 1
    0
    output
    1
    1
    Note

    In the first example k = 2 and Olya has three cartons with expiry dates 0, 1 and 1 (they expire today, tomorrow and tomorrow), and the shop has 3 cartons with expiry date 0 and 3 cartons with expiry date 2. Olya can buy three cartons, for example, one with the expiry date 0and two with expiry date 2.

    In the second example all three cartons Olya owns expire today and it means she would have to throw packets away regardless of whether she buys an extra one or not.

    In the third example Olya would drink k = 2 cartons today (one she alreay has in her fridge and one from the shop) and the remaining one tomorrow.

    做法与官方解答大致相同。如果选k个,肯定是选保质期最长的k瓶。所以将商店中的排序。对选的瓶数进行二分。(如果买x瓶可行,x-1,……1,0瓶肯定都可行)。检验时将原本就有的和买回来的一起看,将他们从过期日期的小到大排来看(这时要从第一天往后看才正确,因为晚过期的提前喝可以,而早就过期的后来喝就不行了),如果在某瓶过期的日子时,在这天之后就已经坏的瓶数之和大于这些天最多可以喝的瓶数就不行了。如果一直看到最后一瓶都可以,那么这个数量x就可以。继续二分下去。

    注意 每天喝的瓶数*天数 可能会超出int,所以这里要用ll

      1 #include <iostream>
      2 #include<bits/stdc++.h>
      3 #include <stack>
      4 #include <queue>
      5 #include <cstdio>
      6 #include <cstring>
      7 #include <algorithm>
      8 using namespace std;
      9 typedef long long ll;
     10 typedef unsigned long long ull;
     11 const int MAX=1e6+5;
     12 int n,m,k;
     13 int a[MAX];
     14 struct node
     15 {
     16     int num;
     17     int id;
     18 }b[MAX];
     19 bool cmp(node x,node y)
     20 {
     21     if(x.num!=y.num)
     22         return x.num>y.num;
     23     else
     24         return x.id<y.id;
     25 }
     26 bool cmp2(int x,int y)
     27 {
     28     return x>y;
     29 }
     30 bool check(int x)
     31 {
     32     int l,r;
     33     l=r=0;
     34     int i=0;
     35     ll ge=0;
     36     int nowday;
     37     while(i<x+n)
     38     {
     39         i++;
     40         ge++;
     41         if(r>=x)
     42         {
     43 
     44             nowday=a[n-1-l];
     45             l++;
     46         }
     47         else if(l>=n)
     48         {
     49             nowday=b[x-r-1].num;
     50             r++;
     51         }
     52         else
     53         {
     54             if(a[n-1-l]<b[x-r-1].num)
     55             {
     56                 nowday=a[n-1-l];
     57                 l++;
     58             }
     59             else
     60             {
     61                 nowday=b[x-r-1].num;
     62                 r++;
     63             }
     64         }
     65         if(ge>(ll)(nowday+1)*(ll)k)
     66             {
     67                 return false;
     68             }
     69     }
     70     return true;
     71 }
     72 int main()
     73 {
     74     scanf("%d %d %d",&n,&m,&k);
     75     int i;
     76     for(i=0;i<n;i++)
     77     {
     78         scanf("%d",&a[i]);
     79     }
     80     sort(a,a+n,cmp2);
     81 
     82     for(i=0;i<m;i++)
     83     {
     84         int tem;
     85         scanf("%d",&tem);
     86         b[i].num=tem;
     87         b[i].id=i+1;
     88     }
     89     if(!check(0))
     90     {
     91         printf("-1
    ");
     92         return 0;
     93     }
     94     sort(b,b+m,cmp);
     95     int left=0,right=m,mid;
     96     int an=-1;
     97     while(left<=right)
     98     {
     99         mid=(left+right)/2;
    100         if(check(mid))
    101         {
    102             an=mid;
    103             left=mid+1;
    104         }
    105         else
    106             right=mid-1;
    107     }
    108     printf("%d
    ",an);
    109     for(i=1;i<=an;i++)
    110     {
    111         printf("%d ",b[i-1].id);
    112     }
    113     printf("
    ");
    114 }
  • 相关阅读:
    关于讯飞语音SDK开发学习
    关于讯飞语音SDK开发学习
    关于讯飞语音SDK开发学习
    Atitti cto的日常流程与职责attilax总结
    Atitti cto的日常流程与职责attilax总结
    Atitti cto的日常流程与职责attilax总结
    MySQL 大表优化方案,收藏了细看!
    Jquery 返回json数据在IE浏览器中提示下载的问题
    多级菜单联动
    解决IE下返回json数据提示文件保存到问题
  • 原文地址:https://www.cnblogs.com/quintessence/p/6415553.html
Copyright © 2020-2023  润新知