Submission #405154

# Submission time Handle Problem Language Result Execution time Memory
405154 2021-05-15T19:18:26 Z tsaraf Election (BOI18_election) C++17
28 / 100
3000 ms 1332 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
 
const int N = 500 + 3;
 
int n, Q;
string s;
map< pair< int, int >, int > memo;
 
void solve(){
    
    cin >> n >> s >> Q;
    while(Q--){
        int L, R; cin >> L >> R; --L; --R;
        if(memo.count({L, R})){
            cout << memo[{L, R}] << '\n';
            continue;
        }
        bool vis[n]; memset(vis, false, sizeof(vis));
        int sum = 0, ans = 0;;
        for(int i = L; i <= R; ++i){
            int vote = (s[i] == 'C' ? +1 : -1);
            if(sum + vote < 0){
                vis[i] = true;
                ++ans;
            } else {
                sum += vote;
            }
        }
        sum = 0;
        for(int i = R; i >= L; --i){
            if(!vis[i]){
                int vote = (s[i] == 'C' ? +1 : -1);
                if(sum + vote < 0){
                    vis[i] = true;
                    ++ans;
                } else {
                    sum += vote;
                }
            }
        }
        cout << ans << '\n';
        memo[{L, R}] = ans;
    }
    
}
 
 
int main(){
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif
 
    ios_base::sync_with_stdio(false); cin.tie(0);
 
    int t = 1;
    // cin >> t;
 
 
    for(int i = 1; i <= t; i++){
        // cout << "Case #" << i << ": ";
 
        solve();
 
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 332 KB Output is correct
2 Correct 8 ms 436 KB Output is correct
3 Correct 8 ms 332 KB Output is correct
4 Correct 8 ms 420 KB Output is correct
5 Correct 5 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 332 KB Output is correct
2 Correct 8 ms 436 KB Output is correct
3 Correct 8 ms 332 KB Output is correct
4 Correct 8 ms 420 KB Output is correct
5 Correct 5 ms 332 KB Output is correct
6 Execution timed out 3049 ms 1332 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 332 KB Output is correct
2 Correct 8 ms 436 KB Output is correct
3 Correct 8 ms 332 KB Output is correct
4 Correct 8 ms 420 KB Output is correct
5 Correct 5 ms 332 KB Output is correct
6 Execution timed out 3049 ms 1332 KB Time limit exceeded
7 Halted 0 ms 0 KB -