您的位置:首页 > 其它

Codeforces Round #379 (Div. 2) C. Anton and Making Potions 前缀最小值+贪心+二分搜索

2016-11-16 21:14 555 查看
C. Anton and Making Potions

time limit per test
4 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare npotions.

Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two types that can faster the process
of preparing potions.

Spells of this type speed up the preparation time of one potion. There are m spells of this type, the i-th
of them costs bi manapoints
and changes the preparation time of each potion to ai instead
of x.

Spells of this type immediately prepare some number of potions. There are k such spells, the i-th
of them costs di manapoints
and instantly create ci potions.

Anton can use no more than one spell of the first type and no more than one spell of the second type, and the total number of manapoints
spent should not exceed s. Consider that all spells are used instantly and right before Anton starts to prepare potions.

Anton wants to get to the next level as fast as possible, so he is interested in the minimum number of time he needs to spent in order to prepare at least n potions.

Input

The first line of the input contains three integers n, m, k (1 ≤ n ≤ 2·109, 1 ≤ m, k ≤ 2·105) —
the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type.

The second line of the input contains two integers x and s (2 ≤ x ≤ 2·109, 1 ≤ s ≤ 2·109) —
the initial number of seconds required to prepare one potion and the number of manapoints Anton can use.

The third line contains m integers ai (1 ≤ ai < x) —
the number of seconds it will take to prepare one potion if the i-th spell of the first type is used.

The fourth line contains m integers bi (1 ≤ bi ≤ 2·109) —
the number of manapoints to use the i-th spell of the first type.

There are k integers ci (1 ≤ ci ≤ n)
in the fifth line — the number of potions that will be immediately created if the i-th spell of the second type is used. It's guaranteed
that ci are not
decreasing, i.e. ci ≤ cj if i < j.

The sixth line contains k integers di (1 ≤ di ≤ 2·109) —
the number of manapoints required to use the i-th spell of the second type. It's guaranteed that di are not
decreasing, i.e. di ≤ dj if i < j.

Output

Print one integer — the minimum time one has to spent in order to prepare n potions.

Examples

input
20 3 2
10 99
2 4 3
20 10 40
4 15
10 80


output
20


input
20 3 2
10 99
2 4 3
200 100 400
4 15
100 800


output
200


Note

In the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of
each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions
spending80 manapoints. The total number of manapoints used is 10 + 80 = 90,
and the preparation time is 4·5 = 20 seconds (15 potions
were prepared instantly, and the remaining 5 will take 4 seconds
each).

In the second sample, Anton can't use any of the spells, so he just prepares 20 potions, spending 10 seconds
on each of them and the answer is 20·10 = 200.

Source

Codeforces Round #379 (Div. 2)

My Solution

题意:要合成n瓶要,合成每瓶药水需要的初始时间是x,并且总可以使用的法力值是s,然后有2种Spells,其中一种是可以把每瓶要的时间从x变成ai,并消耗bi点法力值;另一种是可以瞬间生成ci瓶药水,并消耗di点法力值,且ci、di是非递减的。这2种Spells分别每种最多使用其中的一个Spell。求配置这些药水需要消耗的最短的时间。

其中关于前缀最小值

按照名字,ab[i].a,ab[i].b其中a是效果(设定a越小效果越好),b是花费(法力值),,然后按照b为第一优先级、a为第二优先级升序排序,

然后预处理一下,搞出前缀最小值,这个时候ab[i].a表示但花费小于等于ab[i].b时所能达到的最好的效果,

O(n)预处理,O(1)查询

前缀最小值+贪心+二分搜索

这题比赛期间写挂了,尴尬⊙﹏⊙‖∣有个地方少个==的情况,有个地方< 搞成了<= (┬_┬),没办法以后要多注意这些细节

先预处理出前缀最小值,及先把ab按照b优先升序排列,a为第二优先级,然后预处理以后
ab[i].a表示 消耗法力值<= ab[i].b是所能达到的最好效果,把原来的x缩减为现在的ab[i].a。

然后先找出只使用第一种Spells时的最小只ans;

然后开始贪心,对于每个ci,di,nn
= n - ci, ss = s - di;然后用二分搜索找出最靠右的ab[j].b <= ss的点,

然后遍历for(int
j = max((LL)0, l - 10); j < min(r + 10, m); j++)维护最小值。

其中可以在二分搜索之前 ans
= min(ans, nn * x); 以处理只使用第二种Spells的情况

由于ci、di是升序的且要找出尽可能靠后的ci,所以当ss == 0时continue继续往后扫,直到ss < 0的时候结束,此时ans里所储存的数据就是所求的最终答案了。

复杂度 O(nlogn)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 8;

struct p{
LL a, b;
}ab[maxn];

inline bool cmp(const p &a, const p &b)
{
if(a.b != b.b) return a.b < b.b;
else return a.a < b.a;
}

LL c[maxn], d[maxn];

inline bool check(const LL& x, const LL& ss)
{
if(ab[x].b <= ss) return true;  //<=
else return false;
}

int main()
{
#ifdef LOCAL
freopen("c.txt", "r", stdin);
//freopen("c.out", "w", stdout);
int T = 4;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);

LL n, m, k, x, s;
cin >> n >> m >> k;
cin >> x >> s;
for(int i = 0; i < m; i++){
cin >> ab[i].a;
}
for(int i = 0; i < m; i++){
cin >> ab[i].b;
}

for(int i = 0; i < k; i++){
cin >> c[i];
}
for(int i = 0; i < k; i++){
cin >> d[i];
}

sort(ab, ab + m, cmp);
LL mina = ab[0].a;
for(int i = 0; i < m; i++){
ab[i].a = min(mina, ab[i].a);
mina = ab[i].a;
}

//
LL ans = n * x;
for(int i = 0; i < m; i++){
if(s >= ab[i].b) ans = min(ans, n * ab[i].a);
//WA21     if(s > ab[i].b) ans = min(ans, n * ab[i].a); 掉了个==的时候,哭(┬_┬)
else break;
}

LL xx, ss, nn;
LL l = 0, r = m - 1, mid;
for(int i = 0; i < k; i++){
nn = n - c[i];
ss = s - d[i];
if(ss < 0) break;
ans = min(ans, nn * x); //WA13 考虑只用第二种Spells的情况
if(ss == 0) continue;   //之前把ss <= 0 break了,但ss == 0 是不能break而是continue
l = 0, r = m - 1;
while(l + 1 < r){
mid = (l + r) >> 1;
if(check(mid, ss)) l = mid;
else r = mid;
}
for(int j = max((LL)0, l - 10); j < min(r + 10, m); j++){
if(ab[j].b <= ss){
ans = min(ans, nn * ab[j].a);
}
else break;
}

}

cout << ans << endl;

#ifdef LOCAL
memset(ab, 0, sizeof ab);
memset(c, 0, sizeof c);
memset(d, 0, sizeof d);
cout << endl;
}
#endif // LOCAL
return 0;
}


  Thank you!

                                                                                                             
                                 ------from ProLights 

                                                                                     
                                                         ------from ProLights 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: