# 【题解】期末考试 六省联考 2017 洛谷 P3745 BZOJ 4868 贪心 三分

#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;
typedef long long ll;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int MAXN = 100010;
const int MAXM = 100010;

ll A, B, C;
int n, m, t[MAXN], b[MAXM];

void input() {
scanf( "%lld%lld%lld", &A, &B, &C );
scanf( "%d%d", &n, &m );
for( int i = 0; i < n; ++i ) scanf( "%d", t+i );
for( int i = 0; i < m; ++i ) scanf( "%d", b+i );
}

ll f( int lim ) { // 贪心计算最迟在第lim天出所有成绩的最小代价
ll y = 0;
ll left = 0, extra = 0;
for( int i = 0; i < n; ++i )
if( lim > t[i] ) y += (lim - t[i])*C;
for( int i = 0; i < m; ++i ) {
if( b[i] < lim ) left += lim - b[i];
else extra += b[i] - lim;
}
A = min(A, B);
left = min(left, extra);
y += left*A + (extra - left)*B;
return y;
}

void solve() { // 三分
int low = *min_element(t, t+n);
int high = *max_element(b, b+m);
ll ans = INFLL;
while( high-low >= 4 ) {
int lm = low + (high-low)/3;
int rm = high - (high-low)/3;
if( f(lm) < f(rm) ) high = rm;
else low = lm;
}
for( int i = low; i <= high; ++i )
ans = min(ans, f(i));
printf( "%lld\n", ans );
}

void solve2() { // 对于C=1e16的特殊数据，必须要满足所有学生的需求
int lim = *min_element(t, t+n);
ll left = 0, extra = 0;
for( int i = 0; i < m; ++i ) {
if( b[i] < lim ) left += lim - b[i];
else extra += b[i] - lim;
}
A = min(A, B);
left = min(left, extra);
ll ans = left*A + (extra - left)*B;
printf( "%lld\n", ans );
}

int main() {
input();
if( C == ll(1e16) ) solve2();
else solve();
return 0;
}

posted @ 2017-05-30 11:32 mlystdcall 阅读(...) 评论(...) 编辑 收藏