답안 #434036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
434036 2021-06-20T14:21:28 Z HediChehaidar Election (BOI18_election) C++17
28 / 100
8 ms 716 KB
/*
ID: hedichehaidar
TASK: photo
LANG: C++11
*/
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd  pair<double,double>
#define vdd  vector<pdd>
#define dte  tuple<double , double , double>
using namespace std;
const int INF = 1000*1000*1000; // 1 e 9
const int MOD = INF + 7;
const double EPS = 0.000000001; // 1 e -9
const ll inf = (ll)1e18;

int n;
string s;
bool done[2020];
int main() {
    //ifstream fin ("race.in");
    //ofstream fout ("race.out");
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>s;
    int q; cin>>q;
    while(q--){
        int l , r; cin>>l>>r;
        memset(done , 0 , sizeof done);
        l--; r--;
        bool ok = false;
        for(int i = l ; i <= r; i++) if(s[i] == 'C') ok = true;
        if(!ok) {
            cout << r - l + 1 << "\n"; continue;
        }
        int ans = 0 , cur = 0;
        for(int i = l ; i <= r ; i++){
            if(s[i] == 'T') cur--;
            else if(s[i] == 'C') cur++;
            if(cur < 0){
                cur = 0;
                done[i] = true;
                ans++;
            }
        }
        cur = 0;
        for(int i = r; i >= l ; i--){
           // cout << i << " " << cur << "\n";
            if(s[i] == 'T' && !done[i]) cur--;
            else if(s[i] == 'C') cur++;
            if(cur < 0){
                cur = 0;
                done[i] = true;
                ans++;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}

/*
    Think of : BS / DFS / BFS / SSSP / SCC / MSP / MAX FLOW / TOPSORT / LCA / MATRIX / DP(bitmask) / 2 POINTERS / SEG TREE / MATH / UN FIND / MO
    Read the statement CAREFULLY !!
    Make a GREADY APPROACH !!!! (start from highest / lowest)
    Make your own TESTS !!
    Be careful from CORNER CASES !
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 204 KB Output is correct
2 Correct 8 ms 316 KB Output is correct
3 Correct 7 ms 204 KB Output is correct
4 Correct 8 ms 332 KB Output is correct
5 Correct 5 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 204 KB Output is correct
2 Correct 8 ms 316 KB Output is correct
3 Correct 7 ms 204 KB Output is correct
4 Correct 8 ms 332 KB Output is correct
5 Correct 5 ms 320 KB Output is correct
6 Runtime error 1 ms 716 KB Execution killed with signal 11
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 204 KB Output is correct
2 Correct 8 ms 316 KB Output is correct
3 Correct 7 ms 204 KB Output is correct
4 Correct 8 ms 332 KB Output is correct
5 Correct 5 ms 320 KB Output is correct
6 Runtime error 1 ms 716 KB Execution killed with signal 11
7 Halted 0 ms 0 KB -