当前位置: 首页 > 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.jsqmd.com/news/64437/

相关文章:

  • Ubuntu下,MySQL修改端口号
  • 记CACC 2025区域赛
  • K8S中Ingress的采用
  • 深入解析:Chrome插件:实现Axure RP HTML原型的便捷预览
  • Ubuntu下,MySQL查询报错sql_mode=only_full_group_by
  • CRNN
  • 2025年广东地区湘菜供应链江西小炒社区厨房称重自选食材配送服务商TOP5评测!全品类供应+定制化服务权威榜单发布,赋能餐饮高效运营
  • 详细介绍:【数据库】国产数据库替代实战:金仓KES如何以“智能运维 + 低资源占用”年省百万运维成本?
  • 学习心得
  • 『NAS』在群晖部署一款好看的白板工具-Excalidraw
  • 方差的迭代计算公式 - 指南
  • Ubuntu下,MySQL密码遗失时修改密码
  • 一些特性的演变过程(C++11、C++14、C++17、C++20)
  • 支离破碎发言(七)
  • MD-FPN
  • 2025最新贵州特产/伴手礼供应商TOP5推荐!贵州/贵阳/遵义/毕节/黔东南特产选购平台/渠道/供应商/采购渠道榜单发布,甄选贵州地道风物好礼
  • 进程监控:通过 SSH 远程监测嵌入式设备进程重启
  • 街头徒手健身3硬核核心训练
  • 我们的休闲娱乐区,会变成什么样子(哽咽)
  • 【ZeroRange WebRTC】对称加密 vs 非对称加密(从原理到实践) - 详解
  • Cloudflare成功抵御AISURU僵尸网络发起的破纪录29.7 Tbps DDoS攻击
  • 2025最新贵州伴手礼厂家/采购渠道/供应商/平台/卖场/超市TOP5推荐!地道风物+文化赋能权威榜单发布,甄选贵礼传递黔地心意
  • 从 Spring Boot 2.x 到 3.5.x + JDK21:一次完整的生产环境迁移实战 - Rainbow
  • 2025.12.6日21:24-incapacity无能力
  • 001.makdown快速入门
  • Focal Loss
  • 2025最新贵州/贵阳手信/伴手礼厂家 TOP5 评测!地道风物+文化赋能权威榜单发布,甄选贵礼传递山水心意
  • Oracel VirtualBox安装Windows11时无法找到ISO文件或不满足系统要求
  • 百度统计、Google Analytics平替开源网站分析工具:Umami - 教程
  • 19