1475 建设国家 DP
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1475
这题转化过来就是,给定n个点,每个点都有一个过期时间,一个价值。现在安排他们成一直线,使得总和最大。
一开始就是贪心,这是一个很经典的贪心。
http://www.cnblogs.com/liuweimingcprogram/p/6358456.html
和这题差不多,然后这个比较特别,有一个地方是可以接两个,我就暴力枚举了。然后蜜汁wa
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e3 + ;
struct node {
int val, cost;
node(int a, int b) : val(a), cost(b) {}
node() {}
bool operator < (const struct node & rhs) const {
if (val != rhs.val) return val > rhs.val;
else return cost < rhs.cost;
}
}a[maxn];
int w[maxn];
bool del[maxn];
vector<struct node>vc;
void work() {
int n, H;
cin >> n >> H;
for (int i = ; i <= n; ++i) {
cin >> a[i].cost >> a[i].val;
a[i].cost = H - a[i].cost;
}
sort(a + , a + + n);
int ans = ;
for (int i = ; i <= n; ++i) {
int t = a[i].cost;
if (t > n) {
ans += a[i].val; //必定可以,空位也少了一个了,就放去第n天
del[i] = true;
continue;
}
while (t >= && w[t]) {
t--;
}
if (t) {
w[t] = a[i].val;
ans += a[i].val;
del[i] = true;
}
}
for (int i = ; i <= n; ++i) {
if (del[i]) continue;
vc.push_back(a[i]);
}
// cout << w[1] << endl;
// cout << w[2] << endl;
// cout << w[3] << endl;
// cout << w[4] << endl;
// cout << ans << endl;
int tans = ;
for (int i = ; i <= n; ++i) {
tans += w[i];
int tofind = -inf;
if (w[i] == ) continue;
for (int j = ; j < vc.size(); ++j) {
if (vc[j].cost < i) continue;
tofind = max(tofind, vc[j].val);
}
ans = max(ans, tans + tofind);
}
cout << ans << endl;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
看了题解后居然是dp
用dp[i][j]表示前i个点中,选出了j个点,的最大答案。
那么,要求解dp[i][j],则有dp[i][j] = dp[i - 1][j],就是不选第i个点。
如果要选,那么有两种情况,
1、把这个点作为结束点,也就是两个点放在一起。这需要这个点的保质期 >= j - 1即可。因为现在求解的是选了j个点,那么以前就是选了j - 1个点,排在一起,所以只需要保质期 >= j - 1,然后更新答案,不更新dp数组
2、接在后一排,那么需要保质期 >= j
特别地,一开始就过期的,就不能选。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e3 + ;
int dp[maxn][maxn];
struct node {
int cost, val;
bool operator < (const struct node & rhs) const {
if (cost != rhs.cost) return cost < rhs.cost;
else return val > rhs.val;
}
}a[maxn];
void work() {
int n, h;
cin >> n >> h;
for (int i = ; i <= n; ++i) {
cin >> a[i].cost >> a[i].val;
a[i].cost = h - a[i].cost;
}
sort(a + , a + + n);
// memset(dp, -0x3f, sizeof dp);
dp[][] = ;
int ans = ;
for (int i = ; i <= n; ++i) {
if (a[i].cost == ) continue;
for (int j = ; j <= i; ++j) {
dp[i][j] = dp[i - ][j];
if (a[i].cost >= j - ) {
ans = max(ans, dp[i - ][j - ] + a[i].val);
}
if (a[i].cost >= j) {
dp[i][j] = max(dp[i][j], dp[i - ][j - ] + a[i].val);
}
}
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
1475 建设国家 DP的更多相关文章
- 51 Nod 1475 建设国家 (优先队列+贪心)
1475 建设国家 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城 ...
- 51nod 1475:建设国家 优先队列的好题
1475 建设国家 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城市 ...
- 51nod建设国家
小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城市. 现在小C想把国家建造成这样的形状:选若干(可以是0个)的中间站把他们连成一条直线,然后把首都连在这一条直线的左端.然 ...
- 【BZOJ】1096: [ZJOI2007]仓库建设(dp+斜率优化)
http://www.lydsy.com/JudgeOnline/problem.php?id=1096 首先得到dp方程(我竟然自己都每推出了QAQ)$$d[i]=min\{d[j]+cost(j+ ...
- BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)
[ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在 ...
- poj - 1170 - Shopping Offers(减少国家dp)
意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 & ...
- [ACM] hdu 5045 Contest (减少国家Dp)
Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP
意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...
随机推荐
- MongoDB中对象反序列化的一个小问题
今天在mongoDB存取对象数据的时候,碰到一个小问题:对象的某一个字段类型是抽象类或者接口.在存入的时候没有问题.可是在读取的时候,因为没有详细类的信息,无法完毕对象的又一次构建.就会报错: Can ...
- robotframework接口自动化
robot framework框架在测试接口上比soapUI好用的多,在此介绍下get方法的HTTP接口,其实这个接口也是把POST数据作为参数进行get请求,使用post 方法也是一样,一共6步就可 ...
- javascript/jquery模板引擎——Handlebars初体验
Handlebars.js下载地址:http://handlebarsjs.com/ 最近自己在建一个站,采用完全的前后端分离的方式,现在正在做前端的部分.其中有项功能是需要ajax调用后端接口,返回 ...
- hadoop生态系统学习之路(十)MR将结果输出到hbase
之前讲了MR将结果输出到hdfs.hive.db,今天再给大家分享一下,怎样将结果输出到hbase. 首先,提一句,笔者在hadoop集群运行此MR的时候报了一个错误.是一个jar包的缘故,这个错误是 ...
- 本人会linux系统的各种版本的安装,近期发教程
小弟虽然刚刚踏入职场,可是咱大学也不是打酱油过的啊,研究过各种版本系统的安装,也都均已经实践,勿喷,有问题 咱们可以相互探讨!
- debian repository的成长过程
1 基本概念 1.1 健康的安装 在端系统中的一次健康的安装指的是,在安装的包的集合中,所有的依赖都满足,并且没有冲突存在. 这的健康的安装是相对于端系统而言的,并不是相对于整个repo而言的.对整个 ...
- Calling a parent window function from an iframe
I want to call a parent window JavaScript function from an iframe. <script>function abc(){ ale ...
- Struts2自定义过滤器的小例子-入门篇
创建web项目 实现的效果! 用户点击页面不同的链接,后台调用不同的代码! 创建两个类实现共同的接口! public interface Action { String execute(); } ...
- php判断手机号码
//PHP判断手机号码 public function isMobile($params) { $pattern = "/^(13[0-9]|14[0-9]|15[0-9]|17[0-9 ...
- 网络编程-获取ip-解析ip
import java.net.InetAddress; import java.net.UnknownHostException; /** * 查询ip地址 * * @author chengluc ...