Submission #617750

# Submission time Handle Problem Language Result Execution time Memory
617750 2022-08-01T13:25:48 Z Blagojce Election (BOI18_election) C++11
0 / 100
7 ms 340 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <string.h>
#include <bitset>
#include <numeric>
#include <utility>

#define fr(i, n, m) for(int i = (n); i <= (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int mxn = 2e5;
const ll mod = 998244353;
const ll inf = 1e18;

int n;
string s;


int pref[mxn];
int suff[mxn];



pair<int,int> q_prefix(int l, int r){
    int mi = 0;
    int id = l-1;
    for(int i = l; i <= r; i ++){
        if(pref[i] - pref[l-1] < mi){
            mi = pref[i] - pref[l-1];
            id = i;
        }
    }
    return {abs(mi),id};
}

pair<int,int> q_suffix(int l, int r){
    int mi = 0;
    int id = r+1;
    for(int i = r; i >= l; i --){
        if(suff[i] - suff[r+1] < mi){
            mi = suff[i] - suff[r+1];
            id = i;
        }
    }
    return {abs(mi), id};
}


int solve(int l, int r){
    int RET = 0;
    
    pii A = q_prefix(l, r);
    pii B = q_suffix(l, r);
    
    if(A.st == 0) return B.st;
    if(B.st == 0) return A.st;
    
    
    pii C = q_suffix(A.nd+1, r);
    RET += C.st;
    
    pii D = q_suffix(l, A.nd);
    pii E = q_prefix(l, D.nd-1);
    RET += E.st;
    
    
    int REM1 = A.st - E.st;
    
    
    int new_sum = suff[A.nd+1] - suff[r+1] + C.st;
    
    int REM2 = D.st - new_sum;
    
    
    RET += max({0, REM1, REM2});
    
    
    
    return RET;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    cin >> s;
    
    
    for(int i = 1; i <= n; i ++){
        pref[i] = pref[i-1];
        if(s[i-1] == 'C') pref[i] += 1;
        if(s[i-1] == 'T') pref[i] -= 1;
    }
    for(int i = n; i >= 1; i --){
        suff[i] = suff[i+1];
        if(s[i-1] == 'C') suff[i] += 1;
        if(s[i-1] == 'T') suff[i] -= 1;
    }
    int Q;
    cin >> Q;
    fr(i, 1, Q){
        int l, r;
        cin >> l >> r;
        cout<<solve(l, r)<<endl;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -