前言
P12275 [蓝桥杯 2024 国 Python B] 工厂
题目描述
H 市是一座制造业十分发达的城市。在 H 市中,工厂可以生产 n n n 种不同的物品,部分物品都可以以特定的价格 a i a_i ai 在市场上售出而带来收益。生产方式分为两类,使用第一类生产方式每个工人可以在一天时间内生产若干件物品 y y y。使用第二类生产方式,每个工人可以在一天时间内使用若干件物品 x x x 生产若干件物品 y y y,其中 x ≤ y x \leq y x≤y,即只能将编号较小的物品加工成编号较大的物品。
小蓝作为 H 市的市长自然希望能够最大化收益,由于 H 市的人口非常多,你只需要帮她计算出平均一天内每个工人能够获得的最大收益即可。
输入格式
输入的第一行包含两个正整数 n , m n, m n,m,用一个空格分隔,其中 n n n 表示物品种数, m m m 表示生产方式总数。
第二行包含 n n n 个正整数 a i a_i ai,相邻整数之间使用一个空格分隔,表示物品的售价,若 a i = 0 a_i = 0 ai=0 则表示这种物品无法在市场上售出。
接下来 m m m 行,每行包含四个非负整数 x i , y i , k i , w i x_i, y_i, k_i, w_i xi,yi,ki,wi,相邻整数之间使用一个空格分隔,表示一个工人可以在一天时间内使用 k i k_i ki 件物品 x i x_i xi 生产 w i w_i wi 件物品 y i y_i yi。特殊地,如果 k i = 0 k_i = 0 ki=0(此时 x i x_i xi 不一定为 0 0 0,请忽略 x i x_i xi 的值)则表示该种生产方式是第一类生产方式,不需要原材料。
输出格式
输出一行包含一个实数,四舍五入保留正好两位小数,表示平均每个工人在一天时间内能够获得的最大收益。
输入输出样例 #1
输入 #1
3 3
1 0 2
1 1 0 6
1 2 5 10
2 3 6 10
输出 #1
9.52
说明/提示
样例说明
1 1 1 个工人可以在一天时间内生产 6 6 6 份小麦,或者将 5 5 5 份小麦加工成 10 10 10 份面粉,或者将 6 6 6 份面粉加工成 10 10 10 份饼干。
那么最理想的情况是 5 5 5 个工人生产小麦, 6 6 6 个工人将小麦加工成面粉, 10 10 10 个工人将面粉加工成饼干后在市场上以 2 2 2 的价格出售。
此时需要 21 21 21 个工人生产,共能获得 200 200 200 的收益。平均每个工人一天时间内获得的收益约为 9.52 9.52 9.52。
评测用例规模与约定
- 对于 50 % 50\% 50% 的评测用例, 1 ≤ n , m ≤ 300 1 \leq n, m \leq 300 1≤n,m≤300, w i = 1 w_i = 1 wi=1, 0 ≤ k i ≤ 1 0 \leq k_i \leq 1 0≤ki≤1;
- 另存在 20 % 20\% 20% 的评测用例, x i = y i x_i = y_i xi=yi;
- 对于所有评测用例, 1 ≤ n , m ≤ 300000 1 \leq n, m \leq 300000 1≤n,m≤300000, 0 ≤ a i ≤ 10 6 0 \leq a_i \leq 10^6 0≤ai≤106, 1 ≤ w i ≤ 10 1 \leq w_i \leq 10 1≤wi≤10, 0 ≤ k i ≤ 10 0 \leq k_i \leq 10 0≤ki≤10, 1 ≤ x i ≤ y i ≤ n 1 \leq x_i \leq y_i \leq n 1≤xi≤yi≤n。保证数据中至少存在一个 k i = 0 k_i = 0 ki=0。
P12275 [蓝桥杯 2024 国 Python B] 工厂
**注意一 * *:可能某些原料无法生成,忽略此生产方式。
注意二 :可能某些物品有多种生成方式。取用时最短的。
猜测一 :原料只能生成,不能买入。
由于原料编号一定小,故按输出物品编号小到大处理可以保证无后效性。
v[i] 记录1单位物品i的需要多少天time。如果 0 = = k i , M i n S e l f ( v [ y i ] , 1.0 / w i ) 0==k_i,MinSelf(v[y_i],1.0 / w_i) 0==ki,MinSelf(v[yi],1.0/wi)
如果 0 ≠ k i , M i n S e l f ( v [ y i ] , 1.0 / w i + k i / w i ∗ 原料需要时间 ) 如果0 \neq k_i, MinSelf(v[y_i], 1.0 / w_i + k_i / w_i * 原料需要时间) 如果0=ki,MinSelf(v[yi],1.0/wi+ki/wi∗原料需要时间) 如果原料,无法生产,忽略。
max 1 单位价格 1 单位需要时间 \max\frac{1单位价格}{ 1单位需要时间} max1单位需要时间1单位价格
时间复杂度:O(N)
代码
核心代码
cpp
#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:
double Ans(const int N, vector<int> price, vector<tuple<int, int, int, int>>& xykw) {
sort(xykw.begin(), xykw.end(), [&](auto& t1, auto& t2) {return get<1>(t1) < get<1>(t2); });
vector<double> times(N, 1e100);
for (auto [x, y, k, w] : xykw) {
x--, y--;
if (0 == k) {
times[y] = min(times[y], 1.0 / w);
continue;
}
if (times[x] >= 1e100) { continue; }//缺原料
times[y] = min(times[y], 1.0 / w + (double)k / w * times[x]);
}
double ans = 0;
for (int i = 0; i < N; i++) {
ans = max(ans, price[i] / times[i]);
}
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,Q;
cin >> N >> Q;
auto price = Read<int>(N);
auto xykw = Read< tuple<int, int, int,int>>(Q);
#ifdef _DEBUG
printf("N=%d", N);
Out(price, ",price=");
Out(xykw, ",xykw=");
/*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,price,xykw);
printf("%.2lf", res);
return 0;
};
单元测试
cpp
int N;
vector<int> price;
vector<tuple<int, int, int, int>> xykw;
TEST_METHOD(TestMethod11)
{
N = 3, price = { 1,0,2 }, xykw = { {1,1,0,6},{1,2,5,10},{2,3,6,10} };
auto res = Solution().Ans(N, price, xykw);
AssertEx(9.52, res,0.01);
}
# 扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步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++**实现。
