当前位置: 首页 > news >正文

abc435_f

abc435_f

不理解为什么都不会,其实挺简单的。

思路

首先,猫一开始在最高点,考虑我下一步有意义的操作只有撤掉最高的塔(若撤掉别的塔,对猫没有任何影响,只会减小答案)。

考虑撤掉 \([l,r]\) 最高塔(位置\(P\))后的两种情况

  • 猫跳到左边最高塔(位置\(p_l\)),这步跳跃对答案贡献 \(del_l=(P-p_l)\)
  • 猫跳到右边最高塔(位置\(p_r\)),这步跳跃对答案贡献 \(del_r=(p_r-P)\)

那么此时的答案 \(solve(l,r)\) 就是 \(\max(solve(l,P)+del_l, solve(P,r)+del_r)\)\(solve(1,n)\) 开始递归即可。

那么我们只需要快速处理任意一段区间的最大值即可,考虑倍增维护,于是复杂度为预处理 \(O(n\log(n))\) ,计算 \(O(n)\) 显然正确。

Code

// By wnn
#include<bits/stdc++.h>
// simple name
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pque priority_queue
#define x1 x_1
#define y1 y_1
#define fir first
#define sec second
#define pb push_back
#define myfreopen freopen(".in", "r", stdin),freopen(".out", "w", stdout)
// function
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define mid(l, r) ((l + r) >> 1)
#define debug(x) cerr << x << endl
#define dist(x, y, x2, y2) sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2))
#define WA cerr << "Wrong Answer" << endl
#define init_inf32(x) memset(x, 0x3f, sizeof(x))
#define init_inf64(x) memset(x, 0x3fll, sizeof(x))
#define init_0(x) memset(x, 0, sizeof(x))
#define Dec(x) fixed << setprecision(x)
// val
#define eps 1e-9
#define inf32 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3fll
#define mod1 (int)(1e9 + 7)
#define mod2 998244353
#define PI acos(-1.0)
// god
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// Def important function
inline void init();
inline void ever_init();
inline void solve();
// Init rnd()
mt19937 rnd(time(0) ^ clock());
// Constants
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int N = 2e5 + 5;int n, p[N], pos[N];
int f[N][20];
inline void init1(){for(int i = 1; i <= n; i++){f[i][0] = p[i];}for(int j = 1; (1 << j) <= n; j++)for(int i = 1; i + (1 << j) - 1 <= n; i++){f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);}
}
inline int cal(int l, int r){int k = (log2(r - l + 1));return max(f[l][k], f[r - (1 << k) + 1][k]);
}
inline int dfs(int l, int r){if(l >= r) return 0;int maxn = cal(l, r), mp = pos[maxn];int len1 = mp - pos[cal(l, mp - 1)], len2 = pos[cal(mp + 1, r)] - mp;return max(dfs(l, mp - 1) + len1, dfs(mp + 1, r) + len2);
}inline void init(){cin >> n;for(int i = 1; i <= n; pos[p[i]] = i, i++) cin >> p[i];init1();cout << dfs(1, n);
}
inline void ever_init(){}
inline void solve(){}signed main(){
//    myfreopen;ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);init();int T = 1;
//    cin >> T;while(T--){ever_init();solve();}return 0;
}
/*things to check:
* Will it MLE?
* Is array big enough?
* Do you need long long?
* Is inf big enough?
* max or min?
* Yes,No or YES,NO?
* Is there anything extra to output?
* Did you Countershoot?
* Have you measured the limit data?
* More measurements should be cleared!!!
*/
http://www.rkmt.cn/news/74991.html

相关文章:

  • 记CACC 2025区域赛
  • Ubuntu下,MySQL查询报错sql_mode=only_full_group_by
  • 深入解析:Chrome插件:实现Axure RP HTML原型的便捷预览
  • 老板嫌工期资源投入太多,怎么回答
  • 方差的迭代计算公式 - 指南
  • K8S中Ingress的采用
  • 进程监控:通过 SSH 远程监测嵌入式设备进程重启
  • 【ZeroRange WebRTC】对称加密 vs 非对称加密(从原理到实践) - 详解
  • 2025.12.6日21:24-incapacity无能力
  • 百度统计、Google Analytics平替开源网站分析工具:Umami - 教程
  • 舆情处置高效的技术深度解析:Infoseek 字节探索的 AI 闭环架构与实现逻辑
  • FPS的实时处理能力
  • 数字马力一面-后端开发郑州岗(校招)
  • 详细介绍:中颖AFE芯片:SH367303、SH367306 和 SH367309
  • 主动学习如何优化计算机视觉工作流程
  • 英语_阅读_Heroes come in all ages_待读
  • 收敛至约0.28
  • Scoop 软件清单与配置信息
  • 我不玩了
  • 英语_错题集_2512
  • 深度学习第一周
  • 课后作业10
  • 装饰器模式
  • 2025年靠谱的轮胎品牌哪家好?口碑好的轮胎品牌哪家好?官方精选可靠品牌指南
  • 权重衰减
  • 2025年中国前五轮胎品牌:权威TOP10轮胎榜单发布
  • 读大话数据结构的总结1
  • 作业4
  • 2026年网络安全展望:AI加速、攻击面扩张与专业化红队的未来
  • 接入Impala、Hive 的报表、BI、数据中台的国内厂商评价及接口框架