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. form怎样正确post文件

    form在HTML中,是用于收集用户输入的,基本全部浏览器都支持form.给form加入method属性.就能实现将用户在form内控件输入的信息POST到制定地址.或发送GET请求. 写了以下一段代 ...

  2. fixedBox固定div漂浮代码 支持ie6以上大部分浏览器

    fixedBox固定div漂浮代码 支持ie6以上大部分浏览器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  3. ganglia监控自己定义metric实践

    Ganglia监控系统是UC Berkeley开源的一个项目,设计初衷就是要做好分布式集群的监控.监控层面包含资源层面和业务层面,资源层面包含cpu.memory.disk.IO.网络负载等,至于业务 ...

  4. Eclipse项目遇到问题汇总

    1:gc overhead limit exceeded     原因:这是由于项目中eclipse内存分配不足导致     修改:修改eclipse.ini文件     修改如下:          ...

  5. JNI/NDK开发指南(2)

    1.生成动态库.so,存放于手机的system/lib/中(APP怎样将.so存入该文件夹,奇怪?????),Java层调用JNI的类会运行静态代码System.loadLibrary("* ...

  6. iOS SHA加密算法的实现

    - (NSString *)SHAStringWithSourceData:(NSData *)data type:(SHAType)type{ int shaDigestLength; switch ...

  7. 一些linux嵌入式资源下载地址

    linux内核源代码情景分析 非扫描版 上下册合订版 字清楚 带书签 1575页 pdfhttp://download.csdn.net/source/2002579***************** ...

  8. string operation in powershell

    https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/15/keep-your-hands-clean-use-powershell- ...

  9. linux下开机启动svn配置

    1.在 vi /etc/rc.local文件下添加以下: /home/svn/subversion-1.8.18/bin/svnserve -d --listen-port 3690 -r /home ...

  10. iphone设备尺寸规格

    1.以下是iphone各种设备的尺寸规格 2.开发时只需要按“逻辑分辨率”来,1x,2x,3x主要用于切图时按不同大小来切图,如1x的图就是按照“逻辑分辨率”大小的效果图切出来的原图,2x就是1x原图 ...