#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
const int N = 200005;
vector<int> big;
int ans = 0, n, a[N];
map<int, int> mp;
map<int, bool> mark;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
mp[a[i]]++;
}
int T = sqrt(n);
for (auto &[x, y] : mp) {
if (y >= T) big.push_back(x);
else mark[x] = true;
}
for (int x : big) {
ordered_set s;
int sum = 0;
s.insert({0, -1});
for (int i = 0; i < n; i++) {
sum += (a[i] == x ? 1 : -1);
ans += s.order_of_key({sum, INT_MIN});
s.insert({sum, i});
}
}
for (int i = 0; i < n; i++) {
map<int, int> freq;
int maxFreq = 0, majorityElement = 0;
for (int j = i; j < n; j++) {
freq[a[j]]++;
if (freq[a[j]] > maxFreq) {
maxFreq = freq[a[j]];
majorityElement = a[j];
}
if (maxFreq > (j - i + 1) / 2 && mark[majorityElement]) {
ans++;
}
}
}
printf("%d\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:22:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |