本文涉及知识点
P8803 [蓝桥杯 2022 国 B] 费用报销
题目描述
小明在出差结束后返回了公司所在的城市,在填写差旅报销申请时,粗心的小明发现自己弄丢了出差过程中的票据。
为了弥补小明的损失,公司同意小明用别的票据进行报销,但是公司财务要求小明提交的票据中任意两张的日期差不小于 K K K 天,且总金额不得超过实际差旅费用 M M M。
比如财务要求 K = 7 K=7 K=7 时,若小明提交了一张 1 月 8 日的票据,小明就不能提交 1 月 2 日至 1 月 14 日之间的其他票据,1 月 1 日及之前和 1 月 15 日及之后的票据则可以提交。
公司的同事们一起给小明凑了 N N N 张票据,小明现在想要请你帮他整理一下,从中选取出符合财务要求的票据, 并使总金额尽可能接近 M M M 。
需要注意,由于这些票据都是同一年的,因此 12 月底的票据不会影响到 1 月初票据的提交。这一年不是闰年。
输入格式
第 1 1 1 行: 3 3 3 个整数, N , M , K N, M, K N,M,K。
第 2 ... N + 1 2 \ldots N+1 2...N+1 行:每行 3 个整数 m i , d i , v i m_{i}, d_{i}, v_{i} mi,di,vi, 第 i + 1 i+1 i+1 行表示第 i i i 张票据时间的月份 m i m_{i} mi 和日期 d i d_{i} di, v i v_{i} vi 表示该票据的面值。
输出格式
第 1 1 1 行: 1 1 1 个整数, 表示小明能够凑出的最大报销金额。
输入输出样例 #1
输入 #1
4 16 3
1 1 1
1 3 2
1 4 4
1 6 8
输出 #1
10
说明/提示
【样例说明】
选择 1 月 3 日和 1 月 6 日的票据
【评测用例规模与约定】
对于 100 % 100 \% 100% 的评测用例, 1 ≤ N ≤ 1000 , 1 ≤ M ≤ 5000 , 1 ≤ K ≤ 50 , 1 ≤ m i ≤ 1 \leq N \leq 1000,1 \leq M \leq 5000,1 \leq K \leq 50,1 \leq m_{i} \leq 1≤N≤1000,1≤M≤5000,1≤K≤50,1≤mi≤ 12 , 1 ≤ d i ≤ 31 , 1 ≤ v i ≤ 400 12,1 \leq d_{i} \leq 31,1 \leq v_{i} \leq 400 12,1≤di≤31,1≤vi≤400
日期保证合法。
蓝桥杯 2022 国赛 B 组 F 题。
月日改成相对当年是第几个月,第i个月第d天,则是当前年第d-a[i]天。a[1]=0,a[i+1]=a[i]+b[i]。b[i]是当前月天数。
不是闰年,故b[2]=28,b[1]=b[3]=b[5]=b[7]=b[8]=b[10]=b[12]=31,其它月都是30天。
支票按日期升序排序。有序集合记录和当前支票不冲突的方案的最大总面值,小根堆记录和当前支票冲突的支票的日期和面值。
用[cur,w]枚举当前支票。小根堆中时间<= cur-K的支票,出堆入集合。如果w > W,忽略。curAns = w;it = s.upper(W-w);
curAns = (--it)+ w。 cur和curAns入堆。curAns的最大值就是答案。
时间复杂度 :O(nlogn)
错误原因:4张日期互不冲突的支票1,1,2,2,M=5,结果是5,而不是4。
正确解法
由于已经按时间排序,故用队列代替小根堆。
用向量canPre 代替s。初始canPre[0]=真,canPre[1...W]=假
时间复杂度 :O(NW)
w+i,如果<=W 入队列。pre[i]为真。
代码
核心代码
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 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:
int Ans(const int W, const int K, vector<tuple<int, int, int>>& mdw) {
vector<int> b(13, 31), a(13);
b[2] = 28;
b[4] = b[6] = b[9] = b[11] = 30;
for (int i = 2; i <= 12; i++) {
a[i] = a[i - 1] + b[i - 1];
}
vector<pair<int, int>> dw;
for (const auto& [m, d, w] : mdw) {
dw.emplace_back(a[m] + d, w);
}
sort(dw.begin(), dw.end());
dw.emplace_back(10000, W + 1);
queue<pair<int, int>> que;
vector<bool> hasCan(1 + W);
hasCan[0] = true;
for (const auto& [d, w] : dw) {
while (que.size() && (que.front().first + K <= d)) {
hasCan[que.front().second] = true;
que.pop();
}
for (int i = 0; i <= W - w; i++) {
if (!hasCan[i]) { continue; }
que.emplace(d, i + w);
}
}
for (int i = W; i >= 0; i--) {
if (hasCan[i]) { return i; }
}
return -1;
}
};
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,K;
in >> N >> M >> K ;
auto mdw = in.Read<tuple<int,int,int>>(N);
auto res = Solution().Ans(M,K,mdw);
cout << res << "\n";
#ifdef _DEBUG
printf("M=%d,K=%d", M,K);
Out(mdw, ",mdw=");
//Out(que, "que=");
//Out(B, "B=");
//Out(que, "que=");
//Out(B, "B=");
#endif // DEBUG
return 0;
}
单元测试
cpp
int M,K;
vector<tuple<int, int, int>> mdw;
TEST_METHOD(TestMethod11)
{
M = 16, K = 3, mdw = { {1,1,1},{1,3,2},{1,4,4},{1,6,8} };
auto res = Solution().Ans(M,K,mdw);
AssertEx(10, res);
}
TEST_METHOD(TestMethod12)
{
M = 16, K = 3, mdw = { {1,1,7},{1,4,9} };
auto res = Solution().Ans(M, K, mdw);
AssertEx(16, res);
}
TEST_METHOD(TestMethod13)
{
M = 15, K = 3, mdw = { {1,1,7},{1,4,9} };
auto res = Solution().Ans(M, K, mdw);
AssertEx(9, res);
}
TEST_METHOD(TestMethod14)
{
M = 16, K = 3, mdw = { {1,28,7},{2,3,9} };
auto res = Solution().Ans(M, K, mdw);
AssertEx(16, res);
}
TEST_METHOD(TestMethod15)
{
M = 16, K = 3, mdw = { {1,28,7},{2,4,16} };
auto res = Solution().Ans(M, K, mdw);
AssertEx(16, res);
}
TEST_METHOD(TestMethod16)
{
M = 5, K = 1, mdw = { {1,1,1},{1,2,1},{1,3,2},{1,4,2} };
auto res = Solution().Ans(M, K, mdw);
AssertEx(5, 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++**实现。