D. Omkar and Circle
Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!
You are given \(n\) nonnegative integers \(a1,a2,…,an\) arranged in a circle, where nn must be odd (ie. \(n−1\) is divisible by \(2\)). Formally, for all \(i\) such that \(2≤i≤n\), the elements \(ai−1\) and \(ai\) are considered to be adjacent, and \(an\) and \(a1\) are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.
Help Danny find the maximum possible circular value after some sequences of operations.
Input
The first line contains one odd integer \(n\) (\(1≤n<2⋅10^5\), \(n\) is odd) — the initial size of the circle.
The second line contains nn integers \(a1,a2,…,an\) (\(0≤ai≤10^9\)) — the initial numbers in the circle.
Output
Output the maximum possible circular value after applying some sequence of operations to the given circle.
Examples
input
3
7 10 2
output
17
input
1
4
output
4
Note
For the first test case, here's how a circular value of 1717 is obtained:
Pick the number at index 33. The sum of adjacent elements equals 1717. Delete 77 and 1010 from the circle and replace 22 with 1717.
Note that the answer may not fit in a 3232-bit integer.
题解
第一感觉,这个题和合并果子的那道题很像,可以尝试使用优先队列来写,合并的时候,优先合并当前位置元素小的,结果 \(wa\) 了。
优化一下需要求解的东西,
\(n\) 个物品,需要合并 \(n/2\) 次
每次选择合并的位置不能是相邻的,
\(namo\) 就可以选择以求前缀和的方式来求解不相邻的 \(n/2\) 个的最小值,拿总之减一下,或者是求一下减之后最大值。
代码
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 4e5+55;
ll a[maxn];
ll c[maxn];
ll d[maxn];
int main(){
int n;
ll sum=0;
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%lld",&a[i]);
sum+=a[i];
}
for(int i=0;i<n;++i){
if(i>=2)c[i]=c[i-2]+a[i];
else c[i]=a[i];
}
for(int i=n-1;i>=0;--i){
if(i<=n-3)d[i]=d[i+2]+a[i];
else d[i]=a[i];
}
ll maxx=c[0];
for(int i=0;i<n;++i){
maxx=max(maxx,c[i]+d[i+1]);
}
printf("%lld\n",maxx);
return 0;
}
原文地址:https://www.cnblogs.com/VagrantAC/p/13287236.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Python基础之文件操作知识
- 前端|利用js实现在日历中的签到效果
- Python基础之函数加强
- Python基础之函数二
- 前端|动态发光按钮
- Windows系统命令拼接
- 利用Hexo打造属于自己的网页版简历
- RCE远程控制Windows服务器——以win10虚拟机为例
- 解决InnoDB: Table mysql/innodb_index_stats has length mismatch
- R语言中对文本数据进行主题模型topic modeling分析
- QT学习第3天:QSlider使用方法
- qt学习第2天:QRadioButtonTest+ButtonGroup单选后提示消息,QComBox
- Python使用矩阵分解法找到类似的音乐
- SpringBoot集成ELK实现日志收集实践
- python在Scikit-learn中用决策树和随机森林预测NBA获胜者