【kick start-1】Round C 2020 - Kick Start 2020

第一次成功参赛Kick Start,排名感人,后面会好的!!!

Problem

Avery has an array of N positive integers. The i-th integer of the array is Ai.

A contiguous subarray is an m-countdown if it is of length m and contains the integers m, m-1, m-2, …, 2, 1 in that order. For example, [3, 2, 1] is a 3-countdown.

Can you help Avery count the number of K-countdowns in her array?

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integers N and K. The second line contains N integers. The i-th integer is Ai.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of K-countdowns in her array.

Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ K ≤ N.
1 ≤ Ai ≤ 2 × 105, for all i.

Test set 1
2 ≤ N ≤ 1000.

Test set 2
2 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 2 ≤ N ≤ 1000.

Sample

Input

Output

3
12 3
1 2 3 7 9 3 2 1 8 3 2 1
4 2
101 100 99 98
9 6
100 7 6 5 4 3 2 1 100

Case #1: 2
Case #2: 0
Case #3: 1

In sample case #1, there are two 3-countdowns as highlighted below.
1 2 3 7 9 3 2 1 8 3 2 1
1 2 3 7 9 3 2 1 8 3 2 1

In sample case #2, there are no 2-countdowns.

In sample case #3, there is one 6-countdown as highlighted below.
100 7 6 5 4 3 2 1 100

discussion

  1. 判断一个序列里含有几个子序列
  2. 设置count变量表明已经有几位匹配了,temp表示模板子串中要匹配的值
  3. 我的算法复杂度是O(n),待匹配字符串不走回头路,就是滑动窗口的解法,在这里,我要再次安利Mr labuladong的项目,github链接,大家放肆戳!
  4. 这道题非常easy!!
  5. 一个可以debug的输入是:
    12 3
    1 2 3 7 3 3 2 1 8 3 2 1

code

import java.util.*;
import java.io.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
        for (int i = 1; i <= t; ++i) {
            int n = in.nextInt();
            int m = in.nextInt();
            if(m > n){
                System.out.println("Case #" + i + ": " + 0);
            } else {
                int temp = m, count = 0, res = 0;
                while(n-- > 0){
                    int val = in.nextInt();
                    if(val == m){
                        count = 1;
                        temp = m - 1;
                    } else if(val == temp){
                        ++count;
                        temp--;
                        if(count == m){
                            res++;
                            temp = m;
                            count = 0;
                        }
                    } else {
                        temp = m;
                        count = 0;
                    }
                }
                System.out.println("Case #" + i + ": " + res);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值