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周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...
随机推荐
- 奥斯卡·王尔德十大经典语录
10不够真诚是危急的,太真诚则绝对是致命的.--摘自<身为艺术家的评论者> "A little sincerity is a dangerous thing, and a gre ...
- 【Java 虚拟机探索之路系列】:JIT编译器
作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell 为 ...
- 在CentOS上把PHP从5.4升级到5.5
在CentOS上把PHP从5.4升级到5.5 摘要:本文记录了在CentOS 6.3上,把PHP从5.4.8升级到5.5.13的过程. 1. 概述 在我做的一个项目中,最近我对生产服务器上的一系列系统 ...
- win系统下启动linux上的kafka集群及使用
一.首先在win系统下C:\Windows\System32\drivers\etc文件夹中hosts文件加入例如以下内容: 10.61.6.167 slaves1 10.61.6.168 slave ...
- python itertools
1 product 1.1 一个generator函数 因此它的返回值是一个iterator,可以用for遍历. 1.2 计算product的参数分类 1.2.1 dict和list 只用了dict的 ...
- select标签multiple属性的用法
前些日子公司让做一个功能模块.对于里面一个小功能费了些周折,现将其总结一下: 一.实现效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvaHVh ...
- 每天进步一点点—mysql-mysqldump
一. 简单介绍 mysqldump是client用来备份数据库或者在不通数据库之间进行数据迁移的工具,备份内容包括创建表或者装载表的SQL语句 二. 命令格式 备份单个数 ...
- commons-fileupload、smartUpload和commons-net-ftp
1.本地上传 在许多Web站点应用中都需要为用户提供通过浏览器上传文档资料的功能,例如,上传个人相片.共享资料等.在DRP中,就有这个一个功能,需要将对应的物料图片上传并显示.对于上传功能,其实在浏览 ...
- JS窗口
<SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.html', 'newwindow', 'heigh ...
- C# mouse keyboard monitor
/*********************************************************************************** * C# mouse keyb ...