洛谷题目传送门
目录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.
FJ给奶牛贝西的脚安装上了弹簧,使它可以在农场里快速地跳跃,但是它还没有学会如何降低速度。
FJ觉得让贝西在一条直线的一维线路上进行练习,他在不同的目标点放置了N (1 <= N <= 1000)个目标点,目标点i在目标点x(i),该点得分为p(i)。贝西开始时可以选择站在一个目标点上,只允许朝一个方向跳跃,从一目标点跳到另外一个目标点,每次跳跃的距离至少和上一次跳跃的距离相等,并且必须跳到一个目标点。
每跳到一个目标点,贝西可以拿到该点的得分,请计算他的最大可能得分。
* 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.
* Line 1: The maximum number of points Bessie can receive.
6 5 6 1 1 10 5 7 6 4 8 8 1025There are 6 targets. The first is at position x=5 and is worth 6 points, and so on.
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).
草场上有一条直线,直线上有若干个目标点。每个目标点都有一个分值和一个坐标。现在你可以选择其中任意一个目标点开始跳,只能沿一个方向跳,并且必须跳到另一个目标点。且每次跳的距离都不能少于上一次的距离。请问你能得到的最大分值是多少?、
考试时就是想到了这个做法,但是改了时限100ms过不了
设 \(f_{i , j}\) 表示只通过 \(j\) 步到达点 \(i\) 的最大分值。
\(j\) 会炸空间?
\(j\) 其实最多只有 \(n^2\) 种可能,乱搞一下就好了
那么:
\[f_{i , j} = Max_{k = 1}^{i - 1}(Max_{l = 0}^jf_{k , l} + p_i)\]用线段树维护一下 \(f_{k , l}\) 就好了
#include #define fu(x, y, z) for (int x = y; x <= z; x++)#define fd(x, y, z) for (int x = y; x >= z; x--)#define LL long longusing namespace std;const int N = 1005, M = 1000005;struct node { int x, p;} t[N];LL ans;int n, cnt, flg[M], p[M], p1[M], K = 1000005, len, dis[N][N];struct Tr { LL val; int lp, rp;} tr[M * 8];bool cmp(node x, node y) { return x.x < y.x; }void glp(int x) { tr[x].lp = ++len; }void grp(int x) { tr[x].rp = ++len; }void updata(int x) { tr[x].val = max(tr[tr[x].lp].val, tr[tr[x].rp].val); }void change(int x, int l, int r, int pos, LL v) { if (l == r) tr[x].val = max(tr[x].val, v); else { int mid = l + r >> 1; if (pos <= mid) { if (!tr[x].lp) glp(x); change(tr[x].lp, l, mid, pos, v); } else { if (!tr[x].rp) grp(x); change(tr[x].rp, mid + 1, r, pos, v); } updata(x); }}int query(int x, int l, int r, int pos) { if (r <= pos) return tr[x].val; else { int mid = l + r >> 1, max1 = 0, max2 = 0; if (tr[x].lp) max1 = query(tr[x].lp, l, mid, pos); if (mid < pos && tr[x].rp) max2 = query(tr[x].rp, mid + 1, r, pos); return max(max1, max2); }}void clear(int x) { if (tr[x].lp) clear(tr[x].lp); if (tr[x].rp) clear(tr[x].rp); tr[x].val = tr[x].lp = tr[x].rp = 0;}void fans() { fu(i, 1, n) ans = max(ans, tr[i].val); }int main() { scanf("%d", &n); fu(i, 1, n) scanf("%d%d", &t[i].x, &t[i].p); sort(t + 1, t + n + 1, cmp); fu(i, 1, n) fu(j, 1, n) dis[i][j] = abs(t[i].x - t[j].x); fu(i, 1, n) fu(j, 1, n) { if (!flg[dis[i][j]]) { flg[dis[i][j]] = 1; p[++cnt] = dis[i][j]; K = max(K, dis[i][j]); } } sort(p + 1, p + cnt + 1); fu(i, 1, cnt) p1[p[i]] = i; len = n; fu(i, 1, n) { ans = max(ans, 1ll * t[i].p); change(i, 0, K, p1[0], 1ll * t[i].p); } int k, k2; fu(i, 1, n) { fu(j, 1, i - 1) { k = query(j, 0, K, p1[dis[i][j]]); k2 = query(i, 0, K, p1[dis[i][j]]); if (k + t[i].p > k2) change(i, 0, K, p1[dis[i][j]], k + t[i].p); } } fans(); fu(i, 1, n) clear(i); len = n; fu(i, 1, n) change(i, 0, K, p1[0], t[i].p); fd(i, n, 1) { fd(j, n, i + 1) { k = query(j, 0, K, p1[dis[i][j]]); k2 = query(i, 0, K, p1[dis[i][j]]); if (k + t[i].p > k2) change(i, 0, K, p1[dis[i][j]], k + t[i].p); } } fans(); printf("%lld", ans);} 然后因为查找函数没有加等号 , 就寄掉8分。
差点就AK了
设 \(f_{i , j}\) 表示从 \(j\) 到 \(i\) 的最大分值 , 用单调队列维护
好像宏定义会慢一点?
#include #define LL long long#define fu(x , y , z) for(int x = y ; x <= z ; x ++)using namespace std;const int N = 2005;struct node { int x , p;} t[N];int n , k;LL ans , f[N][N];inline bool cmp (node x , node y) { return x.x < y.x; }inline bool cmp1 (node x , node y) { return x . x > y.x; }inline void fans (int flg) { memset (f , -0x3f , sizeof (f)); if (flg == 1) sort (t + 1 , t + n + 1 , cmp); else sort (t + 1 , t + n + 1 , cmp1); for (int j = 1 ; j <= n ; j ++) { f[j][j] = t[j].p; for (int i = j + 1 , k = j + 1 ; i <= n ; i ++) { f[i][j] = f[i - 1][j] - t[i - 1].p; while (k > 1 && (t[j].x - t[k - 1].x) * flg <= (t[i].x - t[j].x )* flg) f[i][j] = max (f[i][j] , f[j][--k]); f[i][j] += t[i].p; ans = max (ans , f[i][j]); } ans = max (ans , f[j][j]); }}int main () { scanf ("%d" , &n); fu (i , 1 , n) scanf ("%d%d" , &t[i].x , &t[i].p); sort (t + 1 , t + n + 1 , cmp); fans (1); fans (-1); printf ("%lld" , ans); return 0;}
上一篇:周末猪价稳中震荡(一周猪价预测)
下一篇:最后一页
P3089[USACO13NOV]Pogo-CowS弹簧踩高跷[洛谷题目传送门](https: www
windows10无法自动检测到网络代理设置怎么办当用户遇到此问题的时候不
【地评线】东湖评论:发展特色产业助力农民增收---发展特色产业,促进
四川新闻网-首屏新闻成都7月6日讯(记者戴璐岭余开洋)今日四川达万高
作为广东省深圳市爱阅公益基金会的创始人和理事长,李文致力于推动儿童
【7月钢铁行业:宏观预期偏暖钢材市场或震荡回暖】回顾6月份钢材市场,
据海峡都市报报道,价值十多万的珠宝,被家人当做垃圾丢了,是种什么样
7月4日(当地法国巴黎时间),仰韶彩陶坊太阳酒荣获联合国教科文组织
紫金财经7月6日消息长城汽车7月5日发布2023年6月产销数据。
1近年来,随着国家大规模的棚改拆迁结束,为了改善国民的生活质量,许
红网时刻娄底7月4日讯(通讯员吴文婷)近日,娄底市中心医院成功为一名
你需要的东西重级绘图纸HB铅笔统治者黑色彩色铅笔浅黄色彩色铅笔深
股市早8点丨金叉不是一天建成的,个股,沪深,a股,美股,股市,金叉,净卖出,
今天来聊聊桑葚干的功效与作用及食用禁忌,黑桑葚干的功效与作用及禁忌
精神分裂准确的诊断应该是精神分裂症,这是精神科比较严重的一种病,也
豆来为大家解答以上的问题。龙背上的破鞋零,龙背上的破鞋这个很多人还
今天是2023年07月05日星期三。北京天气的关键词是“晴”。今天白天北京
金投网提供2023版30克熊猫银币现在市场价是多少(2023年07月05日),(
碧绿茶园,金色麦田,夏果饱满,风过荷塘……各地夏收夏种夏管如火如荼
英国《银行家》杂志(TheBanker)7月5日发布世界银行1000强榜单。榜单
“南京地铁祝福毕业生毕业快乐”“愿大家星光熠熠,闪耀远方”前阵子放
原标题:吉林镇赉县总推进“县级工会加强年”专项工作(引题)系列举措
本报深圳7月4日电(记者吕绍刚)2023深圳·中山联合招商大会日前举行,这
对于武汉轻轨最早几点开车这个问题感兴趣的朋友应该很多,这个也是目前
平台回应外卖门店照片造假:可举证投诉店家,近日,许多博主爆料称,有
X 关闭
X 关闭