Codeforces Round #398 (Div. 2) D. Cartons of milk —— 二分
来源:互联网 发布:网络打字员兼职招聘网 编辑:程序博客网 时间:2024/06/07 07:21
题目链接:http://codeforces.com/contest/767/problem/D
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.
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.
In the first line there are three integers n, m, k (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.
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.
3 6 21 0 12 0 2 0 0 2
31 2 3
3 1 20 0 01
-1
2 1 20 10
11
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 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.
题解:
首先将冰箱的牛奶排升序, 商店的牛奶排降序(商店的要用结构存编号,因为答案要输出)。然后再二分购买牛奶的瓶数。测试牛奶是否喝的完的时候,一瓶一瓶地模拟就是了,喝完了k瓶,然后又到了新的一天。
代码如下:
#include<bits/stdc++.h>using namespace std;typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 100+10;int n,m,k,i,j;int a[2000005];struct node{ int val, id;}q[1000005];bool cmp(node x, node y){ return x.val>y.val;}int test(int j){ int tod = 0,i = 1,cnt = 0; while(j || i<=n) { if( j && (i==n+1 || q[j].val<=a[i]) ) { if(q[j].val>=tod) j--; else return 0; } else { if(a[i]>=tod) i++; else return 0; } cnt++; if(cnt==k){cnt = 0; tod++;} } return 1;}int binsearch(){ int h = 0, t = m; while(h<=t) { int mid = (h+t)/2; if(test(mid)) h = mid + 1; else t = mid - 1; } return t;//没有答案时,t就是-1, 好巧}int main(){ cin>>n>>m>>k; for(i = 1; i<=n; i++) scanf("%d",&a[i]); for(i = 1; i<=m; i++) scanf("%d",&q[i].val), q[i].id = i; sort(a+1,a+1+n); sort(q+1, q+1+m, cmp); int sum = binsearch(); cout<<sum<<endl; if(sum!=-1) { for(int i = 1; i<=sum; i++) printf("%d ",q[i].id); cout<<endl; }}
- Codeforces Round #398 (Div. 2) D. Cartons of milk —— 二分
- Codeforces Round #398 (Div. 2) D. Cartons of milk
- Codeforces Round #398(Div. 2)D. Cartons of milk
- Codeforces Round #398(Div. 2)D. Cartons of milk【二分+暴力处理】
- 【Codeforces Round #398 (Div. 2)】Codeforces 767D Cartons of milk
- Codeforces Round #398 (Div. 2) A.Snacktower B.The Queue C.Garland D.Cartons of milk E.Change-fr
- 二分——Codeforces Round #377 (Div. 2) D
- Codeforces Round #251 (Div. 2) D 二分
- Codeforces Round #352 (Div. 2) D 二分
- Codeforces Round #377 (Div. 2) D 二分
- Codeforces Round #283 (Div. 2) D Tennis Game ——枚举,二分
- Codeforces Round #291 (Div. 2)D.R2D2 and Droid Army——RMQ+二分
- Codeforces Round #277.5 (Div. 2) D——Unbearable Controversy of Being
- Codeforces Round #401 (Div. 2) D Cloud of Hashtags —— 串
- Codeforces Round #256 (Div. 2) D. Multiplication Table 二分
- Codeforces Round #274 (Div. 2) D. Long Jumps(二分)
- Codeforces Round #220 (Div. 2) D 树状数组 && 二分
- Codeforces Round #298 (Div. 2) D. Handshakes 二分
- pwn --rop
- Linux下vim的设置
- android-view(2)
- 236. Lowest Common Ancestor of a Binary Tree
- 博客开张
- Codeforces Round #398 (Div. 2) D. Cartons of milk —— 二分
- 数据库三范式的简单理解
- 环信收发文本消息
- OpenGL实验(二三代码整理)桌子
- 安卓AndroidScrollView嵌套ListView的问题及其优化
- 基于Java反射实现简易ORM
- ffmpeg:一个简单的格式转换器
- Android 代码实现TextView 数组的应用
- Oracle10g安装过程中的Configuration Assistant配置失败的问题。