$CF1141B Maximal Continuous Rest$
告诉你一天n件事情
a[i]为1是休息 a[i]为2是工作
求最长连续的休息时间(即最长的1
可以作为环状来求。(即环状最长的1
这题就可以用前缀和贪心等什么操作。。
然后用\(ans1ans2\)瞎搞
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
inline LL In() { LL res(0),f(1); register char c ;
while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : f = 1 ;
while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ; return res * f ;
}
int n ;
int a[200000+5] ;
int cnt[200000+5] ;
int flag = 1 ;
int ans = 0 ;
int ans1 = 0 ;
int ans2 = 0 ;
signed main() {
n = In() ;
for(register int i=1;i<=n;i++) a[i] = In() ;//读入
for(register int i=1;i<=n;i++)
if(a[i]==1) cnt[i]=cnt[i-1]+1,ans1=max(ans1,cnt[i]);//求前缀和 即当前最长的1 ans1储存最大值
else {
if(flag) ans2+=(i-1),flag=0;//储存最前面的一段1
cnt[i]=0;
}
if(cnt[n]) ans = cnt[n] + ans2;//如果第n个位置有连在一起的。
//则数字为 cnt[n]+ans2
cout << max(ans,ans1) << endl ;//ans求的是序列中最大值。 ans是首尾环状。 要求的是最长的则取max
return 0 ;
}
随机推荐
- Python学习第二阶段day1 内置函数,序列化,软件目录开发规范
内置函数 1.abs() 求绝对值 2.all() 所有元素为真才返回真 all( [1,1,2,3,-1] ) 值为True 3.any() 所有元素为假才返回假 any([0,0,0 ...
- JAVA实现创建Excel表并导出(转发)
<span style="font-family:Verdana, Arial, Helvetica, sans-serif;line-height:25.2px;background ...
- Django DTL模板语法中定义变量
- 【模板】大数乘法(51nod 1027)
#include<cstdio> #include<cstring> #include<algorithm> #define LL long long #defin ...
- 通过注解配置Bean(2)
问:怎么用注解来配置bean与bean之间的引用关系? [组件装配] 1.<context:component-scan> 元素还会自动注册AutowiredAnnotationBeanP ...
- PostGIS学习相关术语
POI 兴趣点(英语:point of interest,通常缩写成POI)乃是电子地图上的某个地标.景点,用以标示出该地所代表的政府部门.各行各业之商业机构(加油站.百货公司.超市.餐厅.酒店.便利 ...
- 20、Java并发性和多线程-Slipped Conditions
以下内容转自http://ifeve.com/slipped-conditions/: 所谓Slipped conditions,就是说, 从一个线程检查某一特定条件到该线程操作此条件期间,这个条件已 ...
- Oracle Multitenant Environment (二) Purpose
Purpose of a Multitenant Environment A multitenant environment enables the central management of mul ...
- 条款31: 千万不要返回局部对象的引用,也不要返回函数内部用new初始化的指针的引用
先看第一种情况:返回一个局部对象的引用.它的问题在于,局部对象 ----- 顾名思义 ---- 仅仅是局部的.也就是说,局部对象是在被定义时创建,在离开生命空间时被销毁的.所谓生命空间,是指它们所在的 ...
- Android之设置拖拽监听
以EditText为例: username.setOnDragListener(new OnDragListener() { @Overridepublic boolean onDrag(View v ...