E. Best Price

题目链接👈

题目描述🥰

题目思路😀

这个题目的贪心思路是这个题目我没有想到的一个点,思考了很久😇

首先 a数组 和 b数组 的联合中的每一个整数都可能是最大答案

Question:为什么通过枚举 a 和 b 数组中的值已经能够找到全局最优解?

我们可以假设存在一个不在 a 和 b 联合集合中的整数价格 p 是最优价格。由于 p 不在 a 和 b 数组的值中,那么对于任意客户 ip 必然处于某个区间 (a[j], b[j]) 之间(或是小于a[j]),且 p 不等于 a[j] 和 b[j]

当我们将价格从 p 变为 p + 1 时:
  • 对于那些 a[i] > p 的客户 i,因为 a[i] >= p + 1 > p,所以他们依然会购买商品且给出好评,客户状态不变。
  • 对于那些 b[i] < p 的客户 i,因为 b[i] < p < p + 1,所以他们依然不会购买商品,客户状态不变。
  • 对于那些 a[i] < p < b[i] 的客户 i,由于 p 和 p + 1 都在区间 (a[i], b[i]) 内(因为 p 不在 a 和 b 数组中,且 p 和 p + 1 很接近),所以这些客户依然会购买商品且给出负面评价,客户状态不变。
也就是说,当我们将价格从 p 变为 p + 1 时,每个客户的购买状态和评价状态都保持不变。

这就说明如果 p 是一个不在 a 和 b 联合集合中的价格,那么 p + 1 的收益会比 p 的收益更高,这与 p 是最优价格相矛盾。

所以问题就变的很简单了,我们直接枚举a集合和b集合里面的价钱,对于每一个价格进行计算,最后对满足条件的价钱产生的收益取最大值即可

AC代码🧠

// Problem: E. Best Price
// Contest: Codeforces - Codeforces Round 995 (Div. 3)
// URL: https://codeforces.com/contest/2051/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

#define dev1(a) cout << #a << '=' << a << endl;
#define dev2(a, b) cout << #a << " = " << a << "  " << #b << " = " << b << endl;
#define dev3(a, b, c) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << endl;
#define dev4(a, b, c, d) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << "  " << #d << " = " << d << endl;
#define dev5(a, b, c, d, e) cout << #a << " = " << a << "  " << #b << " = " << b << "  " << #c << " = " << c << "  " << #d << " = " << d << "  " << #e << " = " << e << endl;
#define vec(a)                         \
    for (int i = 0; i < a.size(); i++) \
        cout << a[i] << ' ';           \
    cout << endl;
#define darr(a, _i, _n)               \
    cout << #a << ':';                \
    for (int ij = _i; ij <= _n; ij++) \
        cout << a[ij] << ' ';         \
    cout << endl;           
#define cin(a,n)           \
     for(int i=0;i<n;i++) \
      cin>>a[i];          
#define endl "\n"
#define pow pim
int pim(int a,int k)
{
    int res=1;
    if(a==0)return 0;
    while(k) 
    {
        if(k&1)res=(int)res*a;
        k>>=1;
        a=(int)a*a;
    }
    return res;
}
#define fi first
#define se second
#define caseT \
    int T;    \
    cin >> T; \
    while (T--)
#define int long long
// #define int __int128

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 99999999;

const int N = 2e5+10;
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
 
int lcm(int a, int b)
{
    return a * b / gcd(a, b);
}
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
void solve()
{
	int n,k;cin>>n>>k;
   vector<int>a(n),b(n),price;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
       price.push_back(a[i]);
   }
   for(int i=0;i<n;i++)
   {
       cin>>b[i];
       price.push_back(b[i]);
   }
   sort(a.begin(),a.end());
   sort(b.begin(),b.end());
   sort(price.begin(),price.end());
   price.erase(unique(price.begin(),price.end()),price.end());
   int  ans=0;
   //枚举所有价格
   for(auto p:price) 
   {
      int buy=n-(lower_bound(b.begin(),b.end(),p)-b.begin());
       int  good=n-(lower_bound(a.begin(),a.end(),p)-a.begin());
       int  bad=buy-good;
       if(bad<=k)
       {
           ans=max(ans,p*buy);
       }
   }   
    cout<<ans<<endl;
}

signed main()
{

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	caseT
    solve();
    return 0;
}
/*
   0<=x<=ai 好评
   ai<x<=bi 负面
   x>bi 不买
*/

 

 posted on 2025-03-02 22:57  熙玺  阅读(86)  评论(0)    收藏  举报

Shu-How Zの小窝

Loading...