bzoj上访问负下标会跑到奇怪的地方……

其实可以滚动数组优化,但是我看能过就懒得改了

设f[i][j]为已经算了前i个挂饰,当前有j个空的钩子,转移就是f[i][j]=max(f[i-1][j],f[i-1][max(min(j-a[i]+1,n),-n)]+b[i])

注意到只有-n<=j<=n的范围内j是有用的,所以时空都是n^2的,负的j算的时候直接+n即可

#include<iostream>
#include<cstdio>
using namespace std;
const int N=4005;
int n,a[N],b[N],f[N][N],ans;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i],&b[i]);
for(int i=2;i<=n;i++)
f[0][i+n]=-2e9;
for(int i=1;i<=n;i++)
for(int j=-n;j<=n;j++)
f[i][j+n]=max(f[i-1][j+n],f[i-1][max(min(j-a[i]+1,n),-n)+n]+b[i]);
for(int i=0;i<=n;i++)
ans=max(ans,f[n][i+n]);
printf("%d\n",ans);
return 0;
}

bzoj 4247: 挂饰【dp】的更多相关文章

  1. BZOJ 4247 挂饰 背包DP

    4247: 挂饰 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id ...

  2. BZOJ 4247 挂饰(背包问题)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4247 [题目大意] JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将 ...

  3. BZOJ 4247 挂饰 01背包

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4247 JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机 ...

  4. BZOJ 4247: 挂饰 题解

    Description JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上. JOI君的挂饰有一些与众不同--其中的一些挂饰附有可以挂其他挂件的挂钩.每个挂件要么直 ...

  5. BZOJ 4247: 挂饰 动态规划

    按照挂件数量排序,然后做一个 DP 就好了. code: #include <bits/stdc++.h> #define ll long long #define N 2003 #def ...

  6. BZOJ 4247: 挂饰

    背包裸题 #include<cstdio> #include<algorithm> using namespace std; int F[2005]; struct node{ ...

  7. bzoj 4247挂饰

    背包????不会... #include<bits/stdc++.h> #define INF 0x7fffffff #define LL long long #define N 1000 ...

  8. [BZOJ4247]挂饰(DP)

    当最终挂饰集合确定了,一定是先挂挂钩多的在挂挂钩少的. 于是按挂钩从大到小排序,然后就是简单的01背包. #include<cstdio> #include<algorithm> ...

  9. bzoj4247挂饰——DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4247 就是01背包: 把挂钩数限制在n以内,因为不需要更多,而这会带来一些问题,就是有很多挂 ...

随机推荐

  1. Back弹出AlertDialog

    package com.pingyijinren.helloworld.activity; import android.content.DialogInterface; import android ...

  2. HDU1533 最小费用最大流

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. Codeforces Beta Round #57 (Div. 2) E. Enemy is weak

    求满足条件的三元组的个数,可以转换求一元组和二元组组成的满足条件的三元组的个数,且对于(x),(y,z),x > y,且x出现的p_x < p_y. x可直接枚举O(n),此时需要往后查询 ...

  4. Topcoder 658Div2

    补题风向标——>> 假装题意知道 A:暴力合成一遍了 n=s.size(); m=t.size(); ss+=s; tt+=t; if (ss==tt) or not; B:题意是给定 1 ...

  5. 寒武纪camp Day3

    补题进度:9/10 A(多项式) 题意: 在一个长度为n=262144的环上,一个人站在0点上,每一秒钟有$\frac{1}{2}$的概率待在原地不动,有$\frac{1}{4}$的概率向前走一步,有 ...

  6. com.sun.xxx.utils不存在问题的解决

    com.sun.org.apache.xml.internal.security.utils does not exist问题的解决 在网上找个很多的答案,但我的问题没有解决,睡一晚上后,被我误打误撞 ...

  7. spring mvc 选中多文件同时上传(利用input元素的multiple属性)

    原文:http://m.blog.csdn.net/article/details?id=51351388 <!DOCTYPE html> <html> <head> ...

  8. 简述HashMap和Hashtable的差别

    1.HashMap继承AbstractMap类. Hashtable继承了Dictionary类. 2.HashMap同意有null的键和值.       Hashtable不同意有null的键和值. ...

  9. Exchanger使用

    Exchanger使用

  10. JAVA_the user operation is waiting怎么办

    彻底解决 MyEclipse出现the user operation is waiting的问题   2011-05-31 10:32:30|  分类: 软件编程 |  标签:java  myecli ...