E. Best Price
题目链接👈
题目描述🥰

题目思路😀
这个题目的贪心思路是这个题目我没有想到的一个点,思考了很久😇
首先 a数组 和 b数组 的联合中的每一个整数都可能是最大答案
Question:为什么通过枚举 a 和 b 数组中的值已经能够找到全局最优解?
我们可以假设存在一个不在 a 和 b 联合集合中的整数价格 p 是最优价格。由于 p 不在 a 和 b 数组的值中,那么对于任意客户 i,p 必然处于某个区间 (a[j], b[j]) 之间(或是小于a[j]),且 p 不等于 a[j] 和 b[j]。
这就说明如果 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
浙公网安备 33010602011771号