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

题目链接:https://atcoder.jp/contests/abc144/tasks/abc144_e

一道很简单的二分加贪心,但我在比赛时没过。因为我输入错了,它竟然加上样例还有6个点是对的,于是我查了半小时都没发现这件事,到最后只能怀疑是自己想法错了放弃。

(我不管我不管,是数据的锅!)至于难度的话应该有T1无T2吧

首先二分答案sum。

现在的问题是你要将A,F数组一一对应,如果A[i]*F[i]>SUM,K-=A[i]-SUM/F[i],看K最后是否小于0。

然后你就会发现其实只要把A 从小到大,F从大到小排序,然后算出来的就是最优解

为什么呢?我们可以取两个数来试试。

取A1,A2,A1<A2。

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

减去的数很显然越小越好,所以减去的数为MIN(max(0,A1-B1)+max(0,A2-B2),max(0,A1-B2)+max(0,A2-B1))

总共有六种情况,分别为

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

发现没有啊,都对应的啊

所以就直接贪心就好啦。程序如下。

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

原文地址:https://www.cnblogs.com/2014nhc/p/11751730.html

时间: 2024-11-09 09:48:08

AtCoder Beginner Contest 144:E.Gluttony【题解】的相关文章

AtCoder Beginner Contest 144 题解

传送门 $cf$ 自闭了,打 $abc$ 散散心 A - 9x9 ...这个有什么好讲的吗,题目看懂就会做了 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; typedef long long ll; inline int read() { int x=0,f=1; char

Atcoder Beginner Contest 144 F- Fork the Road(概率DP/期望DP)

Problem Statement There is a cave consisting of NN rooms and MM one-directional passages. The rooms are numbered 11 through NN . Takahashi is now in Room 11 , and Room NN has the exit. The ii -th passage connects Room sisi and Room titi (sisi < titi

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

AtCoder Beginner Contest 115 题解

题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit : 1024MB Score : 100 points Problem Statement In some other world, today is December D-th. Write a program that prints Christmas if D=25, Christmas E