Submission #227788

#TimeUsernameProblemLanguageResultExecution timeMemory
227788dolphingarlicMatching (CEOI11_mat)C++14
81 / 100
2059 ms65540 KiB
#include <bits/stdc++.h>
using namespace std;

int p[1000001], h[1000001], l_pred[1000001], l_succ[1000001], partial[1000001];
vector<int> matches;

inline bool can_extend(int i, int j, int *v) {
    return ((!l_pred[j + 1] || v[i - l_pred[j + 1]] < v[i]) && (!l_succ[j + 1] || v[i] < v[i - l_succ[j + 1]]));
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        p[x - 1] = i;
    }
    for (int i = 0; i < m; i++) scanf("%d", h + i);

    set<pair<int, int>> l;
    for (int i = 0; i < n; i++) l.insert({p[i], i});
    for (int i = n - 1; ~i; i--) {
        auto x = l.find({p[i], i});
        l_pred[i] = i - (x == l.begin() ? i : (*prev(x)).second);
        l_succ[i] = i - (x == prev(l.end()) ? i : (*next(x)).second);
        l.erase(x);
    }

    for (int i = 1, j = partial[0] = -1; i < n; i++) {
        while (~j && !can_extend(i, j, p)) j = partial[j];
        if (can_extend(i, j, p)) j++;
        partial[i] = j;
    }

    for (int i = 0, j = -1; i < m; i++) {
        while (~j && !can_extend(i, j, h)) j = partial[j];
        if (can_extend(i, j, h)) j++;
        if (j == n - 1) {
            matches.push_back(i - n + 2);
            j = partial[j];
        }
    }

    printf("%d\n", matches.size());
    for (int i : matches) printf("%d ", i);
    printf("\n");
    return 0;
}

Compilation message (stderr)

mat.cpp: In function 'int main()':
mat.cpp:45:34: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type {aka long unsigned int}' [-Wformat=]
     printf("%d\n", matches.size());
                    ~~~~~~~~~~~~~~^
mat.cpp:13:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
mat.cpp:16:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &x);
         ~~~~~^~~~~~~~~~
mat.cpp:19:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 0; i < m; i++) scanf("%d", h + i);
                                 ~~~~~^~~~~~~~~~~~~
#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...