题目

题意:sum(l,r)表示数列a中索引为l到r-1(都包含)的数之和(如果l==r则为0)。给出数列a,求合适的delim0delim1delim2,使res = sum(0, delim0) - sum(delim0, delim1) + sum(delim1, delim2) - sum(delim2, n)最大。

方法:枚举delim1,扫一遍就可以求出此时能使res最大的delim0和delim2。记录res最大值。实现有一些细节,比如可以将res的计算公式化为前缀和的公式。

曾经错在:1.int会爆,没注意  2.输出了调试的时候输出的内容(ans)而不是dl0,dl1,dl2

 #include<cstdio>
typedef long long LL;
LL n,a[],dl1,dl0,dl2,t_max_dl0,t_max_dl2,max_dl0,max_dl2,t_ans,ans,max1,max_dl1;
int main()
{
LL i,t1;
scanf("%lld",&n);
for(i=;i<n;i++)
scanf("%lld",&a[i]);
for(dl1=;dl1<n;dl1++)
{
t_ans=;
t1=;
for(i=;i<dl1;i++)
t1-=a[i];
//此时表示dl0=0时sum(0,delim0)-sum(delim0,delim1)
max1=t1;
t_max_dl0=;
for(dl0=;dl0<=dl1;dl0++)
{
t1+=*a[dl0-];
if(t1>max1)
{
max1=t1;
t_max_dl0=dl0;
}
}
t_ans+=max1;
t1=;
for(i=dl1;i<n;i++)
t1-=a[i];
//此时表示dl2=dl1时sum(delim1,delim2)-sum(delim2,n)
max1=t1;
t_max_dl2=dl1;
for(dl2=dl1+;dl2<=n;dl2++)
{
t1+=*a[dl2-];
if(t1>max1)
{
max1=t1;
t_max_dl2=dl2;
}
}
t_ans+=max1;
if(t_ans>ans)
{
max_dl0=t_max_dl0;
max_dl1=dl1;
max_dl2=t_max_dl2;
ans=t_ans;
}
}
printf("%lld %lld %lld",max_dl0,max_dl1,max_dl2);
//printf("%lld",ans);
return ;
}

暴力对拍程序:

 #include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
LL c[];
LL n,m;
LL res,max_res,a1,a2,a3;
LL lowbit(LL x)
{
return x&-x;
}
void add(LL num,LL x)
{
while(num<=n)
{
c[num]+=x;
num+=lowbit(num);
}
}
LL sum1(LL x)
{
LL ans=;
while(x>)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
LL sum(LL l,LL r)
{
if(l>r) return ;
return sum1(r)-sum1(l-);
}
int main()
{
LL i,j,k,t;
scanf("%lld",&n);
for(i=;i<=n;i++)
{
scanf("%lld",&t);
add(i,t);
}
for(i=;i<=n;i++)
for(j=i;j<=n;j++)
for(k=j;k<=n;k++)
{
res=sum(,i)-sum(i+,j)+sum(j+,k)-sum(k+,n);
if(res>max_res)
{
max_res=res;
a1=i;a2=j;a3=k;
}
}
printf("%lld",max_res);
return ;
}

Four Segments CodeForces - 846C的更多相关文章

  1. D - Nested Segments CodeForces - 652D (离散化+树桩数组)

    D - Nested Segments CodeForces - 652D You are given n segments on a line. There are no ends of some ...

  2. Segments CodeForces 909B (找规律)

    Description You are given an integer N. Consider all possible segments (线段,划分)on the coordinate axis ...

  3. Xors on Segments Codeforces - 620F

    http://codeforces.com/problemset/problem/620/F 此题是莫队,但是不能用一般的莫队做,因为是最优化问题,没有办法在删除元素的时候维护答案. 这题的方法(好像 ...

  4. A - Points and Segments CodeForces - 429E

    题解: 方法非常巧妙的一道题 首先考虑要求全部为0怎么做 发现是个欧拉回路的问题(很巧妙) 直接dfs一遍就可以了 而这道题 要求是-1,1,0 我们可以先离散化 完了之后判断每个点被奇数还是偶数条边 ...

  5. Bipartite Segments CodeForces - 901C (区间二分图计数)

    大意: 给定无向图, 无偶环, 每次询问求[l,r]区间内, 有多少子区间是二分图. 无偶环等价于奇环仙人掌森林, 可以直接tarjan求出所有环, 然后就可以预处理出每个点为右端点时的答案. 这样的 ...

  6. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  7. codeforces 895B XK Segments 二分 思维

    codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...

  8. Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题

    C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...

  9. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

随机推荐

  1. 使用$.when()解决AJAX异步难题之:多个ajax操作进行逻辑与(and)

    上一篇文章"JQuery.deferred提供的promise解决方式",提到了javascript异步操作的3个问题,以及javascript Promise入门.如今我们看下怎 ...

  2. Android SDK更新失败的解决方案(原创)

    笔者在搭建好Android环境后,进行Android的SDK更新下载升级,乌龟的速度,更让人生气的是到了85%的进度时,直接timeout,循环3次无果.查阅相关资料,原来是Google的服务器遭遇了 ...

  3. Python 003- 小知识汇总(更新中)

    #查询key是否存在,可以在使用未知的字典的时候使用 #-*- coding:utf-8 -*- D={'a':1,'c':3,'b':2} for key in sorted(D): print(k ...

  4. C、C++混合编程之extern "C"

    为何要“混合编程”?举个例子: CHeader.h #ifndef C_HEADER_H #define C_HEADER_H void func(); #endif CHeader.c #inclu ...

  5. TFS Server 2017 自动化部署步骤

    1 第一步,在服务器上安装TFS 2 第二步,安装完TFS后需要配置你的项目,选择管理代码的方式,这里我们可以选择传统的TFS 也可以选择GIT 方式,此处我选择的GIT 方式 3 第三步,设置代理. ...

  6. SDUT OJ 周赛 找有毒的那杯水(思维逻辑 + 分治思想 )

    你打我啊 Time Limit: 500ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 最近看了一个非常好玩的题,如果有972杯水,其中有971个没有毒的,1个有 ...

  7. 内部类 final变量的生命周期

    (1).内部类是外部类的一个成员,就像外部类的成员方法一样,所以内部类有权限访问外部类的所有成员,包括private的. (2).内部类不能访问外部类方法中的局部变量,除非变量是final的(一般发生 ...

  8. 跨平台实现zip压缩加密功能

    使用zlib将文件夹压缩成zip文件时,需要自己读取文件然后写入zip文件.利用官方下载的zlib包中包含的contrib/minizip/zip.h和zip.c代码提供的函数,可以很容易实现这个功能 ...

  9. POJ1113:Wall (凸包:求最小的多边形,到所有点的距离大于大于L)

    Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the ...

  10. [Java] public, private, protected

      同包不同类的成员 不同包中子类的成员 不同包非子类的成员 public √ √ √ protected √ √ × 默认 √ × × private × × ×