当前位置:网站首页>Backpack template
Backpack template
2022-07-02 20:32:00 【. dye】
01 knapsack
int dp[10000];
int v[100];
int p[100];
int main()
{ int n,w;cin>>n>>w;
for(int i=1;i<=n;i++){
cin>>v[i]>>p[i];
}
for(int i=1;i<=n;i++){
for(int j=w;j>=0;j--){
if(j>=v[i])dp[j]=max(dp[j],dp[j-v[i]]+p[i]);
}
}
cout<<dp[w];
}Completely backpack
ll dp[60000];
ll v[60000];
ll p[20000];
int main()
{ ll n,w;scanf("%lld%lld",&n,&w);
for(ll i=1;i<=n;i++){
scanf("%lld%lld",&v[i],&p[i]);
}
for(ll i=1;i<=n;i++){
for(ll j=v[i];j<=w;j++){
if(j>=v[i])dp[j]=max(dp[j],dp[j-v[i]]+p[i]);
}
}
cout<<dp[w];
}
Multiple backpack ( A monotonous queue )
int dp[60000],zu[60000], x[60000],n,m;
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
int v, w, s;
cin >> v >> w >> s;
memcpy(zu, dp, sizeof dp);
for (int j = 0; j < v; ++j) {
int a = 0, b = -1;
for (int k = j; k <= m; k += v) {
if (a <= b && x[a]<k-s* v) a ++;
while (a <= b && zu[x[b]]-(x[b] - j) / v * w <= zu[k] - (k - j) / v * w) b --;
if (a <= b) dp[k] = max(dp[k], zu[x[a]] + (k - x[a]) / v * w); x[++b] = k;
}
}
}
cout<<dp[m];
return 0;
}边栏推荐
- How can testers do without missing tests? Seven o'clock is enough
- Detailed upgrade process of AWS eks
- I would like to ask what securities dealers recommend? Is it safe to open a mobile account?
- Wu Enda's machine learning mind mapping insists on clocking in for 23 days - building a knowledge context, reviewing, summarizing and replying
- NMF-matlab
- GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of shock absorber oil in the global market in 2022
- Summary of interview experience, escort your offer, full of knowledge points
- 想请教一下,我在东莞,到哪里开户比较好?手机开户是安全么?
- [internship] solve the problem of too long request parameters
猜你喜欢

upload-labs

Interested parties add me for private chat
![[real case] trap of program design - beware of large data](/img/bd/d72cc5ce23756cea873c9ced6b642a.jpg)
[real case] trap of program design - beware of large data

CRM客户关系管理系统

ROS learning (10): ROS records multiple topic scripts

面试经验总结,为你的offer保驾护航,满满的知识点

C language linked list -- to be added

Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..

API documentation tool knife4j usage details

Implementation of online shopping mall system based on SSM
随机推荐
什么叫在线开户?现在网上开户安全么?
Use graalvm native image to quickly expose jar code as a native shared library
Research Report on the overall scale, major manufacturers, major regions, products and applications of friction dampers in the global market in 2022
The first of the classic quotations of correspondents is heartbreaking
B-end e-commerce - reverse order process
Istio deployment: quickly start microservices,
【Hot100】23. Merge K ascending linked lists
Esp32c3 crash analysis
NMF-matlab
Function, function, efficiency, function, utility, efficacy
AcWing 1135. Happy New Year (shortest path + search)
【JS】获取hash模式下URL的搜索参数
The metamask method is used to obtain account information
Codeforces Round #771 (Div. 2)(A-C)
Spark source code compilation, cluster deployment and SBT development environment integration in idea
Share several map bed websites for everyone to share pictures
疫情封控65天,我的居家办公心得分享 | 社区征文
Google Earth engine (GEE) - Landsat 9 image full band image download (Beijing as an example)
[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process
Outsourcing for three years, abandoned