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

  1. codeforces 28D(dp)

    D. Don't fear, DravDe is kind time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. CodeForces 28D Don&#39;t fear, DravDe is kind dp

    主题链接:点击打开链接 为了让球队后,删除是合法的.也就是说,对于每一个车辆, l+r+c 一样,按l+r+c分类. 然后dp一下. #include <cstdio> #include ...

  3. CF28D Don't fear, DravDe is kind 背包

    题目传送门:http://codeforces.com/problemset/problem/28/D 题意:给你$N$个物品,每个物品有其价格$P_i$,之前必须要买的物品价格和$L_i$,之后必须 ...

  4. 【神仙题】【CF28D】 Don't fear, DravDe is kind

    传送门 Description 一个有N辆卡车的车队从城市Z驶向城市3,来到了一条叫做"恐惧隧道"的隧道.在卡车司机中,有传言说怪物DravDe在那条隧道里搜寻司机.有些司机害怕先 ...

  5. CF28D Don't fear, DravDe is kind

    传送门 题意:\(n\)个位置,每个位置有价值\(v_i\)和重量\(p_i\),要选出一些位置,如果要选位置\(i\),那么前面选的重量之和要为\(l_i\),后面选的重量之和要为\(r_i\),求 ...

  6. Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)

    补一篇以前的扩展欧几里得的题,发现以前写错了竟然也过了,可能数据水??? 这个题还是很有意思的,和队友吵了两天,一边吵一边发现问题??? L. Knights without Fear and Rep ...

  7. Codeforces 15E Triangles - 组合数学

    Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby fores ...

  8. CodeForces 35D Animals

    G - Animals Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. Codeforces 15E Triangles 【组合计数】

    Codeforces 15E Triangles Last summer Peter was at his granny's in the country, when a wolf attacked ...

随机推荐

  1. JS格式化时间(支持小程序,兼容IOS)

    })-(\d{})-(\d{})T(\d{}):(\d{}):(\d{})/ /** * @function format time * @param val, format * @return {s ...

  2. CVPR2014 Objectness 源码转换(完整版) VS2012 X64 –>win32

    一.版本转换  1.将源码中vs2012 X64版本转换为vs2012 win32版本. 2.源码下载及其相关资料下载http://mmcheng.net/zh/bing/ 3.需要下载源码(Pape ...

  3. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  4. Ubuntu14.04 换源 阿里云

    sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup sudo vim /etc/apt/sources.list sudo apt-g ...

  5. linux下将jpg,jpeg格式转为PDF

    1.安装imagemagick(用其中的convert)和gthumb     sudo apt-get install imagemagick gthumb 2.将tiff图片转换为png或jpeg ...

  6. CentOS下SVN服务器的搭建使用

    转载自:http://ailurus.blog.51cto.com/4814469/1168481 SVN作为新一代代码版本管理工具,有很多优点,管理方便,逻辑明确,安全性高,代码一致性高.SVN数据 ...

  7. classList详解,让你的js方便地操作DOM类

    在此之前,jQuery的hasClass.addClass.removeClass我们已经再熟悉不过了,然而我们并不会在每一个项目中都会去使用 jQuery或者Zepto,譬如在移动端的网页中,考虑到 ...

  8. 转:SWT中的Display 对象和 Shell对象

    转自: http://blog.csdn.net/chulaixi/article/details/3095478 我们书写swt程序的步骤,这些步骤是: 1. 创建一个Display对象 2. 创建 ...

  9. go的websocket实现

    websocket分为握手和数据传输阶段,即进行了HTTP握手 + 双工的TCP连接 RFC协议文档在:http://tools.ietf.org/html/rfc6455 握手阶段 握手阶段就是普通 ...

  10. HDU1248 (完全背包简单变形)

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...