[KMOI R1] 五五五五 (Easy)题解
Fire_flame · · 题解
\mathbf{Solution}
子任务
子任务
那么答案即为:
不难发现,枚举左端点
时间复杂度
子任务
时间复杂度
子任务
因为序列单调不上升,而且最大数为
所以如果出现了
即可转化成子任务
子任务
考虑递推,假设当前右端点枚举到了第
-
如果
a_r\neq5 ,cnt 清零,不添加贡献。 -
如果
a_r=5 ,考虑左端点l :- 首先
cnt 要记得加一。 - 如果左端点
l\le r-cnt ,每一个区间贡献添加cnt 。总贡献添加(r - cnt + 1) \times cnt 。 - 如果左端点
r-cnt+1\le l\le r ,总贡献添加cnt\times(cnt-1)\div 2 。
- 首先
最后统计答案即可。
标程:
#include<bits/stdc++.h>//by Fire_flame
#define int long long
using namespace std;
const int MAXN = 5e5 + 5, MOD = 1e9 + 7;
int n, ans, cnt, a[MAXN];
signed main(){
scanf("%lld", &n);
ans = 0, cnt = 0;
for(int i = 1;i <= n;i ++)scanf("%lld", &a[i]);
for(int i = 1;i <= n;i ++){
if(a[i] == 5)cnt ++;
else cnt = 0;
ans += (i - cnt + 1) * cnt % MOD + cnt * (cnt - 1) / 2 % MOD;
ans %= MOD;
}
printf("%lld", ans);
return 0;
}