Four Segments CodeForces - 846C
题意:sum(l,r)表示数列a中索引为l到r-1(都包含)的数之和(如果l==r则为0)。给出数列a,求合适的delim0, delim1, delim2,使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的更多相关文章
- D - Nested Segments CodeForces - 652D (离散化+树桩数组)
D - Nested Segments CodeForces - 652D You are given n segments on a line. There are no ends of some ...
- Segments CodeForces 909B (找规律)
Description You are given an integer N. Consider all possible segments (线段,划分)on the coordinate axis ...
- Xors on Segments Codeforces - 620F
http://codeforces.com/problemset/problem/620/F 此题是莫队,但是不能用一般的莫队做,因为是最优化问题,没有办法在删除元素的时候维护答案. 这题的方法(好像 ...
- A - Points and Segments CodeForces - 429E
题解: 方法非常巧妙的一道题 首先考虑要求全部为0怎么做 发现是个欧拉回路的问题(很巧妙) 直接dfs一遍就可以了 而这道题 要求是-1,1,0 我们可以先离散化 完了之后判断每个点被奇数还是偶数条边 ...
- Bipartite Segments CodeForces - 901C (区间二分图计数)
大意: 给定无向图, 无偶环, 每次询问求[l,r]区间内, 有多少子区间是二分图. 无偶环等价于奇环仙人掌森林, 可以直接tarjan求出所有环, 然后就可以预处理出每个点为右端点时的答案. 这样的 ...
- 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 ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题
C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
随机推荐
- Vue生命周期方法。
- mysql关闭skip-grant-tables快速重置mysql密码
如果你忘记了mysql密码几乎是没有什么好办法可以直接修改密码了,但我们可以在my.ini把加上skip-grant-tables,然后重启mysql就不需要密码了,这时我们再修改root密码,最后再 ...
- cocos2d-x(vs2012)环境搭建(第一篇)[版本号:cocos2d-x-3.1.1]
1.下载资源 下载cocos2d-x包V3.1.1,下载戳这里: http://www.cocos2d-x.org/download vs2012下载戳这里: http://www.xiazaiba. ...
- WPF DataGrid获取选择行的数据
在WPF中,单击DataGrid,如何获取当前点击的行? 比如在MouseDoubleClick事件中,事实上获取的选中行是一个DataRowview,你可以通过以下的方法来获取选中行的数据,需要引用 ...
- hadoop eclipse插件生成
hadoop eclipse插件生成 做了一年的hadoop开发.还没有自动生成过eclipse插件,一直都是在网上下载别人的用,今天有时间,就把这段遗憾补回来,自己生成一下,废话不说,開始了. 本文 ...
- VUE 之 vuex 和 axios
1.Vuex 部分 1.1 Vuex 是专门为vue.js设计的集中式状态管理架构,其实就是将数据存在一个store中,共各个组件共享使用 1.2 配置: 1.2.1 下载:--npm install ...
- Replace Type Code With Class和Replace Type Code With Subclass和Replace Type Code With State/Strategy
周末闲来写写看书总结,今天写<重构>中的3个重要手法,分别是Replace Type Code With Class.Replace Type Code With Subclass和Rep ...
- spring核心框架体系结构(各个jar包作用)
转自:https://blog.csdn.net/sunchen2012/article/details/53939253 弄懂spring就是弄懂spring各个jar包的作用 spring的jar ...
- nginx配置实战以及查看并发数
http://www.cnblogs.com/kevingrace/p/6095027.html http://www.cnblogs.com/lianzhilei/p/6025267.html
- android user用户版本提高adb权限【转】
本文转载自:http://blog.csdn.net/liyongming1982/article/details/14108111 有的user用户版本的log 不全,且push/pull某些文件或 ...