제출 #1215984

#제출 시각아이디문제언어결과실행 시간메모리
1215984sunflowerExam (eJOI20_exam)C++17
100 / 100
246 ms100468 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define SZ(x) ((int) (x).size())
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define debug(x) cout << "[" << #x << " = " << (x) << "]" << endl

#define left    __left
#define right   __right
#define prev    __prev
#define next    __next
#define fi      first
#define se      second

template <class X, class Y>
    bool maximize(X &x, Y y) {
        if (x < y) return x = y, true;
        else return false;
    }

template <class X, class Y>
    bool minimize(X &x, Y y) {
        if (x > y) return x = y, true;
        else return false;
    }

#define MAX_N 100'100
int a[MAX_N + 2], b[MAX_N + 2], lef[MAX_N + 2], rig[MAX_N + 2];
int n;

void prepare() {
    stack <int> st;
    FOR(i, 1, n) lef[i] = 0, rig[i] = n + 1;

    FOR(i, 1, n) {
        while (!st.empty() && a[st.top()] < a[i]) st.pop();
        if (!st.empty()) lef[i] = st.top() + 1;
        st.push(i);
    }
    while (!st.empty()) st.pop();

    FORD(i, n, 1) {
        while (!st.empty() && a[st.top()] < a[i]) st.pop();
        if (!st.empty()) rig[i] = st.top() - 1;
        st.push(i);
    }
}

namespace subtask1356 {
    bool check() {
        return (n <= 5000);
    }

    int dp[5050][5050];

    void solve() {
        memset(dp, 0, sizeof(dp));

        int ans = 0;
        FOR(j, 1, n) FOR(i, 1, n) {
            dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            if (a[i] == b[j] && lef[i] <= j && j <= rig[i]) {
                dp[i][j] = dp[i][j - 1] + 1;
            }

            maximize(ans, dp[i][j]);
        }

        cout << ans;
    }
};

namespace subtask2 {
    bool check() {
        FOR(i, 2, n) if (b[i] != b[1]) return false;
        return true;
    }

    void solve() {
        int ans = 0;
        bool have = false;
        int cnt = 0;
        FOR(i, 1, n) {
            if (a[i] <= b[i]) {
                ++cnt;
                have |= (a[i] == b[i]);
            } else {
                ans += have * cnt;
                have = false;
                cnt = 0;
            }
        }
        ans += have * cnt;

        cout << ans;
    }
};

struct BIT {
    int bit[MAX_N + 2];

    void update(int x, int v) {
        for (; x <= n; x += x & (-x)) maximize(bit[x], v);
    }

    int get(int x) {
        int ans = 0;
        for (; x > 0; x -= x & (-x)) maximize(ans, bit[x]);
        return ans;
    }
} fen;

namespace subtask4 {
    int dp[MAX_N + 2];
    int pos[2 * MAX_N + 2];

    void solve() {
        memset(pos, 0, sizeof(pos));
        FOR(i, 1, n) pos[a[i]] = i;

        FOR(j, 1, n) {
            int i = pos[b[j]];
            if (i == 0) continue;

            if (lef[i] <= j && j <= rig[i]) {
                dp[j] = fen.get(i) + 1;
                fen.update(i, dp[j]);
            }
        }

        cout << *max_element(dp + 1, dp + 1 + n);
    }
};

void compressed() {
    vector <int> v;
    FOR(i, 1, n) v.push_back(a[i]);
    FOR(i, 1, n) v.push_back(b[i]);

    sort(ALL(v));
    v.resize(unique(ALL(v)) - v.begin());
    FOR(i, 1, n) a[i] = lower_bound(ALL(v), a[i]) - v.begin() + 1;
    FOR(i, 1, n) b[i] = lower_bound(ALL(v), b[i]) - v.begin() + 1;
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    #define task "test"
    if (fopen(task".inp","r")) {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, n) cin >> b[i];

    compressed();
    prepare();

    if (subtask1356 :: check()) return subtask1356 :: solve(), 0;
    if (subtask2 :: check()) return subtask2 :: solve(), 0;
    subtask4 :: solve();

    return 0;
}

/* Discipline - Calm */

컴파일 시 표준 에러 (stderr) 메시지

exam.cpp: In function 'int main()':
exam.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
exam.cpp:157:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  157 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...