답안 #565627

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
565627 2022-05-21T07:48:02 Z 1zaid1 Election (BOI18_election) C++14
28 / 100
3000 ms 540 KB
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef long long ll;
const int M = 2e6 + 5, MOD = 1e9+7;

int res(string s) {
    int ans = 0;

    int a = 0, b = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'T') a++;
        if (s[i] == 'C') b++;

        if (a > b) {
            a--;
            s[i] = 0;
            ans++;
        }
    }

    a = 0;
    b = 0;
    for (int i = s.size()-1; i >= 0; i--) {
        if (s[i] == 'T') a++;
        if (s[i] == 'C') b++;

        if (a > b) {
            a--;
            s[i] = 0;
            ans++;
        }
    }

    return ans;
}

signed main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    srand(time(0));
    
    int n;
    cin >> n;

    string s;
    cin >> s;

    int t;
    cin >> t;

    while (t--) {
        int l, r;
        cin >> l >> r;

        cout << res(s.substr(l-1, r-l+1)) << endl;
    }

    return 0;
}

/*
11
CCCTTTTTTCC
3
1 11
4 9
1 6
*/

Compilation message

election.cpp: In function 'long long int res(std::string)':
election.cpp:12:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |     for (int i = 0; i < s.size(); i++) {
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 340 KB Output is correct
2 Correct 7 ms 340 KB Output is correct
3 Correct 7 ms 340 KB Output is correct
4 Correct 6 ms 352 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 340 KB Output is correct
2 Correct 7 ms 340 KB Output is correct
3 Correct 7 ms 340 KB Output is correct
4 Correct 6 ms 352 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Execution timed out 3072 ms 540 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 340 KB Output is correct
2 Correct 7 ms 340 KB Output is correct
3 Correct 7 ms 340 KB Output is correct
4 Correct 6 ms 352 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Execution timed out 3072 ms 540 KB Time limit exceeded
7 Halted 0 ms 0 KB -