CodeForces - 1333C Eugene and an array(尺取)
题目链接:点击查看
题目大意:给出一个长度为 n 的数组 a,抛出 good 数组的定义:
- good 数组为数组 a 的一个子数组
- good 数组的任意子数组之和均不为 0
(注意区分子数组和子数列的区别)
题目分析:首先是常识,对于一个长度为 n 的数组而言,有 n * ( n + 1 ) / 2 个子数组,有 n * ( n + 1 ) / 2 - 1 个非空子数组
其次分析数据范围,n 高达 2e5,所以肯定不能暴力 n * n 去枚举每一个子数组判断
因为子数组是连续的一段数组,加上题目将子数组之和作为判断条件,提示到这里不难想到可以维护一个前缀和 sum 方便后续操作
然后是加上一小点思维,稍微想一下不难发现,一个 good 数组如果想要满足任意子数组之和均不为 0 ,那么这段 good 数组所对应的前缀和的值不能有重复,换句话说,如果数组 a 中的 [ l , r ] 这段为 good 数组,那么 sum[ i ] != sum[ j ] ,i , j ∈ [ l , r ]
思考到这里不难想到尺取了,现在还差一点,那就是如何维护答案,因为尺取可以维护出最长的那段 good 数组,所以我们可以在实现尺取时,每次都将 r 指针向右移动一个单位,根据条件维护 l 指针的位置,此时新出现的 good 数组有
- a[ l + 1 ] ~ a[ r ]
- a[ l + 2 ] ~ a[ r ]
- ......
- a[ r ] ~ a[ r ]
为了方便实现,l 代表的是当前 good 数组左端点的前一个点,r 代表的自然是当前 good 数组的右端点了
代码的实现意外的简单。。
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;map<LL,bool>vis;LL sum[N];int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n;scanf("%d",&n);vis[0]=true;//sum[0]=0for(int i=1;i<=n;i++){int num;scanf("%d",&num);sum[i]=sum[i-1]+num;}LL ans=0;int l=0,r=1;while(r<=n){while(vis[sum[r]]){vis[sum[l]]=false;l++;}vis[sum[r]]=true;ans+=r-l;r++;}printf("%lld\n",ans);return 0;
}
CodeForces - 1333C Eugene and an array(尺取)相关推荐
- 【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)
题干: You are given an array a with n elements. Each element of a is either 0 or 1. Let's denote the l ...
- 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)
题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...
- CF1547F Array Stabilization (GCD version) st表 + 尺取/二分
传送门 题意: 思路: 容易发现,我们将所有aaa都除上所有aaa的gcdgcdgcd,实际上就是让你求一个最小的lenlenlen,对于所有iii,gcd(ai,ai+1,...,ai+len−1) ...
- Codeforces Round #632 (Div. 2) C. Eugene and an array 思维 + 前缀和
传送门 文章目录 题意: 思路: 题意: 给定一个长度为nnn的序列aaa,定义一段区间为好区间是这段区间的所有连续子区间的和都不为000,求好区间的个数. 思路: 套路题,定义aia_iai的前缀 ...
- Codeforces Round #736 (Div. 2) D. Integers Have Friends ST表gcd + 尺取
传送门 文章目录 题意: 思路: 题意: 给你一个序列aaa,求一个最长的子序列[l,r][l,r][l,r]满足aimodm=ai+1modm=...=armodma_i\bmod m=a_{i+1 ...
- 【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)
题干: The array a with n integers is given. Let's call the sequence of one or more consecutive element ...
- 【尺取或dp】codeforces C. An impassioned circulation of affection
http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...
- Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)
排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...
- CodeForces - 1358D The Best Vacation(前缀和+尺取)
题目链接:点击查看 题目大意:给出 n 个数组成的数列,每个元素都可以展开为 1 , 2 , 3 .... a[ n ] ,现在将数列首尾相接,要求选取一段长度为 x 的连续数列,使得元素和最大 题目 ...
最新文章
- JS禁止input表单元素手动输入方法(转)
- NIO详解(十一):线程间通信管道Pipe
- ABAP 在字符串之间加入若干空格
- BZOJ3609 [Heoi2014]人人尽说江南好 【博弈】
- replace和replaceAll
- linux fork多进程 demo
- rust实现wss访问_Rust的所有权,第2部分
- 福昕高级PDF编辑器
- 随想录(python编写的计算器和贪吃蛇游戏)
- CACHECLOUDV1.0慢日志定时任务创建流程分析
- 华为交换机S3700基本配置
- 什么的SIT测试?什么是UAT测试?
- 微信公众号支付报错:当前页面的url未注册
- 各种开源数据库同步工具汇总
- 一文解读KANO模型
- 企业商誉管理系统(业务开展)
- oracle自动建立分区interval,使用Oracle 11g创建Interval分区表(转)
- Android下的蓝牙自动配对
- 三、Kubernetes集群的命令行工具kubectl
- 依据imu姿态角计算z轴倾角_树莓派小车-07-IMU姿态解算 imu_complementray_filter