CodeForces 767D Cartons of milk (贪心+二分)

题目链接:http://codeforces.com/problemset/problem/767/D

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll unsigned long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
const int  maxn =1e6+5;
const int mod=9999991;
///const int ub=1e6;
///const double e=2.71828;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
题目大意:
本来有n瓶牛奶,现在有m种牛奶可以买,
每种牛奶都有保质期,给定k代表每天喝掉k瓶,
问要想不喝带过期牛奶至多可以买多少瓶牛奶。
输出其序号。

一开始就是想的是二分直接搞,
时间勉强说服了自己时间复杂度。
二分中只要暴力判一下就行,
注意要以k为间隔去扫描,
不难发现如果保质期有序,在k间隔中只要判断第一瓶牛奶的保质期和
当前天数的关系就行了。

二分上界细节注意。

时间复杂度是O(nlogm*log(n+m))........
*/

int n,m,k;
struct node
{
    int u,v;
    bool operator<(const node& y) const
    {
        return u>y.u;
    }
}q[maxn];

int f[maxn];
int tmp[maxn*2];
bool check(int ub)
{
    for(int i=1;i<=n;i++) tmp[i]=f[i];
    for(int i=1;i<=ub;i++) tmp[n+i]=q[i].u;
    sort(tmp+1,tmp+n+ub+1);
    int init=1,cnt=0;
    for(;init<=n+ub;init+=k,cnt++)
    {
        if(tmp[init]<cnt) return false;
    }
    return true;
}

int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++) scanf("%d",&f[i]);
    sort(f,f+n);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&q[i].u);
        q[i].v=i;
    }
    sort(q+1,q+1+m);
    if(check(0)==false) {puts("-1");return 0;}
    int l=1,r=m,ans=0;///取上确界,注意不能是m+1。
    while(l<=r){
        int mid=l+r>>1;
        if(check(mid)) {ans=mid;l=mid+1;}
        else r=mid-1;
    }
    printf("%d\n",ans);for(int i=1;i<=ans;i++) printf("%d%c",q[i].v,(i==ans)?'\n':' ');
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/84197894