Submission #7641111
Source Code Expand
Copy
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
const int INF = 1e10;
const int MOD = 1e9 + 7;
#define dump(x) \
if (dbg) { \
cout << #x << " = " << (x) << endl; \
}
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define V vector<int>
#define S set<int>
#define itn int
bool dbg = 1;
int N, K, P[202020];
itn dp[202020];
//SegmentTree
//How to use
//SegmentTree(n, f, M1):= サイズ n の初期化。ここで f は2つの区間の要素をマージする二項演算, M1はモノイドの単位元である。
//set(k , x):= k 番目の要素に x を代入する。
//build():= セグメント木を構築する。
//query(a , b):= 区間 [a,b) に対して二項演算した結果を返す。
//update(k , x):= k 番目の要素を x に変更する。
//operator[k] : = k 番目の要素を返す。
template <typename Monoid>
struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1)
{
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid& x)
{
seg[k + sz] = x;
}
void build()
{
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid& x)
{
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b)
{
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int& k) const
{
return seg[k + sz];
}
};
//example
/*
int main() {
int N, Q;
scanf("%d %d", &N, &Q);
SegmentTree< int > seg(N, [](int a, int b) { return min(a, b); }, INT_MAX);
while(Q--) {
int T, X, Y;
scanf("%d %d %d", &T, &X, &Y);
if(T == 0) seg.update(X, Y);
else printf("%d\n", seg.query(X, Y + 1));
}
}
*/
void solve()
{
SegmentTree<int> MIN(N, [](itn a, int b) { return min(a, b); }, INF);
SegmentTree<int> MAX(N, [](itn a, int b) { return max(a, b); }, -1ll);
FOR(i, 0, N)
{
MIN.update(i, P[i]);
MAX.update(i, P[i]);
}
itn ans = 0;
/*
bool flag = true;
FOR(i, 0, K)
{
if (i != P[i])
flag = false;
}
if (flag)
ans++;
*/
ans++;
FORR(i, 1, N - K)
{
//前と違えばans++;
//同じならcontinue
// dump(i);
// cout << MIN.query(i - 1, i + K - 1) << " " << MIN.query(i, i + K) << endl;
// cout << MAX.query(i - 1, i + K - 1) << " " << MAX.query(i, i + K) << endl;
if (MIN.query(i - 1, i + K - 1) < MIN.query(i, i + K) && MAX.query(i - 1, i + K - 1) < MAX.query(i, i + K)) {
continue;
} else
ans++;
}
int t = 0;
dp[0] = 1;
FOR(i, 1, N)
{
if (P[i] > P[i - 1])
dp[i] = dp[i - 1] + 1;
else
dp[i] = 1;
if (dp[i] >= K)
t++;
}
ans -= max(t - 1, 0ll);
cout << ans << endl;
}
signed main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N >> K;
FOR(i, 0, N)
cin >> P[i];
solve();
return 0;
}
Submission Info
Submission Time |
|
Task |
B - Sorting a Segment |
User |
cuthbert |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
3875 Byte |
Status |
WA |
Exec Time |
111 ms |
Memory |
11648 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 700 |
Status |
|
|
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 |
1 ms |
256 KB |
01-02.txt |
AC |
1 ms |
256 KB |
01-03.txt |
AC |
67 ms |
11008 KB |
01-04.txt |
AC |
84 ms |
11136 KB |
01-05.txt |
AC |
40 ms |
6016 KB |
01-06.txt |
WA |
70 ms |
10624 KB |
01-07.txt |
WA |
59 ms |
11648 KB |
01-08.txt |
AC |
57 ms |
11648 KB |
01-09.txt |
WA |
56 ms |
11648 KB |
01-10.txt |
WA |
58 ms |
11648 KB |
01-11.txt |
AC |
65 ms |
11648 KB |
01-12.txt |
WA |
98 ms |
11648 KB |
01-13.txt |
AC |
89 ms |
11648 KB |
01-14.txt |
AC |
80 ms |
11648 KB |
01-15.txt |
AC |
111 ms |
11648 KB |
01-16.txt |
AC |
104 ms |
11648 KB |
01-17.txt |
AC |
66 ms |
11648 KB |
01-18.txt |
AC |
88 ms |
11648 KB |
01-19.txt |
AC |
85 ms |
11648 KB |
01-20.txt |
WA |
98 ms |
11648 KB |
01-21.txt |
WA |
82 ms |
11648 KB |
01-22.txt |
AC |
58 ms |
11648 KB |
01-23.txt |
AC |
75 ms |
11648 KB |
01-24.txt |
AC |
71 ms |
11648 KB |
01-25.txt |
WA |
48 ms |
11648 KB |
01-26.txt |
AC |
53 ms |
11648 KB |
01-27.txt |
AC |
49 ms |
11648 KB |
01-28.txt |
AC |
47 ms |
11648 KB |
01-29.txt |
AC |
47 ms |
11648 KB |
sample-01.txt |
AC |
1 ms |
256 KB |
sample-02.txt |
AC |
1 ms |
256 KB |
sample-03.txt |
AC |
1 ms |
256 KB |