我试图解决这个问题。
给出一个整数 M 和一个由 N 个非负整数组成的非空零索引数组 A。数组 A 中的所有整数都小于或等于 M。
一对整数 (P, Q),使得 0 ≤ P ≤ Q < N,称为数组 A 的切片。切片由元素 A[P]、A[P + 1]、...、一个[问]。不同的切片是仅由唯一数字组成的切片。也就是说,单个数字在切片中出现的次数不超过一次。
例如,考虑整数 M = 6 和数组 A 使得:
A[0] = 3 A[1] = 4 A[2] = 5 A[3] = 5 A[4] = 2
正好有九个不同的切片:(0, 0), (0, 1), (0, 2), (1, 1), (1,2), (2, 2), (3, 3), ( 3, 4) 和 (4, 4)。
目标是计算不同切片的数量。
提前致谢。
#include <algorithm>
#include <cstring>
#include <cmath>
#define MAX 100002
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
using namespace std;
bool check[MAX];
int solution(int M, vector<int> &A) {
memset(check, false, sizeof(check));
int base = 0;
int fibot = 0;
int sum = 0;
while(fibot < A.size()){
if(check[A[fibot]]){
base = fibot;
}
check[A[fibot]] = true;
sum += fibot - base + 1;
fibot += 1;
}
return min(sum, 1000000000);
}