# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
140148 | 2019-08-02T07:47:45 Z | asifthegreat | Difference (POI11_roz) | C++14 | 1000 ms | 8568 KB |
// 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); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 380 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 2 ms | 380 KB | Output is correct |
4 | Correct | 2 ms | 376 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 256 KB | Output is correct |
2 | Correct | 2 ms | 400 KB | Output is correct |
3 | Correct | 2 ms | 376 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 256 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
3 | Correct | 2 ms | 256 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 18 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 380 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 150 ms | 1016 KB | Output is correct |
2 | Correct | 2 ms | 400 KB | Output is correct |
3 | Correct | 19 ms | 504 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Execution timed out | 1053 ms | 7932 KB | Time limit exceeded |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Execution timed out | 1064 ms | 8340 KB | Time limit exceeded |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Execution timed out | 1037 ms | 8568 KB | Time limit exceeded |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Execution timed out | 1069 ms | 8008 KB | Time limit exceeded |
2 | Halted | 0 ms | 0 KB | - |