【题目链接】: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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【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 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. remove namespace from xml config file

    从xml配置文件中移除命令空间 https://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-wit ...

  2. 使iframe随内容(target到iframe的内容)改变而自适应高度,完美解决各种获取第一个demo高度后第二个高度不变情况

    转自:http://caiceclb.iteye.com/blog/281102 很高兴,终于使用jquery实现了点击外部链接,更改iframe内容时,iframe的高度自适应问题. 失败的测试就不 ...

  3. Dice (HDU 4652)

    题面: m 面骰子,求1. 出现n个连续相同的停止 ;2. 出现n个连续不同的停止的期望次数.(n, m ≤ 10^6 ) 解析: 当然要先列式子啦. 用f[i](g[i])表示出现i个连续相同(不相 ...

  4. Olddriver’s books

    Olddriver 的书多的吓人,什么算法导论,组合数学英 文版(orz)......他把 n 本书都放在身后的桌子上, 每本书有一定的面积,并且书可以覆盖,求 n 本书覆盖桌面 的面积 输入格式: ...

  5. MSXML2.XMLHTTP.4.0对象

    一.使用步骤:1.创建XMLHTTP对象 //需MSXML4.0支持2.打开与服务端的连接,同时定义指令发送方式,服务网页(URL)和请求权限等.客户端通过Open命令打开与服务端的服务网页的连接.与 ...

  6. P2251 质量检测(ST表)

    P2251 质量检测 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... ...

  7. [Swift通天遁地]三、手势与图表-(10)创建包含圆点、方形、三角形图标的散点图表

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. leetCode----day02---- 买卖股票的最佳时机 II

    要求: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必 ...

  9. 巴什博弈----hdu2147-----较难

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)Total ...

  10. 【洛谷3467/BZOJ1113】[POI2008]海报PLA-Postering(单调栈)

    题目: 洛谷3467 分析: (ti jie shuo)这题是个单调栈经典题. 单调栈就是栈元素递增或递减的栈,这里只考虑递增.新元素入递增栈时,先将所有比它大的元素弹出,然后让新元素入栈,这样保证栈 ...