我真想吐槽USACO的数据弱..= = O(n^3)都能A....上面一个是O(n²), 一个是O(n^3)

O(n^3)做法, 先排序, dp(i, j) = max{ dp(j, p) } + w( i ) ( t <= p <= j ) 表示跳到第 i 个点, 上一个点是在 j 的最大得分, 其中t是满足条件的最小p.

我们在计算dp(i, j) (1 <= j <= i )时会发现, 随着 j 的递减, t也在不断地减小, 这样我们只要在dp过程中维护h(i, j)表示 max{ dp(i, x) } ( j <= x <= i ), 然后逆序枚举 j, 维护t即可. 时间复杂度O(n²)

----------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
  
#define rep(i, n) for(int i = 0; i < n; i++)
#define clr(x, c) memset(x, c, sizeof(x))
  
using namespace std;
 
const int maxn = 1009;
 
struct R {
int p, v;
inline void Read() {
scanf("%d%d", &p, &v);
}
} A[maxn];
 
bool cmpL(const R &a, const R &b) {
return a.p < b.p;
}
bool cmpR(const R &a, const R &b) {
return a.p > b.p;
}
 
int dp[maxn][maxn], h[maxn][maxn], n, ans = 0;
 
void work() {
clr(dp, 0), clr(h, 0);
h[0][0] = dp[0][0] = A[0].v;
for(int i = 1; i < n; ++i) {
int p = i - 1;
h[i][i] = dp[i][i] = A[i].v;
for(int j = i - 1; j >= 0; j--) {
while(p && (abs(A[i].p - A[j].p) >= abs(A[j].p - A[p - 1].p) || p > j)) p--;
if(abs(A[i].p - A[j].p) >= abs(A[j].p - A[p].p))
   dp[i][j] = max(h[j][p], dp[i][j]);
ans = max(ans, dp[i][j] += A[i].v);
h[i][j] = max(h[i][j + 1], dp[i][j]);
}
}
}
 
int main() {
freopen("test.in", "r", stdin);
cin >> n;
rep(i, n) A[i].Read();
sort(A, A + n, cmpL);
work();
sort(A, A + n, cmpR);
work();
cout << ans << "\n";
return 0;
}

-----------------------------------------------------------------------------

3315: [Usaco2013 Nov]Pogo-Cow

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 185  Solved: 100
[Submit][Status][Discuss]

Description

In an ill-conceived attempt to enhance the mobility of his prize cow Bessie, Farmer John has attached a pogo stick to each of Bessie's legs. Bessie can now hop around quickly throughout the farm, but she has not yet learned how to slow down. To help train Bessie to hop with greater control, Farmer John sets up a practice course for her along a straight one-dimensional path across his farm. At various distinct positions on the path, he places N targets on which Bessie should try to land (1 <= N <= 1000). Target i is located at position x(i), and is worth p(i) points if Bessie lands on it. Bessie starts at the location of any target of her choosing and is allowed to move in only one direction, hopping from target to target. Each hop must cover at least as much distance as the previous hop, and must land on a target. Bessie receives credit for every target she touches (including the initial target on which she starts). Please compute the maximum number of points she can obtain.

一个坐标轴有N个点,每跳到一个点会获得该点的分数,并只能朝同一个方向跳,但是每一次的跳跃的距离必须不小于前一次的跳跃距离,起始点任选,求能获得的最大分数。

Input

* Line 1: The integer N.

* Lines 2..1+N: Line i+1 contains x(i) and p(i), each an integer in the range 0..1,000,000.

Output

* Line 1: The maximum number of points Bessie can receive.

Sample Input

6
5 6
1 1
10 5
7 6
4 8
8 10

INPUT DETAILS: There are 6 targets. The first is at position x=5 and is worth 6 points, and so on.

Sample Output

25
OUTPUT DETAILS: Bessie hops from position x=4 (8 points) to position x=5 (6 points) to position x=7 (6 points) to position x=10 (5 points).

从坐标为4的点,跳到坐标为5的,再到坐标为7和,再到坐标为10的。

HINT

Source

