我真想吐槽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. Oracle 经典SQL 专为笔试准备

    相信把这99条sql搞定,Oracle基本笔试简直就像玩的一样(史上最全,最经典的入门级Oracle查询语句) 1. select * from emp; 2. select empno, ename ...

  2. 信号量多-threaded同步Semaphore

    Semaphore它是JDK1.5一个实现后,外面有个办法同步.Semaphore能够保持其当前的线程接入号码.并提供了一个同步机制. 采用Semaphore时,可以用相同的对资源的访问进行控制的线程 ...

  3. js取整

    综述 js中经常会遇到取整问题,所以做了下总结.总的来说分为两个方面,直接取整(不考虑小数点后的部分)还是计算后取整(例如四舍五入,向上取整等). 一.直接取整 1.parseInt(number) ...

  4. 密封关键字sealed

    在两种情况下使用: ·不想让别人继承:例如public sealed class Person{}; ·不想让子类重写自己的方法 例如: public class Person{ public vis ...

  5. 记一次排查log4net 不输出日志的解决过程

    最近发现log4net 不输出日志了,重点排查几个地方,发现都没有问题. 1.[assembly: log4net.Config.XmlConfigurator(ConfigFile = " ...

  6. Swift和OC 混编

    1.首先创建一个Swift工程 2.导入或者创建一个OC文件(.h和.m) 3.再创建一个桥连接文件 4.然后文件样子为 5.在桥接链接里面导入头文件 6.通过targets->->bui ...

  7. tomcat部署web项目的三种方式

    方式一:将web项目拷贝至webapps目录下. 方式二:修改tomcat目录下的conf目录下的server.xml,在其<Host>标签中添加子标签,代码如下: <Host ap ...

  8. JavaSE学习总结第22天_IO流4

    -  22.01  数据输入输出流的概述和讲解 操作基本数据类型 public class DataInputStreamextends FilterInputStream implements Da ...

  9. 删除缓存内容----unrecognized selector sent to instance

    这条错误主要还得看unrecognized前面,,我的时[NSNumber-length...]其实是变量类型错误.. 无法识别选择器发送实例,,本来意思就是你的controllerview找不到视图 ...

  10. Kettle之数据抽取、转换、装载

    Kettle 官网 ETL利器Kettle实战应用解析系列 利用kettle组件导入excel文件到数据库 kettle中实现动态SQL查询 java中调用kettle转换文件