题目

题意: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. 获取Bootstrap-Table的所有内容,修改行内容

    var allTableData = $tableLeft.bootstrapTable('getData');//获取表格的所有内容行 var flag = false; for( i=0;i< ...

  2. IOS开发,知识点小结,ios开发中经常使用的宏定义总结

    IOS开发,从应用跳转到用浏览器打开网页: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:// ...

  3. usaco2008 nov 区间异或求和

    Problem 11: Switching Lights [LongFan, 2008] Farmer John tries to keep the cows sharp by letting the ...

  4. js中一些常见写法的含义

    1. 常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号 ...

  5. Swift(一)简单值

    Swift的源文件扩展名是.swift 按照国际惯例,学习一门新语言写的第一个程序都是在屏幕上输出一句 “Hello, world!” .在Swift里,一行代码就搞定了: 如果你以前写过C或者Obj ...

  6. 后台while收发过程

    fuse_loop_mt.c 中fuse_do_work函数使用while循环在后台不断运行,每一个while循环中,主要有两个操作. 1. fuse_session_receive_buf(mt-& ...

  7. hdu-5721 Palace(最近点对)

    题目链接: Palace Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Others) ...

  8. 【POJ 2407】 Relatives

    [题目链接] 点击打开链接 [算法] 欧拉函数 [代码] #include <algorithm> #include <bitset> #include <cctype& ...

  9. linux线程笔记1之创建线程

    1 线程与进程的对比 这里有一个笔记详细的阐述 http://blog.csdn.net/laviolette/article/details/51506953 2 创建线程函数 int pthrea ...

  10. 洛谷 - P1115 - 最大子段和 - 简单dp

    https://www.luogu.org/problemnew/show/P1115 简单到不想说……dp[i]表示以i为结尾的最大连续和的值. 那么答案肯定就是最大值了.求一次max就可以了. 仔 ...