Kodkraft
Time limit2sMemory limit1024 MB
Given a yearly contest schedule over K divisions, find the shortest window containing each division 1 through K in order, counting only contests that occur between his first and final win.
- Level
Medium6 of 10
- Topics
- Sliding window, Two pointers, Implementation
- Solved
- No attempts yet
Problem
Nicolas wants to start competing in programming on the site kodkraft\texttrademark. There are many different divisions to compete in, but since Nicolas is a new participant on kodkraft™, he has to start in the lowest division (division 1). Nicolas's goal is to reach the highest division (division ) as quickly as possible and win a contest in it.
According to kodkraft™'s rules, you can only move up one division per contest, so he will have to compete in at least one contest in each division. However, Nicolas is very confident and therefore believes he will need to compete in exactly one contest in each division to move up to the next division. When a contest is held on kodkraft™, only one division competes at a time, and two contests never overlap in time. The contests also follow the same schedule every year.
Nicolas may begin his competing on kodkraft™ on whichever date of the year he wants. What Nicolas means by as quickly as possible is that as few contests as possible should take place on kodkraft™ (whether he participates in them or not) between the first contest he participates in and his first win in the highest division. Help Nicolas compute how many contests are required!
Input
The first line contains two integers and (), the number of contests per year and the number of divisions.
This is followed by a line with integers (), the schedule of contests during one year. is the division that competes in the -th contest after New Year's. Every division between and has at least one contest during the year.
Output
One integer, the smallest number of contests that need to take place on kodkraft™ from when he starts competing there until he has won division .