Codeforces Round #398 (Div. 2) D. Cartons of milk

23 篇文章 0 订阅
19 篇文章 0 订阅

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 ≤ 1061 ≤ 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 01 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 0 and 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.


先贪心模拟家里的奶能不能喝完,如果能就二分最多能买多少盒,判断函数里贪心模拟,先喝保质期少的,看看会不会出现过期的现象


二分解法用cin会t。。。。。。。。。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<iostream>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define eps 1e-9
#define PI 3.141592653589793
#define bs 1000000007
#define bsize 256
#define MEM(a) memset(a,0,sizeof(a))
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
struct node{
	int index,da;//da是保质期,index是下标,用于输出答案
}s[1000005],f[1000005];
int n,m,k;
int cmp(node u,node v)
{
	return u.da>v.da;
}
int judge(int x)
{
	int i,j,today,t;
	i=n,j=x,t=0,today=0;
	while(i>0&&j>0)
	{
		if(t==k)
		{
			t=0;
			today++;
		}
		t++;
		if(f[i].da<s[j].da)   //先喝保质期小的 
		{
			if(f[i].da>=today)
			{
				i--;
			}
			else if(s[j].da>=today)
			{
				j--;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			if(s[j].da>=today)
			{
				j--;
			}
			else if(f[i].da>=today)
			{
				i--;
			}
			else
			{
				return 0;
			}
		}
	}
	while(i>0)   //家里的没喝完是情况
	{
		if(t==k)
		{
			t=0;
			today++;
		}
		t++;
		if(f[i].da<today)
		{
			return 0;
		}
		i--;
	}
	while(j>0)    //买的没喝完
	{
		if(t==k)
		{
			t=0;
			today++;
		}
		t++;
		if(s[j].da<today)
		{
			return 0;
		}
		j--;
	}
	return 1;
}
int main()
{
	int today,flog,t,i,j,le,ri,mid;
	scanf("%d %d %d",&n,&m,&k);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&f[i].da);
		f[i].index=i;
	}
	for(i=1;i<=m;i++)
	{
		scanf("%d",&s[i].da);
		s[i].index=i;
	}
	sort(s+1,s+m+1,cmp); 
	sort(f+1,f+n+1,cmp);
	today=0,flog=1,t=0;  //先贪心判断家里的能不能喝完
	for(i=n;i>0;i--)
	{
		t++;
		if(f[i].da<today)
		{
			flog=0;
			break;
		}
		if(t==k)
		{
			t=0;
			today++;
		}
	}
	if(!flog)
	{
		printf("-1\n");
	}
	else
	{
		le=0,ri=m;
		while(le<ri)
		{
			mid=(le+ri)>>1;
			if(judge(mid))
			{
				le=mid+1;
			}
			else
			{
				ri=mid-1;
			}
		}
		for(j=le+1;j>=le-1;j--)
		{
			if(j>m)
			continue;
			if(judge(j))
			{
				printf("%d\n",j);
				for(i=1;i<=j;i++)
				printf("%d ",s[i].index);
				break;
			}
		}
		
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值