【CF Edu 28 C. Four Segments】
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
You are given an array of n integer numbers. Let sum(l, r) be the sum of all numbers on positions from l to r non-inclusive (l-th element is counted, r-th element is not counted). For indices l and r holds 0 ≤ l ≤ r ≤ n. Indices in array are numbered from 0.
For example, if a = [ - 5, 3, 9, 4], then sum(0, 1) = - 5, sum(0, 2) = - 2, sum(1, 4) = 16 and sum(i, i) = 0 for each i from 0 to 4.
Choose the indices of three delimiters delim0, delim1, delim2 (0 ≤ delim0 ≤ delim1 ≤ delim2 ≤ n) and divide the array in such a way that the value of res = sum(0, delim0) - sum(delim0, delim1) + sum(delim1, delim2) - sum(delim2, n) is maximal.
Note that some of the expressions sum(l, r) can correspond to empty segments (if l = r for some segment).
Input
The first line contains one integer number n (1 ≤ n ≤ 5000).
The second line contains n numbers a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109).
Output
Choose three indices so that the value of res is maximal. If there are multiple answers, print any of them.
Examples
input
3
-1 2 3
output
0 1 3
input
4
0 0 -1 0
output
0 0 0
input
1
10000
output
1 1 1
【翻译】给出一个长度为n的序列(1~n),现在需要选择三个点d1,d2,d2(0<=d1<=d2<=d3<=n),设ABCD分别为区间(0,d1],(d1,d2],(d2,d3],(d3,n]的内部元素和,求A-B+C-D取到最大值时d1,d2,d3的值,情况多种就随便输出一种。
题解:
①好像怎么弄都是O(n2),枚举d2,然后取d1,d3的最有位置更新答案。
②为辅助上述方法,预处理(A-B),(C-D)的最大值,并记录取最大值时候d的位置。
③预处理会使用到前缀和。
#include<stdio.h>
#define ll long long
#define comb (val1[i]+val2[i])
#define S(l,r) (sum[r]-sum[l-1])
#define go(i,a,b) for(int i=a;i<=b;i++)
const int N=5003;int n,p1[N],p3[N],P1,P2,P3;
ll a[N],_[N],sum[N],val1[N],val2[N],ans,t;
int main()
{
scanf("%d",&n);ans=1ll*-1e9*1e9; go(i,1,n)scanf("%I64d",a+i),sum[i]=sum[i-1]+a[i];
go(i,0,n){val1[i]=1ll*-1e9*1e9;
go(j,0,i)if((t=S(1,j)-S(j+1,i))>val1[i])p1[i]=j,val1[i]=t;}
go(i,0,n){val2[i]=1ll*-1e9*1e9;
go(j,i,n)if((t=S(i+1,j)-S(j+1,n))>val2[i])p3[i]=j,val2[i]=t;}
go(i,0,n)if(val1[i]+val2[i]>ans)ans=comb,P1=p1[i],P2=i,P3=p3[i]; printf("%d %d %d\n",P1,P2,P3);return 0;
}//Paul_Guderian
挥挥手倦鸟飞过丛林,隐没在碎与溃的深谷。——————汪峰《挥挥手》
【CF Edu 28 C. Four Segments】的更多相关文章
- 【CF Edu 28 A. Curriculum Vitae】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【CF Edu 28 B. Math Show】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【2020.11.28提高组模拟】T1染色(color)
[2020.11.28提高组模拟]T1染色(color) 题目 题目描述 给定 \(n\),你现在需要给整数 \(1\) 到 \(n\) 进行染色,使得对于所有的 \(1\leq i<j\leq ...
- 【CF edu 30 D. Merge Sort】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【Cf edu 30 B. Balanced Substring】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【CF Round 434 B. Which floor?】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【2020.11.28提高组模拟】T2 序列(array)
序列(array) 题目描述 给定一个长为 \(m\) 的序列 \(a\). 有一个长为 \(m\) 的序列 \(b\),需满足 \(0\leq b_i \leq n\),\(\sum_{i=1}^ ...
- B. Lost Number【CF交互题 暴力】
B. Lost Number[CF交互题 暴力] This is an interactive problem. Remember to flush your output while communi ...
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
随机推荐
- MySQL选择的执行计划性能底下原因分析--实战案例分析
MySQL是自动会选择它认为好的执行划,但是MySQL毕竟是程序,还没有达到像人类思考这么智能,还是通过一些按部就班的算法实现最优执行计划(基于cost)的选择.下面就是一个真实的案例,带你来看看My ...
- 交换机基础配置之三层交换机实现vlan间通信
我们以上面的拓扑图做实验,要求为pc1,pc2,pc3配置为vlan10,pc4,pc5,pc6配置为vlan20,pc7,pc8,pc9配置为vlan30 server0和server1配置为vla ...
- ubuntu中使用apt命令安装ipython失败解决方案
在最近使用ubuntu安装ipython时,出现如下报错: 出现这个问题,主要是因为apt还在运行,故解决方案为: 1.找到并且杀掉所有的apt-get 和apt进程 运行下面的命令来生成所有含有 a ...
- PLC状态机编程第四篇-历史状态处理
今天我们接着上次的控制任务,加入历史状态,这个任务会比较复杂,象这样的任务我们倾向于自动生成PLC程序,自己写容易出错.但为了演示,我们可以尝试一下.言归正传,下面是我们的控制任务. 控制任务 这次的 ...
- mybatis中@Param用法
用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中 我们先来看Mapper接口中的@Select方法 package Ma ...
- Cache、Buffer的区别
什么是Cache?什么是Buffer?二者的区别是什么? Buffer和Cache的区别 buffer与cache操作的对象就不一样. 1.buffer(缓冲)是为了提高内存和硬盘(或其他I/O设备) ...
- C# 中的正则简单例子
public static void Main() { Regex rgx = new Regex(@"[S|s]et-[C|c]ookie: (?<cookieName>\w+ ...
- IOS测试,打不开要测试的APP怎么办?设置信任
步骤:设置-->通用-->设备管理-->企业级应用-->信任 具体教程:http://jingyan.baidu.com/article/ab69b27085ab002ca71 ...
- 用Jenkins自动化搭建测试环境
1-1 课程介绍 2-1 Jenkins安装 2-2 Jenkins插件 2-3 Jenkins基础设置 3-1 Linux系统准备 3-2 安装Java环境 3-3 安装并配置Git 3-4 安装并 ...
- Mysql忘记root密码怎么办?(已解决)
为了写这篇文档,假装一下忘记密码!!!! 首先我数据库是正常的,可以使用. root@localhost:~# mysql -uroot -p mysql> mysql> show dat ...