【codeforces 767D】Cartons of milk
【题目链接】:http://codeforces.com/problemset/problem/767/D
【题意】
每个牛奶都有最晚可以喝的时间;
每天喝K瓶牛奶;
你有n瓶牛奶->已知每个牛奶的期限;
然后商店里面有m瓶牛奶;->已知每个牛奶的期限;
然后问你最多能再从商店里买几瓶牛奶;
使得你买的这些牛奶都不会因为过期而被扔掉;
或者你原本的n瓶牛奶都不能做到则输出-1
【题解】
首先肯定喝的顺序是先喝马上就要过期的
->所以原有的n瓶升序排;
然后买的话,肯定是买晚过期的;
所以商店里的m瓶降序排;
然后就是二分答案;
二分x表示买几瓶;->买最晚的x瓶;
然后O(N+X)判断可不可以喝完.
判断的时候没必要把新加的X个再加进去;
直接用类似归并排序的方式看看哪个小就先喝哪个就好;
(两个数组嘛)
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 2e6 + 100;
struct abc
{
int day, id;
};
int n, m, k, num, f[N];
map <int, int> extra;
map <int, int> dic;
abc s[N];
bool cmp1(abc a, abc b)
{
return a.day > b.day;
}
bool check(int x)
{
int R = x,now = k,day = 0,i = 1;
while (i <= n)
{
if (R == 0 || f[i] < s[R].day)
{
if (day > f[i]) return false;
now--;
i++;
}
else
{
if (day > s[R].day) return false;
R--;
now--;
}
if (now == 0) now = k,day++;
}
rep2(i, R, 1)
{
if (day > s[i].day)
return false;
now--;
if (now == 0) day++, now = k;
}
return true;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n), rei(m), rei(k);
rep1(i, 1, n)
rei(f[i]);
rep1(i, 1, m)
rei(s[i].day), s[i].id = i;
sort(f + 1, f + 1 + n);
sort(s + 1, s + 1 + m, cmp1);
if (!check(0))
{
return puts("-1"), 0;
}
int l = 1, r = m, ans = 0;
while (l <= r)
{
int mid = (l + r) >> 1;
if (check(mid))
{
ans = mid, l = mid + 1;
}
else
{
r = mid - 1;
}
}
printf("%d\n", ans);
rep1(i, 1, ans)
printf("%d ", s[i].id);
puts("");
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 767D】Cartons of milk的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【15.07%】【codeforces 625A】Guest From the Past
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- 使用自定义的AlertDialog。
使用自定义的AlertDialog.1.核心的方法是setView(View view):把自己的view放置到dialog中填充.2.setCanceledOnTouchOutside(boolea ...
- B1072 [SCOI2007]排列perm 状压dp
很简单的状压dp,但是有一个事,就是...我数组开大了一点,然后每次memset就会T,然后开小就好了!!!震惊!以后小心点这个问题. 题干: Description 给一个数字串s和正整数d, 统计 ...
- 洛谷 P2634 聪聪可可 —— 树形DP / 点分治
题目:https://www.luogu.org/problemnew/show/P2634 今天刚学了点分治,做例题: 好不容易A了,结果发现自己写的是树形DP...(也不用找重心)(比点分治快) ...
- SQL service 自动解决依赖包 验证
依赖关系解决 ============================================================================================= ...
- bzoj1433[ZJOI2009]假期的宿舍(匈牙利)
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2544 Solved: 1074 [Submit][St ...
- codevs1486愚蠢的矿工(树形dp)
1486 愚蠢的矿工 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description Stupid 家族得知在HYC家的后花园里的中央花坛处,向北走3步,向西走3步, ...
- 上传Android代码到gerrit服务器
1. 配置default.xml default.xml是跟Android代码配套的,可参考google Android源码下的default.xml(.repo/manifests/default. ...
- [转]Linux 正则表达式详解
转自:http://www.jb51.net/article/42989.htm 一.linux文本查找命令 在说linux正规表达式之前,还介绍下linux中查找文本文件常用的三个命令: 1.gre ...
- 【转】Linux 之 数据流重定向
转自:http://www.linuxidc.com/Linux/2012-09/69764.htm linux在你登入时,便将默认的标准输入.标准输出.标准错误输出安排成你的终端.I/O重定向就是你 ...
- cocos2dx实现单机版三国杀(一)
首先需要一个UI交互类 GameUI -layer 一个游戏驱动类,负责游戏逻辑的循环 暂时定为GameScene- scene GameScene obj 调用update 更新游戏,addch ...