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 ...
随机推荐
- Yarn调度器负载模拟器——Yarn Scheduler Load Simulator (SLS)
一.概述: Yarn调度器有很多实现,如Fifo, Capacity和Fair schedulers等.与其同一时候,正在进行一些优化措施来提高调度器在不同负载和工作场景下的性能.每一个调度器都有自己 ...
- Java核心技术之基本数据类型
这篇文章.我们讨论一些java的最主要的东西.这些东西我们一般刚刚学java的时候就学过,可是不一定真正明确. 正好,我在做一个读取内存的值,涉及到bit位的值的读取和写.那就能够讨论一个java的基 ...
- python day-15 匿名函数 sorted ()函数 filter()函数 map()函数 递归 二分法
一.匿名函数 匿名函数的结构:变量 = lamda 参数: 返回值 a = lamda x : x*x # x为参数, : 后边的为函数体 print(a(x)) def ...
- 减肥 day1
今天是我减肥第一天,现在体重是147斤, 早晨吃了一碗面,喝了一碗奶,中午吃了一个apple. 6点钟去打篮球,晚上去食堂稍微吃一点东西.
- 【bzoj3620】似乎在梦中见过的样子
枚举左端点,对于每个右端点处理出以右端点为结尾最大长度使得从左端点开始的前缀等于以右端点结束的后缀,即next数组 然后一直往前跳,直到长度小于子串长度的一半为止. #include<algor ...
- python(二):使用multiprocessing中的常见问题
简介在python的解释器中,CPython是应用范围最广的一种,其具有丰富的扩展包,方便了开发者的使用.当然CPython也不是完美的,由于全局解释锁(GIL)的存在,python的多线程可以近似看 ...
- 洛谷P1113杂物——DP
题目:https://www.luogu.org/problemnew/show/P1113 每个任务的时间就是准备工作中完成最晚的那个的时间再加上自己的时间. 代码如下: #include<i ...
- 【旧文章搬运】Windows句柄表分配算法分析(实验部分)
原文发表于百度空间,2009-03-31========================================================================== 理论结合实 ...
- 阿里云RDS数据库备份文件恢复到本地mysql数据库
一.安装mysql和xtrabackup (1)安装mysql 因为RDS是5.6版本,所以我们本地的mysql数据库要与RDS版本对应. rpm -ivh http://repo.mysql.co ...
- centos7安装xtrabackup
1.安装percona依赖库: yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-re ...