이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int maxn = 1e6+55;
const int inf = 1e9+55;
typedef pair <int,int> pii;
int a[maxn], b[maxn];
int cnt[maxn], n;
int pref[maxn], cntPref[maxn];
int brute()
{
int best = 0;
for (int i = 1; i <= n; i ++)
{
multiset <int> st;
for (int c = 0; c < 26; c ++) cnt[c] = 0;
int mx = 0;
for (int j = i; j <= n; j ++)
{
int c = a[j];
if (st.find(cnt[c]) != st.end()) st.erase(st.find(cnt[c]));
cnt[c] ++;
st.insert(cnt[c]);
int delta = abs(*(st.begin()) - *(--st.end()));
best = max(best, delta);
}
}
return best;
}
int cal(int c1, int c2)
{
vector <int> rem;
rem.push_back(0);
for (int i = 1; i <= n; i ++)
{
if (a[i] == c1)
rem.push_back(1);
else if (a[i] == c2)
rem.push_back(-1);
}
int m = rem.size() - 1;
for (int i = 1; i <= m; i ++) cntPref[i] = cntPref[i-1] + (rem[i] == -1);
for (int i = 1; i <= m; i ++) pref[i] = pref[i-1] + rem[i];
int j = 0, mn = inf, best = 0;
for (int i = 1; i <= m; i ++)
{
while (j < i && cntPref[i] - cntPref[j-1] > 0)
{
mn = min(mn, pref[j]);
j ++;
}
best = max(best, pref[i] - mn);
}
return best;
}
void process()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
char x;
cin >> x;
a[i] = x - 'a';
}
int best = 0;
for (int c1 = 0; c1 < 26; c1 ++) for (int c2 = 0; c2 < 26; c2 ++) if (c1 != c2)
{
best = max(best, cal(c1, c2));
// cout << c1 << " " << c2 << " " << best << endl;
}
// cout << brute() << endl;
cout << best;
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int ntest = 1;
while (ntest --) process();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
roz.cpp: In function 'int brute()':
roz.cpp:23:13: warning: unused variable 'mx' [-Wunused-variable]
23 | int mx = 0;
| ^~
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |