Submission #7641591


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

//! Range Minimum Query用 SegmentTree
class SegmentTreeMin
{
public:
    SegmentTreeMin(int n)
    {
        ll cnt = 1;
        while (cnt < n)
            cnt <<= 1;
        node.resize(2 * cnt - 1);
        REP(i, 2 * cnt - 1)
        {
            node[i] = inf;
        }
        mN = cnt - 1;
    }
    ~SegmentTreeMin()
    {
        node.resize(0);
    }

    //! 0-index get
    ll get(int left, int right, int l = 0, int r = -1, int index = 0)
    {
        if (r == -1)
            r = mN;

        if (r < left || right < l)
            return inf;
        if (left <= l && r <= right)
            return node[index];
        return std::min(get(left, right, l, (l + r) / 2, 2 * index + 1),
                        get(left, right, (l + r) / 2 + 1, r, 2 * index + 2));
    }

    //! 0-index set
    void set(int index, ll value)
    {
        node[mN + index] = value;
        //! update
        int par = (mN + index);
        while (par > 0)
        {
            par = (par - 1) / 2;
            node[par] = std::min(node[2 * par + 1], node[2 * par + 2]);
        }
    }

private:
    ll mN;
    vector<ll> node;
    const ll inf = (1LL << 31) - 1LL;
};

//! Range Minimum Query用 SegmentTree
class SegmentTreeMax
{
public:
    SegmentTreeMax(int n)
    {
        ll cnt = 1;
        while (cnt < n)
            cnt <<= 1;
        node.resize(2 * cnt - 1);
        REP(i, 2 * cnt - 1)
        {
            node[i] = -1;
        }
        mN = cnt - 1;
    }
    ~SegmentTreeMax()
    {
        node.resize(0);
    }

    //! 0-index get
    ll get(int left, int right, int l = 0, int r = -1, int index = 0)
    {
        if (r == -1)
            r = mN;

        if (r < left || right < l)
            return -1;
        if (left <= l && r <= right)
            return node[index];
        return std::max(get(left, right, l, (l + r) / 2, 2 * index + 1),
                        get(left, right, (l + r) / 2 + 1, r, 2 * index + 2));
    }

    //! 0-index set
    void set(int index, ll value)
    {
        node[mN + index] = value;
        //! update
        int par = (mN + index);
        while (par > 0)
        {
            par = (par - 1) / 2;
            node[par] = std::max(node[2 * par + 1], node[2 * par + 2]);
        }
    }

private:
    ll mN;
    vector<ll> node;
};

ll P[200005];
SegmentTreeMin segMin(200005);
SegmentTreeMax segMax(200005);
bool Q[200005]; //! そーとしても変わらないやつか

ll getMin(ll l, ll r)
{
    return segMin.get(l, r);
}
ll getMax(ll l, ll r)
{
    return segMax.get(l, r);
}
int main()
{
    ll N, K;
    cin >> N >> K;

    REP(i, N)
    {
        cin >> P[i];
        segMin.set(i, P[i]);
        segMax.set(i, P[i]);
    }
    int k = 1;
    memset(Q, 0, sizeof(Q));
    FOR(i, N, 1)
    {
        if (P[i] > P[i - 1])
        {
            k++;
            if (k >= K)
                Q[i - k + 1] = true;
        }
        else
        {
            k = 1;
        }
    }
    ll ans = Q[0] ? 0 : 1;
    FOR(i, N, 1)
    {
        if (Q[i])
            continue;
        if (i + K - 1 >= N)
            break;
        ll min = getMin(i - 1, i - 1 + K - 1);
        ll max = getMax(i, i + K - 1);
        //cout << min << " " << max << endl;
        if (P[i - 1] == min && P[i + K - 1] == max)
            continue;
        ans++;
    }
    REP(i, N)
    {
        if (Q[i])
        {
            ans++;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task B - Sorting a Segment
User coco18000
Language C++14 (GCC 5.4.1)
Score 700
Code Size 3924 Byte
Status AC
Exec Time 177 ms
Memory 10368 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 32
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt AC 5 ms 8576 KB
01-02.txt AC 5 ms 8704 KB
01-03.txt AC 135 ms 9856 KB
01-04.txt AC 124 ms 9984 KB
01-05.txt AC 67 ms 9472 KB
01-06.txt AC 103 ms 9728 KB
01-07.txt AC 139 ms 10240 KB
01-08.txt AC 144 ms 10240 KB
01-09.txt AC 136 ms 10240 KB
01-10.txt AC 140 ms 10240 KB
01-11.txt AC 158 ms 10368 KB
01-12.txt AC 164 ms 10240 KB
01-13.txt AC 162 ms 10240 KB
01-14.txt AC 169 ms 10240 KB
01-15.txt AC 177 ms 10240 KB
01-16.txt AC 165 ms 10240 KB
01-17.txt AC 124 ms 10240 KB
01-18.txt AC 133 ms 10240 KB
01-19.txt AC 137 ms 10240 KB
01-20.txt AC 145 ms 10240 KB
01-21.txt AC 124 ms 10240 KB
01-22.txt AC 105 ms 10240 KB
01-23.txt AC 115 ms 10240 KB
01-24.txt AC 116 ms 10240 KB
01-25.txt AC 82 ms 10240 KB
01-26.txt AC 87 ms 10240 KB
01-27.txt AC 82 ms 10240 KB
01-28.txt AC 81 ms 10240 KB
01-29.txt AC 81 ms 10240 KB
sample-01.txt AC 5 ms 8576 KB
sample-02.txt AC 5 ms 8576 KB
sample-03.txt AC 5 ms 8704 KB