题目链接[http://codeforces.com/contest/1165/problem/F2] (Codeforces Round #560 (Div. 3))
#98kai想进wf# 小菜鸡第一次写博客!!之前一直是在一个word文档上记录自己的刷题不会的题,但是发现真的好不方便,看到身边的大佬们都在用写博客的方式来“记忆化”,于是我也就开始写博客了!一般都会更新一些codeforces或者省赛,区域赛的题目。ok废话不多说,先看这道题。
The only difference between easy and hard versions is constraints.
Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions — and he won’t start playing until he gets all of them``.
Each day (during the morning) Ivan earns exactly one burle.
There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly ki microtransactions of the i-th type (he orders microtransactions during the evening).
Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.
There are also m special offers in the game shop. The j-th offer (dj,tj) means that microtransactions of the tj-th type are on sale during the dj-th day.
Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.
Input The first line of the input contains two integers n and m (1≤n,m≤2⋅105) — the number of types of microtransactions and the number of special offers in the game shop.
The second line of the input contains n integers k1,k2,…,kn (0≤ki≤2⋅105), where ki is the number of copies of microtransaction of the i-th type Ivan has to order. It is guaranteed that sum of all ki is not less than 1 and not greater than 2⋅105.
The next m lines contain special offers. The j-th of these lines contains the j-th special offer. It is given as a pair of integers (dj,tj) (1≤dj≤2⋅105,1≤tj≤n) and means that microtransactions of the tj-th type are on sale during the dj-th day.
Output Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing.
Examples inputCopy 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 outputCopy 8 inputCopy 5 3 4 2 1 3 2 3 5 4 2 2 5 outputCopy 20 题意其实很简单,就是给你n中物品,购买每个物品需要消耗2元。每个物品都有一个需求量。然后每天(不一定)都有一些折扣,如果在当天购买有折扣的物品需要1元钱。每天你的钱数会增加1元(现增加,后购买),求至少多少天能把所有需求的物品买完。
我做的时候,乍一看以为是一个dp,因为每天都有选择买和不买两个状态,看到数据量2e5以后,就想最多nlogn,dp好像出不来,当时比赛剩余时间已经不多,加上已经凌晨一点,就放弃挣扎了。不过第二天起来,想一想二分答案,再配上贪心,其实挺简单。就是二分答案,左边界l是1,右边界r是sum*2(sum是所有物品的需求量之和)(这里注意,千万不能是sum,因为每个物品是2元。),然后每次判断在mid天之前能否买完,判断的时候用贪心的思想,先维护一个数组last[],表示第i个物品在mid天内最后一次又折扣是在哪一天,然后比那里每一个折扣(按照d从小到大排序),如果目前的折扣物品有着扣得最后一天就是目前的天数,就能买多少买多少就行了(当然,不能超过上限,就是不超过需求量)。如果所有的折扣遍历完,如果剩下的钱能把所有剩余物品按照2元每个买完,那么就return true,不然就false。维护一个答案ans,ans=min(ans,mid),最后输出ans就行。
第一次写博客,感触:写博客真的好麻烦!!!