当前位置:网站首页>2022 Blue Bridge Cup group B - expense reimbursement - (linear dp| status DP)
2022 Blue Bridge Cup group B - expense reimbursement - (linear dp| status DP)
2022-06-30 16:38:00 【Lovely and beautiful girl】
The question :
It's for you. n Bills , Each note has a time and price , You can choose some , Make the difference between any two dates of these bills >=k, Then make the total value closest to m, But not more than m.
reflection :
At that time, I used the definition of i What is the maximum number that this note can represent . There is a drawback to this , This will ensure maximum , But it may exceed m, exceed m Only special judgment , So this does not guarantee the closest m. So when it doesn't feel right , You can turn the total value into dp The state of , That is, enumerate to i A.m. , by j Whether the status can reach . And then transfer it . But the complexity is nnm, nearly 1e9. So it will time out , But you can get most of the points . So it can be optimized , The optimization point is that this date only has 365 God , therefore dp[i][j] For the first time i God j Whether the status can reach . Then for the enumeration of items, you can use a variable cnt Come and go straight back , The complexity is n*m+365 Of . Because each point only goes once, it only circulates n Time m. This complexity is enough .
Code :
Classical linear dp
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define PII pair<int,int >
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1005;
int T,n,m,k;
PII va[N];
int res[N],sum[N];
int dp[1010][5010];
signed main()
{
IOS;
cin>>n>>m>>k;
res[1] = 31,res[2] = 28,res[3] = 31,res[4] = 30;
res[5] = 31,res[6] = 30,res[7] = 31,res[8] = 31;
res[9] = 30,res[10] = 31,res[11] = 30,res[12] = 31;
for(int i=1;i<=12;i++) sum[i] = sum[i-1]+res[i];
for(int i=1;i<=n;i++)
{
int a,b,c;
cin>>a>>b>>c;
int now = sum[a-1]+b;
va[i] = {
now,c};
}
sort(va+1,va+1+n);
int cnt = 1;
dp[0][0] = 1;va[0].fi = -100;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++) dp[i][j] |= dp[i-1][j];
for(int j=0;j<i;j++)
{
for(int t=va[i].se;t<=m;t++)
{
if(va[i].fi-va[j].fi>=k)
dp[i][t] |= dp[j][t-va[i].se];
}
}
}
int maxn = 0;
for(int i=m;i>=0;i--)
{
if(dp[n][i])
maxn = max(maxn,i);
}
cout<<maxn;
return 0;
}
Convert to status after optimization dp
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define PII pair<int,int >
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1005;
int T,n,m,k;
PII va[N];
int res[N],sum[N];
int dp[1010][5010];
signed main()
{
IOS;
cin>>n>>m>>k;
res[1] = 31,res[2] = 28,res[3] = 31,res[4] = 30;
res[5] = 31,res[6] = 30,res[7] = 31,res[8] = 31;
res[9] = 30,res[10] = 31,res[11] = 30,res[12] = 31;
for(int i=1;i<=12;i++) sum[i] = sum[i-1]+res[i];
for(int i=1;i<=n;i++)
{
int a,b,c;
cin>>a>>b>>c;
int now = sum[a-1]+b;
va[i] = {
now,c};
}
sort(va+1,va+1+n);
int cnt = 1;
dp[0][0] = 1;
for(int i=1;i<=365;i++)
{
for(int j=0;j<=m;j++) dp[i][j] |= dp[i-1][j];
while(cnt<=n&&va[cnt].fi==i)
{
for(int j=0;j<=m;j++)
{
if(i>=k&&j>=va[cnt].se) dp[i][j] |= dp[i-k][j-va[cnt].se];
else if(i<k&&j==va[cnt].se) dp[i][j] |= 1;
}
cnt++;
}
}
int maxn = 0;
for(int i=m;i>=0;i--)
{
if(dp[365][i])
maxn = max(maxn,i);
}
cout<<maxn;
return 0;
}
summary :
Think more , Think about similar solutions and optimizations .
边栏推荐
- 药品管理系统加数据库,一夜做完,加报告
- 构建适合组织的云原生可观测性能力
- Implementation of Devops in the core field of qunar, the Internet R & D Efficiency
- Policy Center-Permissions and APIs that Access Sensitive Information
- 技不压身,快速入门ETH智能合约开发,带你进入ETH世界
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- After 15 years of working on 21 types of hardware, where is Google?
- 《网络是怎么样连接的》读书笔记 - 汇总篇
- Smart wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
- register_chrdev和cdev_init cdev_add用法区别
猜你喜欢

Build cloud native observability capability suitable for organizations
MySQL8.0开启远程连接权限的方法步骤

Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world

Implementation of Devops in the core field of qunar, the Internet R & D Efficiency

“低代码”在企业数字化转型中扮演着什么角色?

Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022

微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”

halcon知识:区域专题【07】

Policy Center > Malware > Malware

The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
随机推荐
How cloudxr promotes the future development of XR
RT-Thread 堆區大小設置
牛客网:乘积为正数的最长连续子数组
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
Cloud XR, how to help industrial upgrading
Lecun points out the direction of next generation AI: autonomous machine intelligence
'&lt;', hexadecimal value 0x3C, is an invalid 问题解决
如何得到股票开户的优惠活动?在线开户安全么?
Policy Center > Misrepresentation
MySQL开放远程连接权限的两种方法
Yunhe enmo won the bid for Oracle maintenance project of Tianjin Binhai rural commercial bank in 2022-2023
In depth analysis of the core code of the gadgetinspector
Dart: string replace related methods to solve replacement characters
I implement "stack" with C I
Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution
Log4j2 进阶使用
快照和备份
【Verilog基础】关于Clock信号的一些概念总结(clock setup/hold、clock tree、clock skew、clock latency、clock transition..)
思源笔记:能否提供页面内折叠所有标题的快捷键?
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)