[Codeforces 28D] Do not fear,DravDe is kind
Brief Intro:
对于四元组(v,c,l,r),求其子序列中v最大的和,并使其满足:
1、Ci+Li+Ri相同
2、L1=0,Rn=0
3、Li=Sigma(C1...Ci-1)
Solution:
算是有条件约束的DP吧
设dp[k]为选到k且选k的最大值
对于每个条件,我们这样处理:
1、将所有数据按Ci+Li+Ri的和进行分组处理
2、当Li=0时,将其视为起点;当Ri=0时,用dp[i]去更新res
3、此条件可转换为:仅当Lp=Lq+Cq时,dp[p]可由dp[q]转移而来
于是我们将数组按SUM排序后,用Best_val[t]记录Lk+Ck=t时最大的dp[k],用Best_id[t]记录Lk+Ck=t时最大的k,这样每次dp[k]由Best_val[Lk]更新,再由Best_id[Lk]更新父亲节点即可
#include <bits/stdc++.h> using namespace std; inline int read()
{
char ch;int num,f=;
while(!isdigit(ch=getchar())) f|=(ch=='-');
num=ch-'';
while(isdigit(ch=getchar())) num=num*+ch-'';
return f?-num:num;
} template<class T> inline void putnum(T x)
{
if(x<)putchar('-'),x=-x;
register short a[]={},sz=;
while(x)a[sz++]=x%,x/=;
if(sz==)putchar('');
for(int i=sz-;i>=;i--)putchar(''+a[i]);
putchar(' ');
} const int MAXN=1e5+;
int n;
struct truck
{
int v,c,l,r,num;
}dat[MAXN];
int pre[MAXN],dp[MAXN],b_val[MAXN],b_id[MAXN],T[MAXN],start=-,res=; bool cmp(truck x,truck y)
{
if(x.c+x.l+x.r==y.c+y.l+y.r) return x.num<y.num;
return x.c+x.l+x.r<y.c+y.l+y.r;
} int main()
{
n=read();
for(int i=;i<=n;i++)
dat[i].v=read(),dat[i].c=read(),dat[i].l=read(),dat[i].r=read(),dat[i].num=i; sort(dat+,dat+n+,cmp); for(int i=,j=;i<=n;i=j)
{
while(j<=n && dat[i].c+dat[i].l+dat[i].r==dat[j].c+dat[j].l+dat[j].r) j++;
for(int k=i;k<j;k++)
{
if(!dat[k].l) dp[k]=dat[k].v,pre[k]=-;
else if(T[dat[k].l]==i)
{
dp[k]=b_val[dat[k].l]+dat[k].v;
pre[k]=b_id[dat[k].l];
} int t=dat[k].l+dat[k].c;
if(T[t]!=i || dp[k]>b_val[t])
{
b_val[t]=dp[k];T[t]=i;b_id[t]=k;
} if(!dat[k].r && dp[k]>res) res=dp[k],start=k;
}
} vector<int> res;
while(start!=-) res.push_back(dat[start].num),start=pre[start];
putnum(res.size());puts(""); if(!res.size()) return ;
for(int i=res.size()-;i>=;i--) putnum(res[i]);
return ;
}
Review:
1、 注意子序列是不能改变相对次序的,因此排序时在SUM相同时要按原ID作为关键字排序(保证稳定性)
2、 对vector.size()少进行减法运算,防止unsigned int溢出
3、 对于具有决策单调性的DP,只要记录当前转移态前最优的状态即可
[Codeforces 28D] Do not fear,DravDe is kind的更多相关文章
- codeforces 28D(dp)
D. Don't fear, DravDe is kind time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- CodeForces 28D Don't fear, DravDe is kind dp
主题链接:点击打开链接 为了让球队后,删除是合法的.也就是说,对于每一个车辆, l+r+c 一样,按l+r+c分类. 然后dp一下. #include <cstdio> #include ...
- CF28D Don't fear, DravDe is kind 背包
题目传送门:http://codeforces.com/problemset/problem/28/D 题意:给你$N$个物品,每个物品有其价格$P_i$,之前必须要买的物品价格和$L_i$,之后必须 ...
- 【神仙题】【CF28D】 Don't fear, DravDe is kind
传送门 Description 一个有N辆卡车的车队从城市Z驶向城市3,来到了一条叫做"恐惧隧道"的隧道.在卡车司机中,有传言说怪物DravDe在那条隧道里搜寻司机.有些司机害怕先 ...
- CF28D Don't fear, DravDe is kind
传送门 题意:\(n\)个位置,每个位置有价值\(v_i\)和重量\(p_i\),要选出一些位置,如果要选位置\(i\),那么前面选的重量之和要为\(l_i\),后面选的重量之和要为\(r_i\),求 ...
- Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)
补一篇以前的扩展欧几里得的题,发现以前写错了竟然也过了,可能数据水??? 这个题还是很有意思的,和队友吵了两天,一边吵一边发现问题??? L. Knights without Fear and Rep ...
- Codeforces 15E Triangles - 组合数学
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby fores ...
- CodeForces 35D Animals
G - Animals Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- Codeforces 15E Triangles 【组合计数】
Codeforces 15E Triangles Last summer Peter was at his granny's in the country, when a wolf attacked ...
随机推荐
- noip 2011观光公交
P1315 观光公交 95通过 244提交 题目提供者该用户不存在 标签贪心递推2011NOIp提高组 难度提高+/省选- 提交该题 讨论 题解 记录 题目描述 风景迷人的小城Y 市,拥有n 个美 ...
- System l类arraycopy的用法
package org.springframework; /** * @author 秦林森 */ public class Test { public static void main(String ...
- Eclipse Jetty调试时无法保存js文件
Jetty会使用内存映射文件来缓存静态文件,包括js,css文件. 在Windows下,使用内存映射文件会导致文件被锁定,所以当Jetty启动的时候无法在编辑器对js或者css文件进行编辑. 解决办法 ...
- spring和Quartz的定时功能
一:前沿 最近在做一个定时的功能,就是在一定时间内查询订单,然后告诉用户未付款,已付款等消息通知,而且要做集群的功能,这个集群的功能是指,我部署两套代码,其中一个定时的功能在运行,另外一个就不要运行. ...
- centos7下yum快速安装 mariadb(mysql)
从最新版本的centos系统开始,默认的是 Mariadb而不是mysql! 使用系统自带的repos安装很简单: yum install mariadb mariadb-server systemc ...
- Quartus ModelSim联合仿真中的RAM初始化
Modelsim只支持Hex格式的初始化文件,文件需要放在仿真的根目录下,例如:.\simulation\modelsim:并且在利用Quartus宏生成IP时,选择的初始化文件必须用绝对路径!否则M ...
- bzoj3779: 重组病毒 link-cut-tree
题目传送门 这道题看了做了个神转换.....推荐个博客给各位大爷看看吧神犇传送门 代码敲了半天....题目也读了半天 线段树维护的东西很容易和lct混在一起 调了调能过也是很开心啊 运气比较好吧233 ...
- java List排序 顺序 倒序 随机
List list = new LinkedList(); for ( int i = 0 ; i < 9 ; i ++ ) { list.add( " a " + i); ...
- AtCoder Regular Contest 092 C D E F
C - 2D Plane 2N Points 题意 二维平面上有\(N\)个红点,\(N\)个蓝点,一个红点和一个蓝点能配成一对当且仅当\(x_r<x_b\)且\(y_r<y_b\). 问 ...
- cobalt strike使用笔记
启动: ./teamserver 192.168.74.1 admin #启动cs服务器.admin为密码. 监听器: windows/beacon_dns/reverse_dns_txtwindow ...