codeforces 883J Renovation 贪心,好题,有意义

来源:互联网 发布:北邮孙腾霄 知乎 编辑:程序博客网 时间:2024/05/14 20:42

J. Renovation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The mayor of the Berland city S sees the beauty differently than other city-dwellers. In particular, he does not understand at all, how antique houses can be nice-looking. So the mayor wants to demolish all ancient buildings in the city.

The city S is going to host the football championship very soon. In order to make the city beautiful, every month the Berland government provides mayor a money tranche. The money has to be spent on ancient buildings renovation.

There are n months before the championship and the i-th month tranche equals to ai burles. The city S has m antique buildings and the renovation cost of the j-th building is bj burles.

The mayor has his own plans for spending the money. As he doesn't like antique buildings he wants to demolish as much of them as possible. For the j-th building he calculated its demolishing cost pj.

The mayor decided to act according to the following plan.

Each month he chooses several (possibly zero) of m buildings to demolish in such a way that renovation cost of each of them separatelyis not greater than the money tranche ai of this month (bj ≤ ai) — it will allow to deceive city-dwellers that exactly this building will be renovated.

Then the mayor has to demolish all selected buildings during the current month as otherwise the dwellers will realize the deception and the plan will fail. Definitely the total demolishing cost can not exceed amount of money the mayor currently has. The mayor is not obliged to spend all the money on demolishing. If some money is left, the mayor puts it to the bank account and can use it in any subsequent month. Moreover, at any month he may choose not to demolish any buildings at all (in this case all the tranche will remain untouched and will be saved in the bank).

Your task is to calculate the maximal number of buildings the mayor can demolish.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the number of months before the championship and the number of ancient buildings in the city S.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the tranche of the i-th month.

The third line contains m integers b1, b2, ..., bm (1 ≤ bj ≤ 109), where bj is renovation cost of the j-th building.

The fourth line contains m integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the demolishing cost of the j-th building.

Output

Output single integer — the maximal number of buildings the mayor can demolish.

题意:

给出n个月,每个月的预算为ai。

给出m个建筑,每个建筑的b值为bi,还有一个值就是拆毁所需要花费的值,记为pi。

每个月可以拆除的建筑的b值不能超过当月的预算ai。预算用不完可以累计到下一个月。

最大化能拆毁的建筑数量。

题解:

这个贪心核心思想在于转换思考对象。

正常情况下我们以每个月进行分析,看那些能拆毁,但是我们并没有想出全局最优的拆毁方案。

那么我们就改变想法,以建筑进行考虑。思考如下问题:

每个建筑j可以在什么时候进行拆除,那就是ai>=bj的第i个月份。

那么建筑j可以使用的金钱是什么呢?

那就是最后一个i使得ai>=bj的区间a[1,i]的钱,也就是sum[i]

ok,到这里就可以了,那么我们让每个建筑都在他最后的ai那里被拆毁。

这样的话对于每个ai,可能有多个建筑要被拆毁,显然是贪心的选取最便宜的进行拆毁啦。然后就ojbk了。


对a序列进行倒着遍历,每次把小于ai的建筑p值的全部丢进小顶堆里面去。用当前的钱来摧毁堆顶的建筑,钱不够就把建筑摧毁一部分,然后再丢回堆里面去。

代码:

#include <bits/stdc++.h>using namespace std;typedef pair<int,int> pii;typedef long long LL;int n,m;const int maxn = 1e5+7;int a[maxn];pii ps[maxn];int main(){a[0] = 1e9+7;scanf("%d%d",&n,&m);for(int i = 1;i <= n;++i) scanf("%d",&a[i]);for(int i = 1;i <= m;++i) scanf("%d",&ps[i].first);for(int i = 1;i <= m;++i) scanf("%d",&ps[i].second);sort(ps+1,ps+1+m);priority_queue<int,vector<int>,greater<int> >Q;int j = 0,k,pre = 1;LL tot;int ans = 0;for(int i = n;i >= 1;i = j){tot = 0;for(j = i;a[j] <= a[i];j--) tot += a[j];for(k = pre;k <= m && ps[k].first <= a[i];++k) Q.push(ps[k].second);pre = k;while(!Q.empty()){if(tot >= Q.top()) ans++,tot -= Q.top(),Q.pop();else{tot = Q.top()-tot;Q.pop();Q.push(tot);break;}}}cout<<ans<<endl;return 0;} 





阅读全文
0 0
原创粉丝点击