【贪心 决策包容性】P12331 [蓝桥杯 2023 省 Java B] 最大开支|普及+
本文涉及知识点
P12331 [蓝桥杯 2023 省 Java B] 最大开支
题目描述
小蓝所在学校周边新开业了一家游乐园,小蓝作为班长,打算组织大家去游乐园玩。已知一共有 N N N 个人参加这次活动,游乐园有 M M M 个娱乐项目,每个项目都需要买门票后才可进去游玩。门票的价格并不是固定的,团购的人越多单价越便宜,当团购的人数大于某个阈值时,这些团购的人便可以免费进入项目进行游玩。这 M M M 个娱乐项目是独立的,所以只有选择了同一个项目的人才可以参与这个项目的团购。第 i i i 个项目的门票价格 H i ( X ) H_i(X) Hi(X) 与团购的人数 X X X 的关系可以看作是一个函数:
H i ( X ) = max ( K i × X + B i , 0 ) H_i(X) = \max(K_i \times X + B_i, 0) Hi(X)=max(Ki×X+Bi,0)
其中 max \max max 表示取二者之中的最大值。当 H i = 0 H_i = 0 Hi=0 时说明团购人数达到了此项目的免单阈值。
这 N N N 个人可以根据自己的喜好选择 M M M 个娱乐项目中的一种,或者有些人对这些娱乐项目都没有兴趣,也可以选择不去任何一个项目。每个人最多只会选择一个娱乐项目,如果多个人选择了同一个娱乐项目,那么他们都将享受对应的团购价格。小蓝想知道他至少需要准备多少钱,使得无论大家如何选择,他都有能力支付得起所有 N N N 个人购买娱乐项目的门票钱。
输入格式
第一行两个整数 N N N、 M M M,分别表示参加活动的人数和娱乐项目的个数。接下来 M M M 行,每行两个整数,其中第 i i i 行为 K i K_i Ki、 B i B_i Bi,表示第 i i i 个游乐地点的门票函数中的参数。
输出格式
一个整数,表示小蓝至少需要准备多少钱,使得大家无论如何选择项目,自己都支付得起。
输入输出样例 #1
输入 #1
4 2
-4 10
-2 7
输出 #1
12
说明/提示
样例说明
样例中有 4 4 4 个人, 2 2 2 个娱乐项目,我们用一个二元组 ( a , b ) (a, b) (a,b) 表示 a a a 个人选择了第一个娱乐项目, b b b 个人选择了第二个娱乐项目,那么就有 4 − a − b 4 - a - b 4−a−b 个人没有选择任何项目,方案 ( a , b ) (a, b) (a,b) 对应的门票花费为 max ( − 4 × a + 10 , 0 ) × a + max ( − 2 × b + 7 , 0 ) × b \max(-4 \times a + 10, 0) \times a + \max(-2 \times b + 7, 0) \times b max(−4×a+10,0)×a+max(−2×b+7,0)×b,所有的可能如下所示:
| a a a | b b b | 花费 |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 5 |
| 0 | 2 | 6 |
| 0 | 3 | 3 |
| 0 | 4 | 0 |
| 1 | 0 | 6 |
| 1 | 1 | 11 |
| 1 | 2 | 12 |
| 1 | 3 | 9 |
| 2 | 0 | 4 |
| 2 | 1 | 9 |
| 2 | 2 | 10 |
| 3 | 0 | 0 |
| 3 | 1 | 5 |
| 4 | 0 | 0 |
其中当 a = 1 , b = 2 a = 1, b = 2 a=1,b=2 时花费最大,为 12 12 12。此时 1 1 1 个人去第一个项目,所以第一个项目的单价为 10 − 4 = 6 10 - 4 = 6 10−4=6,在这个项目上的花费为 6 × 1 = 6 6 \times 1 = 6 6×1=6; 2 2 2 个人去第二个项目,所以第二个项目得单价为 7 − 2 × 2 = 3 7 - 2 \times 2 = 3 7−2×2=3,在这个项目上的花费为 2 × 3 = 6 2 \times 3 = 6 2×3=6;还有 1 1 1 个人没去任何项目,不用统计;总花费为 12 12 12,这是花费最大的一种方案,所以答案为 12 12 12。
评测用例规模与约定
- 对于 30 % 30\% 30% 的评测用例, 1 ≤ N , M ≤ 10 1 \leq N, M \leq 10 1≤N,M≤10。
- 对于 50 % 50\% 50% 的评测用例, 1 ≤ N , M ≤ 1000 1 \leq N, M \leq 1000 1≤N,M≤1000。
- 对于 100 % 100\% 100% 的评测用例, 1 ≤ N , M , B i ≤ 10 5 1 \leq N, M, B_i \leq 10^5 1≤N,M,Bi≤105, − 10 5 ≤ K i < 0 -10^5 \leq K_i < 0 −105≤Ki<0。
P12331 [蓝桥杯 2023 省 Java B] 最大开支
令 f i ( x ) = H i ( x ) × x f_i(x)=H_i(x) \times x fi(x)=Hi(x)×x,表示x人参加项目i的总费用。
某项目,暂时用f代替 f i f_i fi。x人变成x+1人,总费用的变化 g ( i , x ) = f ( x + 1 ) − f ( x ) = k ( x + 1 ) ( x + 1 ) + b x + b − ( k x x + b x ) = 2 k x + k + b g(i,x)=f(x+1)-f(x)= k(x+1)(x+1)+bx+b-(kxx+bx)= 2kx+k+b g(i,x)=f(x+1)−f(x)=k(x+1)(x+1)+bx+b−(kxx+bx)=2kx+k+b
性质一:g(i,x)<0,则i项目最多x-1人增加。因为:y > x,k < 0,则g(i,y)<g(i,x)。即g(i,x)单调递减。
如果i项目参加x人,则ans += g(i,z) , z ∈ [ 0 , x − 1 ] z\in[0,x-1] z∈[0,x−1]
初始:将g(i,0)放入大根堆中,g(i,0)出堆后,将g(i,1)入堆。出堆m次, 堆顶 ≤ 0 时提前结束 堆顶 \leq 0时提前结束 堆顶≤0时提前结束。
本题不会陷入局部最优解。
某解存在(i1,x1)不存在(i2,x2),且g(i2,x2) > g(i1,x1)。
如果此解不存在(i2,x2-1) x2–,不断迭代,直到x等于0或(i2,x2-1)存在。
求解中存在(i1,x)最大x,用(i2,x2)替换。
代码
核心代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>
#include <bitset>
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T1, class T2, class T3, class T4, class T5, class T6, class T7 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4,T5,T6,T7>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class COutBuff
{
public:
COutBuff() {
m_p = puffer;
}
template<class T>
void write(T x) {
int num[28], sp = 0;
if (x < 0)
*m_p++ = '-', x = -x;
if (!x)
*m_p++ = 48;
while (x)
num[++sp] = x % 10, x /= 10;
while (sp)
*m_p++ = num[sp--] + 48;
AuotToFile();
}
void writestr(const char* sz) {
strcpy(m_p, sz);
m_p += strlen(sz);
AuotToFile();
}
inline void write(char ch)
{
*m_p++ = ch;
AuotToFile();
}
inline void ToFile() {
fwrite(puffer, 1, m_p - puffer, stdout);
m_p = puffer;
}
~COutBuff() {
ToFile();
}
private:
inline void AuotToFile() {
if (m_p - puffer > N - 100) {
ToFile();
}
}
char puffer[N], * m_p;
};
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
class Solution {
public:
long long Ans(const int N, vector<pair<int, int>>& kb) {
priority_queue<pair<long long, int>> heap;
for (const auto& [k, b] : kb) {
heap.emplace(k + b, k);
}
long long ans = 0;
for (int i = 0; (i < N) && (heap.top().first > 0); i++) {
const auto [price, k] = heap.top(); heap.pop();
ans += price;
heap.emplace(price + 2LL * k, k);
}
return ans;
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0); cin.tie(nullptr);
//CInBuff<> in; COutBuff<10'000'000> ob;
int N,M;
cin >> N >> M;
auto kb = Read<pair<int, int>>(M);
#ifdef _DEBUG
printf("N=%d", N);
Out(kb, ",kb=");
//Out(P, ",P=");
/*Out(edge, ",edge=");
Out(que, ",que=");*/
//Out(ab, ",ab=");
//Out(par, "par=");
//Out(que, "que=");
//Out(B, "B=");
#endif // DEBUG
auto res = Solution().Ans(N,kb);
cout <<res;
return 0;
};
单元测试
vector<pair<int, int>> kb;
int N;
TEST_METHOD(TestMethod11)
{
N = 4, kb = { {-4,10},{-2,7} };
auto res = Solution().Ans(N, kb);
AssertEx(12LL, res);
}
# 扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。
更多推荐



所有评论(0)