当前位置:网站首页>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;
}边栏推荐
- Esp32c3 crash analysis
- 【实习】解决请求参数过长问题
- Detailed explanation of VBScript (I)
- pytorch 模型保存的完整例子+pytorch 模型保存只保存可訓練參數嗎?是(+解决方案)
- Research Report on the overall scale, major manufacturers, major regions, products and applications of hollow porcelain insulators in the global market in 2022
- API documentation tool knife4j usage details
- [JS] get the search parameters of URL in hash mode
- How to do interface testing? After reading this article, it will be clear
- How to open an account online? Is it safe to open a mobile account?
- Solution to blue screen after installing TIA botu V17 in notebook
猜你喜欢

Web3js method to obtain account information and balance
![[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process](/img/e6/1dc747de045166f09ecdce1c5a34b1.jpg)
[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process

HDL design peripheral tools to reduce errors and help you take off!

【每日一题】241. 为运算表达式设计优先级

Conscience summary! Jupyter notebook from Xiaobai to master, the nanny tutorial is coming!

SBT tutorial

攻防世界pwn题:Recho
![[cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps](/img/af/58fbc51000cdc39de80e7c10e5b099.jpg)
[cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps

upload-labs

The first of the classic quotations of correspondents is heartbreaking
随机推荐
Postman接口测试实战,这5个问题你一定要知道
通信人的经典语录,第一条就扎心了……
【871. 最低加油次数】
Is it safe to open an account for online stock speculation? I'm a novice, please guide me
【每日一题】241. 为运算表达式设计优先级
想请教一下,究竟有哪些劵商推荐?手机开户是安全么?
JS modularization
笔记本安装TIA博途V17后出现蓝屏的解决办法
upload-labs
Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem
Interested parties add me for private chat
【Hot100】23. Merge K ascending linked lists
Web3js method to obtain account information and balance
C language linked list -- to be added
为什么我对流程情有独钟?
【Hot100】23. 合并K个升序链表
B-end e-commerce - reverse order process
AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion
Driverless learning (III): Kalman filter
Resunnet - tensorrt8.2 Speed and Display record Sheet on Jetson Xavier NX (continuously supplemented)