2021.7.21--牛客补题
题目内容:
链接:https://ac.nowcoder.com/acm/contest/18428/C 来源:牛客网 Arup has to make many practice questions for his Computer Science 1 students. Since many of the questions deal with arrays, he has to generate arrays for his students. Since he doesn’t want to give them difficult practice problems, he always guarantees that the arrays (given to the students) have unique values. Namely, no value will appear twice in any of his arrays. Unfortunately, Arup is too lazy to generate arrays! About 20 years ago when he started teaching Computer Science 1, he made one really long array to reuse but this long array may have duplicate values. When he makes problems, he simply grabs a contiguous subsequence of this long array to be the array to be used for a problem but he needs to make sure the contiguous subsequence does not contain duplicates. If the long array has terms a[0], a[1], …, a[n-1], a contiguous subsequence of the long array is any sequence of j-i+1 terms a[i], a[i+1], …, a[j] where 0 ≤ i ≤ j ≤ n – 1. Given an array of n integers, determine how many contiguous subsequences of the array do not contain any repeated values. Note that two subsequences with the same contents are considered different (i.e., both counted in the total) if they start at different locations of the original long array. 输入描述: The first input line contains a single positive integer, n (1 ≤ n ≤ 1e5), representing the size of the input array. The following line contains n space separated integers, representing the values of the input array, in the order they appear in the array. Each of the array values will be an integer between 1 and 1e18, inclusive. (Note: 1e5 means 1 times 10 to the fifth power, that is, 100000) 输出描述: On a line by itself, output the total number of contiguous subsequences of the input array that do not contain any repeated values. 示例1 输入 复制 5 1 1 2 1 5 输出 复制 9 示例2 输入 复制 8 2 12 3 12 3 2 6 9 输出 复制 22
题意:
给定一个由 n 个整数组成的数组,确定该数组有多少个连续子序列不包含任何重复值。如果具有相同内容的两个子序列从原始长数组的不同位置开始,则它们被认为是不同的(即,两者都计入总数)。
输入描述: 第一个输入行包含一个正整数 n (1 ≤ n ≤ 1e5),表示输入数组的大小。下面一行包含 n 个空格分隔的整数,表示 输入数组,按照它们在数组中出现的顺序。每个数组值都是 1 到 1e18(含)之间的整数。 (注:1e5 表示 10 次方的 1 次方,即 100000)
输出描述: 在一行上,输出输入数组的不包含任何重复值的连续子序列的总数。
思路:
采用双指针 l ,r ,用map<ll,ll>vis数组做标记,没重复时使其vis[s[i]]值为0,否则为1,记录输入的数是否重复,首先 l=i=0,让r一直往右走直到有重复数出现,再让l向右走,l每移动一步可得到r-i个连续子序列,同时也要从数组删掉已用完的 vis[s[i]],l==r时,再重复操作。
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; map<ll,ll>vis; ll s[100010]; int main() { int n; cin>>n; for(int i=0;i<n;i++)cin>>s[i]; int l=0,r=0; ll ct=0; for(int i=0;i<n;i++) { while(vis[s[r]]==0&&r<n) { vis[s[r]]=1; r++; } ct+=r-i; vis.erase(s[i]); } cout<<ct<<endl; }

浙公网安备 33010602011771号