CF573E Bear and Bowling 题解
1.CF505E Mr. Kitayuta vs. Bamboos 题解2.CF506E Mr. Kitayuta's Gift 题解3.CF512D Fox And Travelling 题解4.CF516D Drazil and Morning Exercise 题解5.CF516E Drazil and His Happy Friends 题解6.CF521D Shop 题解7.CF521E Cycling City 题解8.CF527E Data Center Drama 题解9.CF547D Mike and Fish 题解10.CF559E Gerald and Path 题解11.CF568C New Language 题解12.CF576D Flights for Regular Customers 题解13.CF578E Walking! 题解14.CF585F Digits of Number Pi 题解15.CF605E Intergalaxy Trips 题解16.CF613E Puzzle Lover 题解17.CF626G Raffles 题解18.CF526G Spiders Evil Plan 题解19.CF538G Berserk Robot 题解20.CF538H Summer Dichotomy 题解21.CF553E Kyoya and Train 题解22.CF566C Logistical Questions 题解23.CF566E Restoring Map 题解24.CF568E Longest Increasing Subsequence 题解25.CF571E Geometric Progressions 题解
26.CF573E Bear and Bowling 题解
27.CF578F Mirror Box 题解28.[AGC038E] Gachapon 题解29.CF666E Forensic Examination 题解30.CF671E Organizing a Race 题解Description
- 给定一个长度为
的序列 。 - 你要求一个
的子序列 (可以为空),使得 的值最大。 , 。
Solution
有一个显然的 dp 是设
考虑优化。
有一个结论是对于每个
证明就考虑设
通过观察可以发现
设
对于
又因为:
所以结论得证。
然后就可以从前往后枚举
时间复杂度:
Code
#include <bits/stdc++.h> // #define int int64_t const int kMaxN = 1e5 + 5; int n; int64_t a[kMaxN]; struct FHQTreap { int tot = 0, rt, ch[kMaxN][2], rd[kMaxN], sz[kMaxN]; int64_t val[kMaxN], rnk[kMaxN], tag1[kMaxN], tag2[kMaxN]; std::mt19937 rnd; int newnode(int64_t x, int64_t y) { val[++tot] = x, rnk[tot] = y; ch[tot][0] = ch[tot][1] = tag1[tot] = tag2[tot] = 0, rd[tot] = rnd(); sz[tot] = 1; return tot; } FHQTreap() { tot = 0, rnd.seed(std::random_device{}()); rt = newnode(-1e18, 1); } void pushup(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1; } void addtag(int x, int64_t v1, int64_t v2) { val[x] += v1, rnk[x] += v2, tag1[x] += v1, tag2[x] += v2; } void pushdown(int x) { if (ch[x][0]) addtag(ch[x][0], tag1[x], tag2[x]); if (ch[x][1]) addtag(ch[x][1], tag1[x], tag2[x]); tag1[x] = tag2[x] = 0; } int merge(int x, int y) { if (!x || !y) return x + y; pushdown(x), pushdown(y); if (rd[x] < rd[y]) { ch[x][1] = merge(ch[x][1], y), pushup(x); return x; } else { ch[y][0] = merge(x, ch[y][0]), pushup(y); return y; } } void split(int x, int v, int &a, int &b) { // a : >= v, b : < v if (!x) return void(a = b = 0); pushdown(x); if (val[x] >= rnk[x] * v) { a = x, split(ch[x][1], v, ch[a][1], b); } else { b = x, split(ch[x][0], v, a, ch[b][0]); } pushup(x); } void ins(int64_t x) { int a, b; split(rt, x, a, b); if (b) addtag(b, x, 1); rt = merge(merge(a, newnode(x * (sz[a] + 1), sz[a] + 1)), b); } int64_t getval(int x) { if (!x) return 0; pushdown(x); int64_t ret = std::max<int64_t>(val[x], 0); return ret + getval(ch[x][0]) + getval(ch[x][1]); } } t; void dickdreamer() { std::cin >> n; for (int i = 1; i <= n; ++i) { std::cin >> a[i]; t.ins(a[i]); } std::cout << t.getval(t.rt) << '\n'; } int32_t main() { #ifdef ORZXKR freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0); int T = 1; // std::cin >> T; while (T--) dickdreamer(); // std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步