CF_1883_F. You Are So Beautiful
题目链接:Problem - 1883F - Codeforces
题目大意:
给定数组a,统计有多少连续子数组b,满足b作为子序列在a中只出现一次(即仅有b自身作为连续子数组时才能匹配)。
思路:
子数组一定是要连续的,但子序列不用连续.

观察图片规律,不难发现,要满足条件,该子数组左边是该数字第一次出现,右边是该数字最后一次出现
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<tuple>
#include<array>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/numeric>
#define inf 72340172838076673
#define int long long
#define endl '\n'
#define F first
#define S second
#define mst(a,x) memset(a,x,sizeof (a))
#define gmap __gnu_pbds::gp_hash_table
#define power __gnu_cxx::power
using namespace std;
typedef pair<int, int> pii;
const int N = 200086, mod = 998244353;
int n, m;
int a[N];
void solve() {
cin >> n;
gmap<int, int> mp, cnt;
vector<int> f(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
mp[a[i]]++;
}
int res = 0;
for (int i = 1; i <= n; i++) {
f[i] = f[i - 1];
if (++cnt[a[i]] == 1) f[i]++;
if (cnt[a[i]] == mp[a[i]]) {
res += f[i];
}
}
cout << res << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) solve();
return 0;
}

浙公网安备 33010602011771号