【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 ...
随机推荐
- kentico9开始移除的webpart
https://devnet.kentico.com/articles/fighting-featuritis https://blog.intercom.com/product-strategy-m ...
- GoLang笔记-数组和切片,本质是就是长度不可变的可变的区别
数组 Arrays 数组是内置(build-in)类型,是一组同类型数据的集合,它是值类型,通过从0开始的下标索引访问元素值.在初始化后长度是固定的,无法修改其长度.当作为方法的入参传入时将复制一份数 ...
- bzoj1951 [Sdoi2010]古代猪文 ——数论综合
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1951 题意就是要求 G^( ∑(k|n) C(n,k) ) % p,用费马小定理处理指数,卢 ...
- 数据库异常 :java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
最近在新项目中突然出现了 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) ...
- Akka源码分析-Event Bus
akka中的EventBus其实是不常用,也最容易被忽略的一个组件. 但如果你深入Cluster的实现就会发现,这个东西其实还挺有用的,而且它是ActorSystem系统中所有事件消息的一个横切面,通 ...
- akka设计模式系列-While模式
While模式严格来说是while循环在Akka中的合理实现.while是开发过程中经常用到的语句之一,也是绝大部分编程语言都支持的语法.但while语句是一个循环,如果循环条件没有达到会一直执行wh ...
- day03_12/13/2016_bean的管理之作用域与初始化时间
在Spring中,Bean有几种作用域: 1.singleton作用域 当一个bean的作用域设置为singleton,那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean ...
- Linux命令(005) -- kill、pkill和killall的比较
kill命令用来“杀掉”指定进程PID的进程.终止一个前台进程可以使用Ctrl+C,终止一个后台进程就须用kill命令.kill命令是通过向进程发送指定的信号来结束相应进程的.在默认情况下,kill命 ...
- ArcGIS Android工程迁移到其他电脑不能打开的问题
问题描述:当我把已经做好的ArcGIS Android工程想在其他电脑运行时,总是会提示报错.而报错的地方,正是出现在下面这条语句上. compile 'com.esri.arcgisruntime: ...
- servlet-有参数的init和无参的init方法
package gz.itcast.d_init; import javax.servlet.ServletConfig; import javax.servlet.ServletException; ...