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的更多相关文章

  1. 51 Nod 1475 建设国家 (优先队列+贪心)

    1475 建设国家  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城 ...

  2. 51nod 1475:建设国家 优先队列的好题

    1475 建设国家 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城市 ...

  3. 51nod建设国家

    小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城市. 现在小C想把国家建造成这样的形状:选若干(可以是0个)的中间站把他们连成一条直线,然后把首都连在这一条直线的左端.然 ...

  4. 【BZOJ】1096: [ZJOI2007]仓库建设(dp+斜率优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1096 首先得到dp方程(我竟然自己都每推出了QAQ)$$d[i]=min\{d[j]+cost(j+ ...

  5. BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)

    [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在 ...

  6. poj - 1170 - Shopping Offers(减少国家dp)

    意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 & ...

  7. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  8. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  9. HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP

    意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...

随机推荐

  1. HDU 3080 The plan of city rebuild(prim和kruskal)

    The plan of city rebuild Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  2. css中使input输入框与img(图片)在同一行居中对齐

    input,img{vertical-align:middle;},同时设置input和img的vertical-align属性,兼容ie7

  3. RGB中的颜色的设置

    用来表示一个 RGB 颜色值. 语法 RGB(red, green, blue) RGB 函数的语法含有以下这些命名参数: 部分 描述 red 必要参数:Variant (Integer).数值范围从 ...

  4. 用CSS画小猪佩奇,你就是下一个社会人! js将“I am a coder”反转成 “coder a am I”,不许用split,join,subString,reverse;求解方法三

    用CSS画小猪佩奇,你就是下一个社会人!   欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作者:江志耿 | 腾讯TEG网络工程师 我是佩奇,哼,这是我的弟弟乔治,呱呱,这是我的妈妈,嚯 ...

  5. 积跬步,聚小流------Bootstrap学习记录(3)

    响应式作为Bootstrap的一大特色.栅格系统可谓是功不可没,既然如此,那我们就来看一下栅格系统是怎样帮助bootstrap实现响应式布局的呢? 1.什么是栅格系统 我们能够从Bootstrap的官 ...

  6. 理解Paxos Made Practical

    Paxos Made Practical 当一个组中一台机器提出一个值时,其它成员机器通过PAXOS算法在这个值上达成一致. Paxos分三个阶段. 第一阶段: 提出者会选出一个提议编号n(n> ...

  7. 3 微信开发本地代理环境的搭建--实现将内网ip映射到外网

    微信公众号的开发,要搭建网站,并且随时都有可能修改网站内容进行调试,这就需要临时外网能返回本地开发环境搭建的项目进行测试,即内网映射到公网,但是好多开发者没有自己的域名和服务器,这里我们先来搭建一个本 ...

  8. java Http post请求发送json字符串

    最近差点被业务逻辑搞懵逼,果然要先花时间思考,确定好流程再执行.目前最好用的jar包还是org.apache.http. public class HttpClientHelper { private ...

  9. Mac Mysql [ERR] 2006 - MySQL server has gone away

    Mac mysql 安装后,导入sql数据,出现这个错误: 处理方式,是因为sql文件太大,需要修改mysql的配置.如果没有my.cnf就自己建一个. cd /etc sudo vim my.cnf ...

  10. 【BZOJ 2456】 mode

    [题目链接] 点击打开链接 [算法] 此题初看是大水题,只需调用std :: sort即可 但是,n最大500000,显然会超时 而且,内存限制1MB,我们连数组也开不了! 那怎么做呢 ? 我们发现, ...