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周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...
随机推荐
- mina客户端与服务端通信的易错点
使用mina进行项目开发时,如果客户端与服务端不在同一个项目下,需要关注一下两点: 第一.服务端与客户端的编码解码器一致 第二.过程中所用到的实体类的包名需要一致
- Python不同功能的函数
•函数作为参数 import math def add(x,y,f): return f(x) + f(y)print add(25,36,math.sqrt) •map()函数 map()是 Pyt ...
- linux系统编程:线程同步-相互排斥量(mutex)
线程同步-相互排斥量(mutex) 线程同步 多个线程同一时候訪问共享数据时可能会冲突,于是须要实现线程同步. 一个线程冲突的演示样例 #include <stdio.h> #includ ...
- Pthon的定时任务APScheduler的启动与关闭
Pthon的定时任务APScheduler的启动与关闭 安装: sudo pip install apscheduler 使用: 直接运行Python文件即可,如 python XXX.py,XXX. ...
- 猫猫学iOS 之第一次打开Xcode_git配置,git简单学习
猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:错误 当第一次打开Xcode我们进行commit操作的时候会 ...
- 使用scanf_s报错:0xC0000005: Access violation writing location 0x00000000
在vs2010中写了一行scanf("%s",name); 调式时 提示warning , 提示修改为scanf()使用可能会存在不安全,建议使用scanf_s() 但是我修改成s ...
- sphinx是支持结果聚类的——WHERE、ORDER BY和GROUP BY
原生API提供的匹配筛选.排序和分组配置和SQL语法提供的WHERE.ORDER BY和GROUP BY语句的效果是一样的,你可以对匹配结果进行你需要的筛选.排序和分组匹配.例如,如果你要搜索MySQ ...
- 【184】FileZilla 搭建 FTP 及访问
参考:FileZilla 下载中心 参考:使用FileZilla Server轻松搭建个人FTP服务器 建好后,Windows 访问:Windows徽标键+R打开运行窗口,输入ftp://*** ,* ...
- vs2008添加消息函数方法
开发MFC时,开发工具VS2008不能像开发工具VC++6.0那样,直接在类文件上右击选择“Add Window Message Handles”来添加消息映射.对于我这个初学者,刚开始一直没找到可以 ...
- Ubuntu12.04中新的快捷键(转载)
转自:http://blog.51osos.com/linuxnews/ubuntu12-04%E4%B8%AD%E6%96%B0%E7%9A%84%E5%BF%AB%E6%8D%B7%E9%94%A ...