题目

题意: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. [攻防实战]CTF大赛准备(手动注入sql)

    一.IIS write漏洞利用 先用工具扫描,再上传小马,使用菜刀连接即可. 思考点: 如何获知是一台IIS站点? 本例中上传的一句话木马是什么意思? <%eval request(" ...

  2. chmod|chown|chgrp和用法和区别

    1.chgrp(改变文件所属用户组) chgrp 用户组   文件名   ###就是这个格了.如果整个目录下的都改,则加-R参数用于递归. 如:chgrp -R   user smb.conf 2.c ...

  3. 20170228 ALV method中用E消息,会退出到初始界面;STOP 会dump;

    再回车就处理界面了, 所以,Handel_data_change 做数据检查时,如果需要报错要用到, CALL METHOD er_data_changed->add_protocol_entr ...

  4. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. 组合模式(遍历树,file基表示文件也表示文件夹)

    组合模式多个对象形成树形结构以表示“整体--部分”的结构层次.组合模式对单个对象(即叶子对象)和组合对象(即容器对象)的使用具有一致性. 组合模式又可以称为“合成模式“ 或 ”整体-部分模式”,属于对 ...

  6. Codeforces Beta Round #25 (Div. 2 Only)E. Test

    E. Test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  7. Oracle :修改数据库服务器字符集

    最近,有现场反应,程序显示乱码.感觉很奇怪,该系统已经卖出去无数了.肯定是现场数据库字符集有问题,经过查看, 现场环境: window系统,oracle10g. 我们要求的数据库字符集是AL32UTF ...

  8. 2016-5-23 jsp

    1.table的边框:rules这个参数,它有三个值(cols,rows,none),当rules=cols时,表格会隐藏横向的分隔线,也就是我们只能看到表格的列:当rules=rows时,就隐藏了纵 ...

  9. Go语言的管道Channel用法

    本文实例讲述了Go语言的管道Channel用法.分享给大家供大家参考.具体分析如下: channel 是有类型的管道,可以用 channel 操作符 <- 对其发送或者接收值. ch <- ...

  10. Extjs散记

    在元素的后面添加文字,如 汇率:_____%   { fieldLabel : '输入框', xtype : 'textfield', anchor : '80%', listeners: {     ...