답안 #759736

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
759736 2023-06-16T17:16:08 Z vjudge1 Election (BOI18_election) C++17
28 / 100
3000 ms 532 KB
#include<bits/stdc++.h>
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<int, int>, null_type, less<pair<int, int>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
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)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#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 speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////

void usaco_problem(){
    freopen("milkvisits.in", "r", stdin);
    freopen("milkvisits.out", "w", stdout);
}

void init(){
    #ifndef ONLINE_JUDGE
 
freopen("input.txt", "r", stdin);
 
freopen("output.txt", "w", stdout);
 
#endif // ONLINE_JUDGE
}

const int mx = 105;
const int LOG = 25;
const ll inf = 1e15;
const ll mod = 1e9+7;


void run_case(){
    int n;cin >> n;
    string s;cin >> s;
    int q;cin >> q;
    while(q--){
        int l, r;cin >> l >> r;l--;r--;
        int pf = 0;
        string x = s;
        int ans = 0;
        for(int i = l; i <= r;){
            while(i <= r && x[i] == 'C'){
                pf++;
                i++;
            }
            if(i > r)break;
            int ind = i;
            while(i <= r && x[i] == 'T'){
                pf--;
                i++;
            }
            if(pf < 0){
                int c = abs(pf);
                while(c--){
                    x[ind] = 'N';
                    ans++;
                    ind++;
                }
                pf = 0;
            }
        }
        pf = 0;
        for(int i = r; i >= l;){
            while(i >= l && x[i] == 'N'){
                i--;
            }
            if(i < l)break;
            while(i >= l && x[i] == 'C'){
                pf++;
                i--;
            }
            if(i < l)break;
            while(i >= l && x[i] == 'N'){
                i--;
            }
            if(i < l)break;
            int ind = i;
            while(i >= l && x[i] == 'T'){
                pf--;
                i--;
            }
            if(pf < 0){
                //cout << i+1 << endl;
                int c = abs(pf);
                while(c--){
                    x[ind] = 'N';
                    ans++;
                    ind--;
                }
                pf = 0;
            }
        }
        cout << ans << endl;
    }
}

int main(){
    speed;
    int t;
    //cin >> t;
    t = 1;
    while(t--){
        run_case();
    }
}

/*
    NEVER GIVE UP!
    DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
    Your Guide when stuck:
    - Continue keyword only after reading the whole input
    - Don't use memset with testcases
    - Check for corner cases(n=1, n=0)
    - Check where you declare n(Be careful of declaring it globally and in main)
*/

Compilation message

election.cpp: In function 'void usaco_problem()':
election.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen("milkvisits.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |     freopen("milkvisits.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp: In function 'void init()':
election.cpp:40:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:42:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 | freopen("output.txt", "w", stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 212 KB Output is correct
2 Correct 5 ms 212 KB Output is correct
3 Correct 4 ms 328 KB Output is correct
4 Correct 5 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 212 KB Output is correct
2 Correct 5 ms 212 KB Output is correct
3 Correct 4 ms 328 KB Output is correct
4 Correct 5 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
6 Execution timed out 3059 ms 532 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 212 KB Output is correct
2 Correct 5 ms 212 KB Output is correct
3 Correct 4 ms 328 KB Output is correct
4 Correct 5 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
6 Execution timed out 3059 ms 532 KB Time limit exceeded
7 Halted 0 ms 0 KB -