# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
140148 | asifthegreat | 새로운 문제 (POI11_roz) | C++14 | 1069 ms | 8568 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// POI 18-2, Difference
// O(N * A + A^2) where A is the alphabet size
#include <bits/stdc++.h>
using namespace std;
const int NAX = 1e6 + 5;
char s[NAX];
vector<int> occurrences[26]; // occurrences[x] - indices where letter 'x' occurs
int consider(const vector<int>& A, const vector<int>& B) {
if(A.empty() || B.empty()) {
return 0;
}
// merge the two lists/vectors into one with -1, +1
vector<int> seq;
int pointer2 = 0;
for(int i : A) {
while(pointer2 < (int) B.size() && B[pointer2] < i) {
seq.push_back(-1); // indices from B are changed to -1
++pointer2;
}
seq.push_back(1); // indices from A are changed to +1
}
while(pointer2 < (int) B.size()) {
seq.push_back(-1);
++pointer2;
}
//~ for(int x : seq) {
//~ printf("%d ", x);
//~ }
//~ puts("");
// find the maximum sum of a subarray that contains at least one -1
const int n = seq.size();
vector<int> pref{0};
for(int x : seq) {
pref.push_back(pref.back() + x); // prefix sums
}
vector<int> pref_min(n + 1);
for(int i = 1; i <= n; ++i) {
pref_min[i] = min(pref_min[i-1], pref[i]); // minima of prefix sums
}
// for(int i = 1; i <= n;i++)cout << pref[i] << " ";cout << endl;
// for(int i = 1; i <= n;i++)cout << pref_min[i] << " ";cout << endl;
//~ for(int x : pref) {
//~ printf("%d ", x);
//~ }
//~ puts("");
int answer = 0;
int last_negative = -1; // fake value, meaning there were no negative values so far
for(int i = 0; i < n; ++i) {
if(seq[i] == -1) {
last_negative = i;
}
if(last_negative != -1) {
answer = max(answer, pref[i+1] - pref_min[last_negative]);
}
}
return answer;
}
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
assert(n == (int) strlen(s));
for(int i = 0; i < n; ++i) {
occurrences[s[i]-'a'].push_back(i);
}
int answer = 0;
// int answer = consider(occurrences[24], occurrences[7]);
// cout << answer << endl;
for(int a = 0; a < 26; ++a) {
for(int b = 0; b < 26; ++b) {
if(a != b) {
// if(consider(occurrences[a], occurrences[b]) == 1){
// cout << (char)('a'+a) <<" " << (char)('a'+b) << " " << a << " " << b << endl;
// }
answer = max(answer, consider(occurrences[a], occurrences[b]));
}
}
}
printf("%d\n", answer);
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |