当前位置: 首页> 科技> 数码 > 黄骅市怎么读_广州市工贸技师学院_chatgpt入口_seo交流qq群

黄骅市怎么读_广州市工贸技师学院_chatgpt入口_seo交流qq群

时间:2025/7/11 8:06:52来源:https://blog.csdn.net/2301_80215285/article/details/144853328 浏览次数:0次
黄骅市怎么读_广州市工贸技师学院_chatgpt入口_seo交流qq群

在这里插入图片描述在这里插入图片描述

DP加双指针解法:200ms

1.第一种

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[105], b[105], f[105];
int main(){int t; cin >> t;while(t--){int n, k; cin >> n >> k;for(int i = 0; i < n; i++) cin >> a[i];for(int i = 0; i < n; i++) cin >> b[i];f[0] = b[0];int ans = 0;for(int i = 1; i < n; i++){f[i] = b[i];for(int j = i - 1; j >= 0; j--)if(a[i] > a[j] + k) f[i] = max(f[i], f[j] + b[i]);else f[i] = max(f[j], f[i]);}//for(int i = 0; i < n; i++) ans = max(ans, f[i]);cout << f[n-1] << endl;}return 0;
}

2.第二种

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[105], b[105], f[105];
int main(){int t; cin >> t;while(t--){int n, k; cin >> n >> k;for(int i = 0; i < n; i++) cin >> a[i];for(int i = 0; i < n; i++) cin >> b[i];f[0] = b[0];int ans = 0;for(int i = 1; i < n; i++){f[i] = b[i];for(int j = i - 1; j >= 0; j--)if(a[i] > a[j] + k) f[i] = max(f[i], f[j] + b[i]);}for(int i = 0; i < n; i++) ans = max(ans, f[i]);cout << ans << endl;}return 0;
}
关键字:黄骅市怎么读_广州市工贸技师学院_chatgpt入口_seo交流qq群

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: