P4890 Never·island
考虑把总区间长度减去最多能减少的区间长度
把所有区间离散化,对每一小段计算贡献
分类讨论一波,对于边界 $i,i+1$ ,设它们之间距离 $d$,$i$ 属于 $x$ 考察队的边界,$i+1$ 属于 $y$ 考察队的边界:
$i$ 为左边界,$i+1$ 为左边界,显然如果给 $x$ 钥匙就可以让这一段时间关门,给 $x$ 的贡献 $val[x]$ 加上 $d$ ,表示给 $x$ 钥匙得到的贡献
$i$ 为左边界,$i+1$ 为右边界,如果 $x=y$ ,显然给 $x$ 钥匙就可以让这段时间关门,那么给 $val[x]$ 加 $d$
如果不为同一个考察队,那么如果要得到这一段的价值就必须同时给两个考察队钥匙,就从 $x$ 往 $y$ 连一条边权为 $d$ 的单向边,表示 $x,y$ 都有钥匙得到的贡献
$i$ 为右边界,$i+1$ 为左边界,那么这一段显然关门就好,直接给答案加上 $d$
$i$ 为右边界,$i+1$ 为右边界,那么给 $y$ 钥匙就可以让这段时间关门,所以 $val[y]+=d$
(注意此时不用考虑 $x$ 能不能进来,$x$ 在之前就已经考虑过了)
发现这的图是由几条链组成的,所以可以对每条链分别 $dp$
设 $f[i][j][1/0]$ 表示前 $i$ 个点,给了 $j$ 个点钥匙,当前点 $i$ 选$/$不选的最大代价
具体实现起来还有点难度,看代码吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=5e3+,INF=1e9+;
int n,K,tot,ans;
int pos[N],val[N],eval[N],nex[N];//eval是边的价值,nex存边
map <int,int> mp;//维护某个位置的编号
int id[N],f[][][];//id是新的编号(同一条链的编号连续),f是dp数组
bool vis[N];//是否被重新编号过
void dfs(int x) { vis[ id[++tot]=x ]=; if(nex[x]) dfs(nex[x]); }//给一条链的每个节点编号
int main()
{
n=read(),K=read();
int l,r;
for(int i=;i<=n;i++)
{
l=read(),r=read();
mp[l]=i<<; mp[r]=i<<|;//编号
pos[i]=l; pos[n+i]=r;//存位置,注意位置都是左闭右开的!
}
sort(pos+,pos+(n<<)+);//排序
int res=;
for(int i=;i<(n<<);i++)//对每一小段分类讨论
{
l=pos[i],r=pos[i+];
int ld=mp[l],rd=mp[r];
if( !(ld&) )//i为左边界
{
if( !(rd&) ) val[ld>>]+=r-l;//i+1为左边界
else//i+1为右边界
{
if( (ld|)==rd ) val[ld>>]+=r-l;//如果属于同一点
else nex[ld>>]=(rd>>),eval[ld>>]=r-l;
}
}
else//i为右边界
{
if( !(rd&) ) res+=r-l;//i+1为左边界
else val[rd>>]+=r-l;//i+1为右边界
}
}
for(int i=n<<;i;i--)//注意从后往前找,要让链的节点编号连续
{
int d=mp[pos[i]];
if( !(d&) && !vis[d>>] ) dfs(d>>);
}
memset(f,~0x3f,sizeof(f));//记得初始化
f[tot+][][]=;
for(int i=tot;i;i--)
{
f[i][][]=;
for(int j=;j<=min(K,tot-i+);j++)
{
f[i][j][]=max(f[i+][j][],f[i+][j][]);
f[i][j][]=max(f[i+][j-][], f[i+][j-][]+eval[ id[i] ] )+val[ id[i] ];
}
}
printf("%d", pos[n<<]-pos[]-res-max(f[][K][],f[][K][]) );
return ;
}
P4890 Never·island的更多相关文章
- P4890 Never·island(dp)
P4890 Never·island 求门开的最小时间,其实也就是求门关的最大时间. 坐标这么大....显然坐标要离散化 离散化排序后,我们发现x轴被这些点划分成若干条线段$(l,r)$,并且有4种情 ...
- [LeetCode] Island Perimeter 岛屿周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 463. Island Perimeter
https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...
- Leetcode-463 Island Perimeter
#463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...
- LeetCode Island Perimeter
原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...
- sicily1024 Magic Island(图的遍历)
Description There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. ...
- codeforce B Island Puzzle
B. Island Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- cf.301.D. Bad Luck Island(dp + probabilities)
D. Bad Luck Island time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [CareerCup] 6.4 Blue Eyes People on Island 岛上的蓝眼人
6.4 A bunch of people are living on an island, when a visitor comes with a strange order: all blue-e ...
随机推荐
- PHP 5.5环境配置
php5.5 + apache2.4 安装配置 1 2 3 4 5 6 7 分步阅读 php5.5 做了大量的更新,在与apache搭配的时候如何选择也很有讲究,这里我们以64位 php5.6 和 A ...
- 基于 EntityFramework 的数据库主从读写分离架构(2)- 改进配置和添加事务支持
回到目录,完整代码请查看(https://github.com/cjw0511/NDF.Infrastructure)中的目录: src\ NDF.Data.EntityFramew ...
- 获取HTML元素位置--js学习笔记
对于不同的元素,不同的浏览器,offsetParent含义不同,有时,指的是直接包含的元素,有时指的是HTML元素,有时不存在offsetParent. 如果所研究的元素没有offsetParent, ...
- 31.SUM() 函数
SUM() 函数 SUM 函数返回数值列的总数(总额). SQL SUM() 语法 SELECT SUM(column_name) FROM table_name SQL SUM() 实例 我们拥有下 ...
- scala _ parameter
Given that sequence, use reduceLeft to determine different properties about the collection. The foll ...
- SQL 批量插入有标识列的数据
代码: SET IDENTITY_INSERT 表名 ON SET IDENTITY_INSERT 表名 OFF
- ESP8266-iot-简介1
ESP8266简介
- hdu 4277 USACO ORZ (Dfs)
题意: 给你n个数,要你用光所有数字组成一个三角形,问能组成多少种不同的三角形 时间分析: 3^15左右 #include<stdio.h> #include<set> usi ...
- delphi计算两个时间差
uses DateUtils; var S1, S2: string; T1, T2: TDateTime; D, H, M, S: Integer; Value: Int64; begin S1 : ...
- centos7 .net core 使用supervisor守护进程,可以后台运行
1.安装supervisor yum install supervisor 2.配置supervisor vi /etc/supervisord.conf 拉到最后,这里的意思是 /etc/super ...