[luoguP2854] [USACO06DEC]牛的过山车Cow Roller Coaster(DP + sort)
2024-08-30 23:35:01
先按照起点 sort 一遍。
这样每一个点的只由前面的点决定。
f[i][j] 表示终点为 i,花费 j 的最优解
状态转移就是一个01背包。
——代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> int L, N, B;
int f[][]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} struct node
{
int x, w, f, c;
}p[]; inline bool cmp(node x, node y)
{
return x.x < y.x;
} inline int max(int x, int y)
{
return x > y ? x : y;
} int main()
{
int i, j;
L = read();
N = read();
B = read();
for(i = ; i <= N; i++)
{
p[i].x = read();
p[i].w = read();
p[i].f = read();
p[i].c = read();
p[i].w += p[i].x;
}
std::sort(p + , p + N + , cmp);
memset(f, -, sizeof(f));
for(i = ; i <= B; i++) f[][i] = ;
for(i = ; i <= N; i++)
for(j = B; j >= p[i].c; j--)
if(f[p[i].x][j - p[i].c] ^ -)
f[p[i].w][j] = max(f[p[i].w][j], f[p[i].x][j - p[i].c] + p[i].f);
printf("%d\n", f[L][B]);
return ;
}
最新文章
- bootstrap 无限极菜单
- javascript判断手机旋转横屏竖屏
- zookeeper安装配置
- jQuery 遍历json数组的实现代码
- Java锁的种类
- 《iOS开发指南》要改iOS8版本了,听听您的意见?
- php设置和获取变量类型
- 循环语句之for循环
- GenericFactoryMethod泛型工厂模式实现简单IOC功能
- 做了5年的Android,我转Java后台了!
- BZOJ1786 [Ahoi2008]Pair 配对 动态规划 逆序对
- 数据库 简介 升级 SQLite 总结 MD
- TypeScript学习笔记(九):装饰器(Decorators)
- JS&;Jquery中的循环/遍历
- 每天CSS学习之top/left/right/bottom
- ES6学习笔记<;一>; let const class extends super
- Netty - 3 内存分配
- MFC CHotKeyCtrl控件
- Kafka与Flink集成
- JS 中 this 的用法