Submission #1240531

#TimeUsernameProblemLanguageResultExecution timeMemory
1240531bncodero_o123Matching (CEOI11_mat)C++20
0 / 100
70 ms12612 KiB
#include <bits/stdc++.h>
#define ll long long
#define name "code"
using namespace std;

const int max_n= 1e6;
int n, m, p[max_n + 2], h[max_n + 2];
int pos[max_n + 2], a[max_n + 2], b[max_n + 2];
int kmp[max_n + 2];

void solve() {
    cin >> n >> m;
    for(int i= 1; i <= n; ++i) cin >> p[i], pos[p[i]]= i;
    for(int i= 1; i <= m; ++i) cin >> h[i];

    for(int i= 1; i < n; ++i) a[i]= pos[i + 1] > pos[i];
    for(int i= 1; i < m; ++i) b[i]= h[i + 1] > h[i];
    a[n]= 2;

    for(int i= 2, j= 0; i < n; ++i) {
        while(j && a[i] != a[j + 1]) j= kmp[j];
        kmp[i]= j += a[i] == a[j + 1];
    }

    int ans= 0;
    for(int i= 1, j= 0; i < m; ++i) {
        while(j && b[i] != a[j + 1]) j= kmp[j];
        j += b[i] == a[j + 1];
        ans += j == n - 1;
    }

    cout << ans << '\n';
}

signed main() {
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL);
    if(fopen(name".inp", "r")) {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }

    int T= 1;
    for(; T; --T) solve();

    return 0;
}

Compilation message (stderr)

mat.cpp: In function 'int main()':
mat.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mat.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen(name".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...
#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...