Submission #53107562


Source Code Expand

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

class segtree{
public:
	static const int N=1<<18;
	int dp[1<<19];
	segtree(){
		memset(dp,0,sizeof(dp));
	}
	void update(int k,int v){
		k+=N-1;
		dp[k]=v;
		while(k>0){
			k=(k-1)/2;
			dp[k]=max(dp[k*2+1],dp[k*2+2]);
		}
	}

	int query(int a,int b,int k=0,int l=0,int r=N){
		if(b<=l || r<=a)return -99999999;
		if(a<=l && r<=b)return dp[k];
		int mid=(l+r)/2;
		int vl=query(a,b,k*2+1,l,mid);
		int vr=query(a,b,k*2+2,mid,r);
		return max(vl,vr);
	}
};

int n,k;
int p[200005];
segtree seg,segm;

int main(void){
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&p[i]);
        p[i]--;
        seg.update(p[i],i);
        segm.update(p[i],-i);
    }
    int ans=9999999;
    for(int i=0;i<=n-k;i++){
        int v1=seg.query(i,i+k);
        int v2=-segm.query(i,i+k);
        ans=min(ans,v1-v2);
        //printf("%d %d\n",v1,v2);
    }
    printf("%d\n",ans);
	return 0;
}

Submission Info

Submission Time
Task D - Permutation Subsequence
User ryoissy
Language C++ 20 (Clang 16.0.6)
Score 425
Code Size 1071 Byte
Status AC
Exec Time 103 ms
Memory 8776 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 3
AC × 28
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 03_handmade_00.txt, 03_handmade_01.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 3 ms 7816 KiB
00_sample_01.txt AC 3 ms 7664 KiB
00_sample_02.txt AC 3 ms 7768 KiB
01_random_00.txt AC 33 ms 8388 KiB
01_random_01.txt AC 84 ms 8660 KiB
01_random_02.txt AC 32 ms 7956 KiB
01_random_03.txt AC 72 ms 8512 KiB
01_random_04.txt AC 79 ms 8608 KiB
01_random_05.txt AC 80 ms 8768 KiB
01_random_06.txt AC 78 ms 8456 KiB
01_random_07.txt AC 48 ms 8656 KiB
01_random_08.txt AC 35 ms 8592 KiB
01_random_09.txt AC 90 ms 8452 KiB
01_random_10.txt AC 15 ms 8024 KiB
01_random_11.txt AC 56 ms 8440 KiB
01_random_12.txt AC 13 ms 8036 KiB
01_random_13.txt AC 73 ms 8692 KiB
01_random_14.txt AC 19 ms 8100 KiB
01_random_15.txt AC 88 ms 8688 KiB
01_random_16.txt AC 35 ms 8652 KiB
01_random_17.txt AC 3 ms 7872 KiB
02_random2_00.txt AC 32 ms 8456 KiB
02_random2_01.txt AC 88 ms 8508 KiB
02_random2_02.txt AC 103 ms 8648 KiB
02_random2_03.txt AC 98 ms 8540 KiB
02_random2_04.txt AC 47 ms 8776 KiB
03_handmade_00.txt AC 82 ms 8512 KiB
03_handmade_01.txt AC 83 ms 8660 KiB