답안 #963061

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963061 2024-04-14T12:40:03 Z RaresFelix Chorus (JOI23_chorus) C++17
16 / 100
1 ms 464 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC target("avx,avx2,fma")

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ld = long double;  // or double, if TL is tight
using str = string; 
using ii = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;

const ll INF = 1e18;

struct CHT {
    int p = 0;
    vector<pair<ll, ll> > V;
    void insert(ll a, ll b) { /// ax + b
        //a va scadea mereu
        while(V.size() > 1) {
            auto [a1, b1] = V.end()[-1];
            auto [a2, b2] = V.end()[-2];
            if((b2 - b) * (a1 - a2) <= (b2 - b1) * (a - a2)) {
                V.pop_back();
                p = min(p, int(V.size()) - 1);
            }
            else break;
        }
        V.push_back({a, b});
    }

    ll query(ll x) {
        if(V.empty()) return INF;
        ll re = INF;
        auto eval = [&](int i) -> ll {
            return V[i].first * x + V[i].second;
        };
        while(p + 1 < V.size() && eval(p + 1) < eval(p)) ++p;
        return min(eval(p), INF);
    }
};

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    int n, k;
    cin >> n >> k;

    string s0, s;
    cin >> s0;
    ll re0 = 0;
    int nrB = 0, sum = 0;
    for(auto it : s0) {
        if(it == 'A') {
            ++sum;
            s += 'A';
            re0 += nrB;
            if(nrB && sum) {
                s += 'B';
                --nrB;
                --sum;
            }
        } else {
            if(sum) {
                s += 'B';
                --sum;
            } else {
                ++nrB;
            }
        }
    }

    vll A, P(n + 1);
    vll SA;
    P[0] = -1;
    for(auto it : s) {
        if(it == 'A'){
            A.push_back(nrB);
            if(SA.empty())
                SA.push_back(A.back());
            else
                SA.push_back(A.back() + SA.back());
        } 
        else {
            ++nrB;
            P[nrB] = (int)A.size() - 1;
        }
    }
    vll SAP(n);
    for(int i = 0; i < n; ++i) {
        SAP[i] = 0;
        if(P[i] != -1) SAP[i] = SA[P[i]];
    }

    auto cost = [&](ll st, ll dr) {
        return st * P[st] - SAP[st] + SA[dr] - st * dr;
    };


    auto t = [&](ll lambda) {
        ll re = - lambda * k;
        vll DP(n, INF);

        CHT Sol;

        for(int i = 0; i < n; ++i) {
            DP[i] = cost(0, i) + lambda;
            if(i)
                Sol.insert(-i, i * P[i] - SAP[i] + DP[i - 1]);
            DP[i] = min(DP[i], lambda + SA[i] + Sol.query(i));
        }

        return DP[n - 1] + re;
    };

    ll st = 0, dr = INF, mij;

    ll re = 0;
    for(int i = 0; i <= 100; ++i) {
        ll m1 = (2 * st + dr) / 3, v1 = t(m1);
        ll m2 = (st + 2 * dr) / 3, v2 = t(m2);
        re = max(re, v1);
        re = max(re, v2);
        if(v1 > v2) dr = m2;
        else st = m1;
    }


    cout << re0 + re << "\n";
    return 0;
}

Compilation message

chorus.cpp: In member function 'll CHT::query(ll)':
chorus.cpp:43:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         while(p + 1 < V.size() && eval(p + 1) < eval(p)) ++p;
      |               ~~~~~~^~~~~~~~~~
chorus.cpp:39:12: warning: unused variable 're' [-Wunused-variable]
   39 |         ll re = INF;
      |            ^~
chorus.cpp: In function 'int main()':
chorus.cpp:122:26: warning: unused variable 'mij' [-Wunused-variable]
  122 |     ll st = 0, dr = INF, mij;
      |                          ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 360 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 456 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 360 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 456 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Incorrect 1 ms 344 KB Output isn't correct
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 360 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 456 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Incorrect 1 ms 344 KB Output isn't correct
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 360 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 456 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Incorrect 1 ms 344 KB Output isn't correct
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 360 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 0 ms 464 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 456 KB Output is correct
9 Correct 0 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Incorrect 1 ms 344 KB Output isn't correct
18 Halted 0 ms 0 KB -