BZOJ 3315: [Usaco2013 Nov]Pogo-Cow( dp )的更多相关文章

  1. 【BZOJ】3315: [Usaco2013 Nov]Pogo-Cow(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3315 果然自己太弱. 想不出dp方程啊.. 其实,以后记住...与上一个状态或下一个状态有关,,可以 ...

  2. BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )

    从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...

  3. bzoj 3312: [Usaco2013 Nov]No Change

    3312: [Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for his ...

  4. BZOJ 1640: [Usaco2007 Nov]Best Cow Line 队列变换

    Description FJ打算带着他可爱的N (1 ≤ N ≤ 2,000)头奶牛去参加"年度最佳老农"的比赛.在比赛中,每个农夫把他的奶牛排成一列,然后准备经过评委检验. 比赛 ...

  5. BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314 题意: N头牛在一个坐标轴上,每头牛有个高度.现给出一个距离值D. 如果某头牛在它的 ...

  6. BZOJ 1640 [Usaco2007 Nov]Best Cow Line 队列变换:贪心【字典序最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1640 题意: 给你一个长度为n的字符串. 你可以将原串的首字母或尾字母移动到新串的末尾. ...

  7. BZOJ3315: [Usaco2013 Nov]Pogo-Cow

    3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 79[Submit] ...

  8. Bzoj3315 [Usaco2013 Nov]Pogo-Cow(luogu3089)

    3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 352  Solved: 181[Submit ...

  9. bzoj1742[Usaco2005 nov]Grazing on the Run 边跑边吃草*&&bzoj3074[Usaco2013 Mar]The Cow Run*

    bzoj1742[Usaco2005 nov]Grazing on the Run 边跑边吃草 bzoj3074[Usaco2013 Mar]The Cow Run 题意: 数轴上有n棵草,牛初始在L ...

随机推荐

  1. GTW likes math(简单数学)

    GTW likes math  Accepts: 472  Submissions: 2140  Time Limit: 2000/1000 MS (Java/Others)  Memory Limi ...

  2. android下文件下载

    public static void downFile(final String url){ new Thread(){ public void run(){ FileOutputStream os= ...

  3. c语言编写经验逐步积累4

    寥寥数笔,记录我的C语言盲点笔记,仅仅为以前经历过,亦有误,可交流. 1.逻辑表达式的使用 取值 = 表达式 ? 表达式1:表达式2: 比方x = y > z ? y:z 2."+,- ...

  4. 80x86汇编小站站长简单介绍

    [人生格言] 1] 一生都用头脑而不是情绪解决这个问题 2] 仅仅有偏执狂才会成功 3] 在最困难时都要保持一份幽默感 4] 吾生也有涯,而知也无涯,以有涯随无涯,殆已 [简历] 我的生日: 1981 ...

  5. 深入理解java嵌套类和内部类

    一.什么是嵌套类及内部类 能够在一个类的内部定义还有一个类.这样的类称为嵌套类(nested classes),它有两种类型:静态嵌套类和非静态嵌套类.静态嵌套类使用非常少,最重要的是非静态嵌套类,也 ...

  6. String的几种比较方法对比(Compare,CompareTo, CompareOrdinal、Equals)

    String类字符串比较大概有4种方法:Compare(),CompareTo(), CompareOrdinal()和Equals(). Compare()方法是CompareTo()的静态版本.而 ...

  7. Centos6.4安装JDK

    链接地址:http://www.iteye.com/topic/1130311 1.先看看OpenJDK的安装包 $ rpm -qa |grep javatzdata-java-2013b-1.el6 ...

  8. PHPExcel 生成图形报表

    db.php为数据库操作类, $config为数据库配置,PHPExcel版本为PHPExcel_1.8.0,  PHP代码: $dir = dirname(__FILE__); require $d ...

  9. ASP.NET 导入excel 数据

    1,需要给上传文件的目录给予权限 2. <asp:FileUpload ID="FileUpload1" runat="server" /> < ...

  10. C语言循环小技巧

    写代码,有两类追求,一种是追求实用(Coder),一种是追求代码艺术(Artist) 我是那种追实用追腻了,偶然追一下艺术(就是偶然和艺术有一腿)的那种Coder 很多人,已经习惯了for(i=0; ...