题目链接:传送门

题目:

A. A Prank
time limit per test
second
memory limit per test
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 ≤a1<a2<…<an≤ , 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
(≤n≤ ) — the number of elements in the array. The second line of the input contains n
integers ai (≤a1<a2<⋯<an≤ ) — 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 .
Examples
Input
Copy Output
Copy Input
Copy Output
Copy Input
Copy Output
Copy Note In the first example, JATC can erase the third and fourth elements, leaving the array [,,_,_,,] . 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 [,_,_]
. Because all the elements are less than or equal to , 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
elements. Since all the elements are greater than or equal to , Giraffe can still restore the array. Note, that he can't erase the last 4 elements.

题目大意:

  一个长度为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 + ; int a[MAX_N]; int main()
{
int N;
cin >> N;
for (int i = ; i <= N; i++) {
scanf("%d", a+i);
}
a[++N] = ;
int pre = ;
int len = ;
int ans = ;
for (int i = ; i <= N; i++) {
if (a[i] == pre+) {
len++;
pre++;
}
else {
ans = max(ans, len-);
len = ;
pre = a[i];
}
}
ans = max(ans, len-);
cout << ans << endl;
return ;
}

最新文章

  1. PL/SQL数据库,Oracle登录
  2. Android UiAutomator快速调试
  3. LINQ的All的方法
  4. POJ1002 487-3279
  5. 数组名取地址所算数运算应注意的&amp;quot;trap&amp;quot;
  6. mock.js-无需等待,随机产生数据,让前端独立于后端进行开发
  7. zabbix如何监控进程
  8. C语言总结报告
  9. pandas.read_csv参数详解
  10. JDK10都发布了,nio你了解多少?
  11. redis 安装时候遇到 jemalloc 问题记录
  12. batch 常用命令
  13. [AI]SKLearn章1 快速入门
  14. 基于 Vue + Koa2 + MongoDB + Redis 实现一个完整的登录注册
  15. Python——Django-应用的models.py内容
  16. 1.1.8 怎样在Word的页眉中插入一级标题
  17. Java学习笔记36(File类)
  18. Mysql中的锁机制
  19. 测试开发之前端——No9.HTML5中的视频/音频
  20. P2376 [USACO09OCT]津贴Allowance

热门文章

  1. Ansible 的安装
  2. python第一阶段总结(2)
  3. Linux 软连接 (ln命令)
  4. Vue:(一)概况
  5. Rancher 容器管理平台-免费视频培训-链接及内容-第三季
  6. 使用Rancher的RKE快速部署Kubernetes集群
  7. inline-block间隙问题总结, ,style一个样式后面 多加了一个 分号; 导致 样式失效
  8. 力扣(LeetCode)804. 唯一摩尔斯密码词
  9. [Hibernate] 分页查询
  10. Multivariate normal distribution | 多元正态分布