[ABC144E] Gluttony
Genius_Star · · 题解
思路:
一道很简单的二分加贪心,但我在比赛时没过。
因为我输入错了,它竟然加上样例还有
首先二分答案
现在的问题是你要将
然后你就会发现其实只要把
为什么呢?我们可以取两个数来试试。
取
取
减去的数很显然越小越好,所以减去的数为:
总共有六种情况,分别为(
-
A_1<A_2<B_1<B_2,T=0 -
A_1<B_1<A_2<B_2,T=0 -
B_1<A_1<A_2<B_2,T=\min(A_1-B_1,A_2-B_1)=A_1-B_1 -
A_1<B_1<B_2<A_2,T=\min(A_2-B_2,A_2-B_1)=A_2-B_2 -
B_1<A_1<B_2<A_2,T=\min(A_1-B_1+A_2-B_2,A_2-B_1)=A_1-B_1+A_2-B_2 -
B_1<B_2<A_1<A_2,T=A_1-B_1+A_2-B_2
发现没有啊,都对应的啊
所以就直接贪心就好啦。
完整代码:
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
typedef long long ll;
int n,he;
ll a[1000009],b[1000009];
ll ans=0;
bool check(ll sum,ll k)
{
he=1;
for (int i=n;i>=1;i--)
{
if (a[he]*b[i]<=sum) {he++;continue;}
if ((a[he]-sum/b[i])>k) return false;
k-=a[he]-sum/b[i];
he++;
}
return true;
}
int main()
{
ll k;
scanf("%d%lld",&n,&k);
for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
for (int i=1;i<=n;i++) scanf("%lld",&b[i]);
sort(a+1,a+1+n); sort(b+1,b+1+n);
ll l=0,r=0;
r=b[n]*a[n];ans=r;
while (l<=r)
{
ll mid=(l+r)/(ll)2;
if (check(mid,k)) ans=mid,r=mid-1;
else l=mid+1;
}
printf("%lld\n",ans);
return 0;
}