zoukankan      html  css  js  c++  java
  • Codeforces Round #520 (Div. 2) A. A Prank

    A. A Prank

    time limit per test   1 second
    memory limit per test    256 megabytes

    题目链接https://codeforc.es/contest/1062/problem/A

    Description:

    JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a1, a2, ..., anan of integers, such that 1a1<a2<<an1031≤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][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 nn (1n100,1≤n≤100) — the number of elements in the array.

    The second line of the input contains nn integers aiai (1a1<a2<<an103,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.

    Sample Input:

    6
    1 3 4 5 6 9

    Sample Output:

    2

    题意:

    给出一个严格递增的数组,问有连续的多少个数在数组里面的位置是可以确定的。

    题解:

    通过模拟发现当ai - ai-1 = ai+1 - ai = 1时,位置时可以被确定的,然后模拟一下就好了。

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    const int N = 1005;
    int a[N],d[N];
    int n,ans=0;
    
    int main(){
        scanf("%d",&n);
        a[n+1]=1001;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]) , d[i]=a[i]-a[i-1];  
        d[n+1]=a[n+1]-a[n];
        int cnt = 0;
        for(int i=2;i<=n+1;i++){
            if(d[i]==d[i-1] &&d[i]==1){
                cnt++;
            }else{
                ans=max(ans,cnt);
                cnt=0;
            }
        }
        printf("%d",max(ans,cnt));
        return 0;
    }
  • 相关阅读:
    浅析 Java 中的 final 关键字
    谷歌Java编程风格指南
    分布式事务之两阶段提交协议(2PC)and 使用事件和消息队列实现分布式事务
    零和博弈与木桶定律
    Executors类创建四种常见线程池
    软件设计的原则&101个设计模式-2011年04月25日 陈皓
    编程中的命名设计那点事-陈皓
    从面向对象的设计模式看软件设计- 2013年02月01日 陈皓
    SQL语句
    分布式事务
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/9986256.html
Copyright © 2011-2022 走看看