• Codeforces1062A. A Prank(暴力)


    题目链接:传送门

    题目:

    A. A Prank
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output
    
    JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a1
    , a2, ..., an of integers, such that 1≤a1<a2<…<an≤103
    
    , and then went to the bathroom.
    
    JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1,103]
    
    .
    
    JATC wonders what is the greatest number of elements he can erase?
    Input
    
    The first line of the input contains a single integer n
    (1≤n≤100
    
    ) — the number of elements in the array.
    
    The second line of the input contains n
    integers ai (1≤a1<a2<⋯<an≤103
    
    ) — the array written by Giraffe.
    Output
    
    Print a single integer — the maximum number of consecutive elements in the array that JATC can erase.
    
    If it is impossible to erase even a single element, print 0
    
    .
    Examples
    Input
    Copy
    
    6
    1 3 4 5 6 9
    
    Output
    Copy
    
    2
    
    Input
    Copy
    
    3
    998 999 1000
    
    Output
    Copy
    
    2
    
    Input
    Copy
    
    5
    1 2 3 4 5
    
    Output
    Copy
    
    4
    
    Note
    
    In the first example, JATC can erase the third and fourth elements, leaving the array [1,3,_,_,6,9]
    
    . As you can see, there is only one way to fill in the blanks.
    
    In the second example, JATC can erase the second and the third elements. The array will become [998,_,_]
    . Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2
    
    elements.
    
    In the third example, JATC can erase the first 4
    elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
    View Code

    题目大意:

      一个长度为n(1 ≤ n ≤ 100)的,在1-1000范围内的严格递增序列a[n]。

      问最多在序列里删掉多少个元素,可以使得填回去的方法唯一(要求仍为严格递增序列)。

      比如123,就可以删掉2变成1_3,要求填进去的数比1大比3小,那么只能填2。

    思路:

      直接跑一边数组,求最长的连续整数序列的长度就好了。但是要注意细节

      ①:n=1时,不能删去,答案为0。。好像有很多人fst在了这个上,据说输出了负值?。

      ②:a1 = 1,a2 = 2时,a1可以删去,而a1 = 2,a2 = 3时不能删去a1

      ③:an-1 = 999,an = 1000时,an可以删去,而an-1 = 998,an = 999时不能删去an

      对于①,初始化答案为0,不断求最大的答案就可以;

      对于②③,不妨令a[0] = 0,a[n+1] = 1001,就可以直接求最长的连续序列长度了,答案为长度-2。

    代码:

    #include <bits/stdc++.h>
    
    using namespace std;
    const int MAX_N = 1e2 + 5;
    
    int a[MAX_N];
    
    int main()
    {
        int N;
        cin >> N;
        for (int i = 1; i <= N; i++) {
            scanf("%d", a+i);
        }
        a[++N] = 1001;
        int pre = 0;
        int len = 1;
        int ans = 0;
        for (int i = 1; i <= N; i++) {
            if (a[i] == pre+1) {
                len++;
                pre++;
            }
            else {
                ans = max(ans, len-2);
                len = 1;
                pre = a[i];
            }
        }
        ans = max(ans, len-2);
        cout << ans << endl;
        return 0;
    }
    View Code
  • 相关阅读:
    几个不同的关键XPath概念
    go get 下载的包放在哪里呢?
    之前写的关于chromedp的文章被别人转到CSDN,很受鼓励,再来一篇golang爬虫实例
    微信小程序填坑之旅(2)-wx.showLoading的时候,仍能点击穿透,造成重复点击button的问题
    微信小程序填坑之旅(1)-app.js中用云开发获取openid,在其他页上用app.globaldata.openid获取为空
    JS 定时器-setInterval、clearInterval、setTimeout
    微信小程序开发入门教程(四)---自己动手做个小程序
    MT【247】恒成立画图像
    MT【246】方程根$acksim$图像交点
    MT【245】小概率事件
  • 原文地址:https://www.cnblogs.com/Lubixiaosi-Zhaocao/p/9964071.html
Copyright © 2020-2023  润新知