Continuous Intervals Gym - 102222L(2018宁夏邀请赛暨2019银川icpc网络预选赛)
Lamis is a smart girl. She is interested in problems about sequences and their intervals.
Here she shows you a sequence of length nn with positive integers, denoted by a1,a2,a3,?,ana1,a2,a3,?,an. She is amazed at those intervals, which is a consecutive subsequence of a1,a2,?,ana1,a2,?,an, with several continuous numbers, and names them continuous intervals.
More precisely, consider a interval al,al+1,?,ar?1,aral,al+1,?,ar?1,ar for instance where 1≤l≤r≤n1≤l≤r≤n. If, after sorting the interval, the difference of any two neighbouring items is less than or equal to 11, the interval will be considered as continuous.
As her best friends, you came from far and wide and travelled thousands of miles to Ningxia, to help her count the number of continuous intervals of the sequence.
Input
The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 10001000.
For each test case, the first line contains an integer n (1≤n≤105)n (1≤n≤105) which is the length of the given sequence. The second line contains nn integers describing all items of the sequence, where the ii-th one is denoted by ai (1≤ai≤109)ai (1≤ai≤109).
We guarantee that the sum of nn in all test cases is up to 106106.
Output
For each test case, output a line containing Case #x: y, where x is the test case number starting from 11, and y is the number of continuous intervals in this test case.
Example
Input
2
4
1 2 1 2
4
1 3 2 4
Output
Case #1: 10
Case #2: 8
題意:找出區(qū)間max-區(qū)間min+1區(qū)間數(shù)字種類(lèi)數(shù)的區(qū)間個(gè)數(shù)。
找出max-min+1=cnt的區(qū)間個(gè)數(shù),就是求max-min-cnt-1的區(qū)間個(gè)數(shù)。這樣就轉(zhuǎn)換成了求區(qū)間max-min-cnt最小的問(wèn)題。并且要維護(hù)最小值的個(gè)數(shù)。對(duì)于區(qū)間最大值,我們可以用單調(diào)棧維護(hù),并且記錄下來(lái)他們的位置。對(duì)于區(qū)間最小值也是一樣,也是用單調(diào)炸維護(hù)。對(duì)于求區(qū)間個(gè)數(shù),就是普通操作。具體解釋看代碼。
代碼如下:
努力加油a啊,(o)/~
總結(jié)
以上是生活随笔為你收集整理的Continuous Intervals Gym - 102222L(2018宁夏邀请赛暨2019银川icpc网络预选赛)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Factories Gym - 1022
- 下一篇: Greedy Sequence(2019