【ST】【CF855B】 Marvolo Gaunt's Ring
Description
给定三个数 \(p~,~q~,~r~\),以及一个数组 \(a\),
找出三个数 \(i~,~j~,~k\) ,其中 \(i~\leq~j~\leq~k\) 最大化 \(p~\times~a_i~+~q~\times~a_j~+~r~\times~a_r\)
Input
第一行是数组长度 \(n\) 以及 \(p~,~q~,~r~\)。
第二行 \(n\) 个数代表这个数组
Output
输出一个整数代表答案
Hint
\(-10^5~\leq~n~\leq~10^5\)
其他数据 \(\in~[-10^9~,~10^9]\)
Solution
考虑最暴力的方法,枚举 \(i~,~j~,~k\) ,计算答案,时间复杂度\(O(n^3)\)
然后考虑如果枚举了其中两个值,剩下一个可以直接通过查询区间最值确定。例如,如果枚举了 \(i~,~j\),则 \(k\) 对应的最优解一定是 ( \(k~>~0\) 时) \(k~\times~\max_{s~=~j}^{n}~a_s\),( \(k~\leq~0\) 时) \(k~\times~\min_{s~=~j}^{n}~a_s\)。枚举其它两个元素同理。这样就可以用ST预处理区间最值,然后做到 \(O(1)\) 查询,复杂度 \(O(n\log n)-O(n^2)\) 。
考虑上述做法的缺陷在于枚举量还是太大,能否可以只枚举一个位置呢?我们发现如果枚举一个位置以后可以确定剩下两个区间,就可以只枚举一个位置。于是发现枚举 \(j\) 符合要求。于是只枚举 \(j\) 按照上面的方法求 \(i~,~k\) 的对应答案。时间复杂度 \(O(n\log n)~-~O(n)\)
Code
#include <cmath>
#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long
typedef long long int ll;
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 100010;
const ll INF = -1000000000ll;
int n;
int LOG[maxn];
ll p, q, r, ans = -3000000000000000010ll;
ll MU[maxn], ST[2][20][maxn];
ll ask(ci, ci, ci);
int main() {
freopen("1.in", "r", stdin);
qr(n); qr(p); qr(q); qr(r);
for (rg int i = 1; i <= n; ++i) qr(MU[i]);
for (rg int i = 0; (1 << i) <= n; ++i) {
LOG[1 << i] = i;
}
for (rg int i = 3; i <= n; ++i) if(!LOG[i]) LOG[i] = LOG[i - 1];
for (rg int i = 0; i < 20; ++i)
for (rg int j = 0; j < maxn; ++j)
ST[1][i][j] = INF;
for (rg int i = 0; i < 20; ++i)
for (rg int j = 0; j < maxn; ++j)
ST[0][i][j] = -INF;
for (rg int i = 1; i <= n; ++i) ST[1][0][i] = ST[0][0][i] = MU[i];
for (rg int i = 1; i < 20; ++i) {
int len = (1 << i) - 1;
for (rg int l = 1; l <= n; ++l) {
int r = l + len; if (r > n) break;
ST[0][i][l] = std::max(ST[0][i - 1][l], ST[0][i - 1][l + (1 << (i - 1))]);
ST[1][i][l] = std::min(ST[1][i - 1][l], ST[1][i - 1][l + (1 << (i - 1))]);
}
}
for (rg int i = 1; i <= n; ++i) {
ans = std::max(q * MU[i] + p * ask(1, i, p < 0) + r * ask(i, n, r < 0), ans);
}
qw(ans, '\n', true);
return 0;
}
ll ask(ci l, ci r, ci cur) {
int len = r - l + 1;
if (cur) return std::min(ST[1][LOG[len]][l], ST[1][LOG[len]][r - (1 << LOG[len]) + 1]);
else return std::max(ST[0][LOG[len]][l], ST[0][LOG[len]][r - (1 << LOG[len]) + 1]);
}
Summary
这次貌似也没啥好summary的……就是我好菜啊ST都调了半天
【ST】【CF855B】 Marvolo Gaunt's Ring的更多相关文章
- Codeforces 855B - Marvolo Gaunt's Ring
855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个 ...
- Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)
Description Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as h ...
- B. Marvolo Gaunt's Ring 前缀后缀
B. Marvolo Gaunt's Ring 这种一般只有三个的都可以处理前缀和后缀,再枚举中间这个值. 这个和之前写过的C. Four Segments 前缀后缀 处理方式很像. #include ...
- Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)
B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaun ...
- 【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring
[链接]h在这里写链接 [题意] 给你n个数字; 让你在其中找出三个数字i,j,k(i<=j<=k); 使得p*a[i]+q*a[j]+r*a[k]最大; [题解] /* 有一个要 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】
要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...
- 【ST开发板评测】Nucleo-F411RE开箱报告
前言 面包板又举办开发板试用活动了,很荣幸能获得一块ST官方的Nucleo-F411RE开发板,感谢面包板社区和ST意法半导体的赞助,这是我第一次试用官方的开发板,收到板子后查了一些关于ST官方开发板 ...
- 【ST开发板评测】使用Python来开发STM32F411
前言 板子申请了也有一段时间了,也快到评测截止时间了,想着做点有意思的东西,正好前一段时间看到过可以在MCU上移植MicroPython的示例,就自己尝试一下,记录移植过程. MicroPython是 ...
随机推荐
- TPO-17 C2 Reschedule part-time job in campus dining hall
TPO-17 C2 Reschedule part-time job in campus dining hall 第 1 段 1.Listen to a conversation between a ...
- ES数据备份到HDFS
1.准备好HDFS(这里我是本机测试) 2.es 安装repository-hdfs插件 (如es为多节点需在每个节点都安装插件) elasticsearch-plugin install repos ...
- 20181113-7 Beta阶段第1周/共2周 Scrum立会报告+燃尽图 05
作业要求https://edu.cnblogs.com/campus/nenu/2018fall/homework/2387 版本控制https://git.coding.net/lglr2018/F ...
- 前端获取URL和SESSON中的值
.CS中代码 public ActionResult Index(string viewname, bool partial = false) { //获取URL中的 foreach (var key ...
- pspo过程文档
项目计划总结: 日期/任务 听课 编写程序 阅读相关书籍 日总计 周一 110 60 ...
- [数位DP]把枚举变成递推(未完)
动态规划(DP)是个很玄学的东西 数位DP实际上 就是把数字上的枚举变成按位的递推 有伪代码 for i =这一位起始值 i<=这一位终止值 dp[这一位][i]+=dp[这一位-1][i]+- ...
- oracle执行完shutdown immediate后登陆不上了怎么办
在sqlplus 里登录后使用shutdown immediate 关闭数据库后若没有使用startup重启数据库就退出窗口则会出现下一次重启sqlplus窗口时无法登录的现象,解决方法如下 一.启动 ...
- Visual C++ 8.0对象布局
哈哈,从M$ Visual C++ Team的Andy Rich那里又偷学到一招:VC8的隐含编译项/d1reportSingleClassLayout和/d1reportAllClassLayout ...
- IOC 依赖注入 Unity
http://kb.cnblogs.com/page/115333/ http://www.bianceng.cn/Programming/net/201007/18255.htm http://bl ...
- 软件工程个人作业3——集大通APP案例分析
第一部分:调研, 评测 1.第一次上手体验 主要界面截图: 感受: 1.界面不美观: 2.特色功能展现模块不突出,以上截图为打开APP所看到的界面展示,但是这些功能都不是该APP的特色功能,显得有些累 ...