codeforces C. Eugene and an array

在这里插入图片描述

题目

题意:

给你一个序列,这个序列可以删除开始的元素和结尾的元素,删完后如果满足序列中连续的相加没有一个为 0 0 则算好的,最后问好的有多少个。

思路:

因为我们求出来的不能出现和为 0 0 的数值,所以如果用前缀和来表示的话,那么就是两个前缀和不能是同一数字,如果是的话,那么就说明了中间的和相加是 0 0 ,所以这个时候我们能够取的地方就是前一个一样数字的后一个元素,这样才可以保证中间不可能出现相加和为 0 0 的情况,然后在我们取前缀和的时候实际上就已经将删除结尾的元素算进去了,比如你取第 i i 个元素,那么 i i ~ n n 的元素是不是相当于删除的状态,每一次都加上 i M a x i- Max ,是因为我们以 i i 点为结束点,那么在 M a x Max ~ i i 中,都存在符合条件的,所以以 i i 点为结束点,那么是不是有 M a x Max ~ i i 为一种, ( M a x + 1 ) (Max+1) ~ i i 等等,所以最后就是 i M a x i-Max

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 2e5 + 10;
ll ql[maxn] = {0}, a[maxn], qr[maxn] = {0};
map<ll, int> mp;
int main() {
    int n, Max = 0;
    read(n);
    for (int i = 1; i <= n; i++) read(a[i]), ql[i] = ql[i - 1] + a[i];
    mp[0] = 1;
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        Max = max(Max, mp[ql[i]]);
        ans += 1ll * (i - Max);
        mp[ql[i]] = i + 1;
    }
    printf("%lld", ans);
    return 0;
}

发布了463 篇原创文章 · 获赞 27 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_45031646/article/details/105424052