AtCoder Beginner Contest 144:E.Gluttony【题解】

Topic links: https://atcoder.jp/contests/abc144/tasks/abc144_e

A very simple dichotomy plus greedy, but I have not seen in the game. Because I entered was wrong, it turned out there are six points plus sample was right, so I checked half an hour did not find it, in the end can only suspect is wrong to give up their own ideas.

(I do not care I do not care, is the pot of data!) As for the difficulty, then there should be no T1 T2 it

First-half answer sum.

The question now is you want A, F array of correspondence, if A [i] * F [i]> SUM, K- = A [i] -SUM / F [i], to see the last K is less than 0.

Then you will find that as long as the A small to large, F descending order, then counted out is the optimal solution

why? We can take two numbers to try.

Take A1, A2, A1 <A2.

取B1=SUM/F1,B2=SUM/F2,F1>F2。

Obviously subtracting the number as small as possible, so the subtracted number MIN (max (0, A1-B1) + max (0, A2-B2), max (0, A1-B2) + max (0, A2-B1))

A total of six cases, respectively

1.A1<A2<B1<B2 MIN=0

2.A1<B1<A2<B2 MIN=0

3.B1<A1<A2<B2 MIN=min(A1-B1,A2-B1)=A1-B1

4.A1<B1<B2<A2 MIN=min(A2-B2,A2-B1)=A2-B2

5.B1<A1<B2<A2 MIN=min(A1-B1+A2-B2,A2-B1)=A1-B1+A2-B2

6.B1<B2<A1<A2 MIN=A1-B1+A2-B2

We found no ah, ah corresponding

So directly greedy thousand million. Procedure is as follows.

#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;
}

 

Guess you like

Origin www.cnblogs.com/2014nhc/p/11751730.html