题目链接
思路:二分答案,如果x能满足的话,0 1 2.。。x-1也肯定都能满足,我们优先喝即将过期的牛奶,看看能不能喝完。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e6+1;
ll a[maxn],k;
pair<ll,int>b[maxn];
bool cmp(int a,int b)
{
return a>b;
}
bool cmp2(const pair<int,int>&a,const pair<int,int>&b)
{
return (a.first==b.first)?a.second<b.second:a.first>b.first;
}
bool check(int x,int n)
{
ll now,size=0;
int i=1,l=1,r=1;
while(i<=x+n)
{
i++;
size++;
if(l>n) now=b[x-r+1].first,r++;
else if(r>x) now=a[n-l+1],l++;
else {
if(a[n-l+1]<b[x-r+1].first) now=a[n-l+1],l++;
else now=b[x-r+1].first,r++;
}
if(size>(now+1)*k) return false;
}
return true;
}
int main()
{
int n,m;
scanf("%d%d%lld",&n,&m,&k);
for(int i=1;i<=n;++i)
scanf("%lld",&a[i]);
sort(a+1,a+1+n,cmp);
for(int i=1;i<=m;++i) scanf("%lld",&b[i].first),b[i].second=i;
sort(b+1,b+1+m,cmp2);
if(!check(0,n)){
puts("-1");return 0;
}
int l=1,r=m;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid,n)) l=mid+1;
else r=mid-1;
}
cout<<r<<endl;
for(int i=1;i<=r;++i) printf("%d ",b[i].second);
}