2020-2-16 A Prank

A Prank
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
6
1 3 4 5 6 9
Output
2
Input
3
998 999 1000
Output
2
Input
5
1 2 3 4 5
Output
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.

*先考虑一般再考虑特殊
一般:大多数数据的计算方法
特殊:题目所要求的 【连续发生的位置 以及 连续的数本身 影响连续区间长度最大值的判断】
算法本身的特殊性:这个非常固定

所有可能的连续情况:
头部按123连续
尾部997 998 999 1000连续(特殊)
其他的连续(一般)

我自己的AC代码:

#include <iostream>
using namespace std;
int main(void)
{
	int n;cin>>n;
	int i;
	int data[101];
	for(i = 1; i <= n; i++){
		cin>>data[i];
	} 
	
	int maxer = 0;
	int t1 = 1, t2 = 1;
	
	for(i = 2; i <= n; i++){
		t2 = i;
		if(data[i] - data[i-1] == 1){
			continue;
		}
		else{
			if(t2-t1 >= 2){
				if(t1 == 1){
					if(t2-1 == data[t2-1]) maxer = max(t2-2,maxer);
					else if(t2-t1 >= 3) maxer = max(t2-t1-2,maxer);					
				}
				else if(t2-t1 != 2) maxer = max(t2-t1-2,maxer);	

			}
			t1 = t2;
		}
	}
	
	if(t1 != t2) {
		if(n == data[n] || data[n] == 1000) maxer = max(t2-t1,maxer);
		else maxer = max(t2-t1-1,maxer);	
	}

	cout<<maxer<<endl;

	return 0;
} 

经过思考修改后的代码:

#include <iostream>
using namespace std;
int main(void)
{
	int n;cin>>n;
	int i;
	int data[101];
	for(i = 1; i <= n; i++){
		cin>>data[i];
	} 
	
	//一般 
	int maxer = 0;
	int b = 1;
	for(i = 2; i <= n+1; i++){
		if(i != n+1 && data[i] - data[i-1] == 1) continue;//到头了以后要断开连续性 
		else{
			if(i-b > 2) maxer = max(i-b-2,maxer);	
			b = i;
		}
	}
	
	//特殊
	int t = 0;
	if(data[1] == 1){
		for(i = 2; i <= n; i++){
			if(data[i] - data[i-1] == 1){
				t++;
				continue;
			}
			break; 
		}
	}
	maxer = max(maxer, t);
	t = 0;
	if(data[n] == 1000){
		for(i = n; i >= 2; i--){
			if(data[i] - data[i-1] == 1){
				t++;
				continue;
			}
			break; 
		}
	}
	maxer = max(maxer, t);
	
	cout<<maxer<<endl;

	return 0;
} 

这样思路更加地清晰~

*这里有必要对一般情况的代码进行分析(错了好多次,幸好有cf的测试数据。。捂脸)

	int maxer = 0;
	int b = 1;
	for(i = 2; i <= n+1; i++){
		if(i != n+1 && data[i] - data[i-1] == 1) continue;//到头了以后要断开连续性 
		else{
			if(i-b > 2) maxer = max(i-b-2,maxer);	
			b = i;
		}
	}

有编号为1,n的n个数据
i指向下一个被检测数据的编号
b用来保存连续区间开始的元素的编号
maxer保存最大长度
注意
i要扫描到n+1
当i为n+1时意味着扫描到达终点

再来看看dalao的代码
大题结构和我写的相同,但是人家的代码就是这么简洁 orz

#include<iostream> 
using namespace std;
int main(void)
{
	int a[200];
    int n;cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    
    int tmp=1, ans=0, m=0;
    int i;
    for(i = 1; i < n; i++){
        if(a[i] - a[i-1] == 1){
            tmp++;
            if(a[i-1] == 1 || a[i] == 1000) tmp++;//仅仅一行就解决了问题,妙哉妙哉
            if(i == n-1){
                ans = ans + max(tmp-2, 0);//这个小技巧需要学习一下
                m = max(m, ans);
            }
        }
		else{
            ans = ans + max(tmp-2, 0);
            tmp = 1;
            m = max(m, ans);
            ans = 0;
        }
    }
    cout<<m<<endl;;
}

经过比较与结合,我最终的代码是:

#include <iostream>
using namespace std;
int main(void)
{
	int n;cin>>n;
	int i;
	int data[101];
	for(i = 1; i <= n; i++) cin>>data[i];
	
	int maxer = 0;
	int len = 1;//因为关心的是长度,用长度来代替b可减少计算
	for(i = 2; i <= n+1; i++){
		if(i != n+1 && data[i] - data[i-1] == 1){
			len++;
			if(data[i-1] == 1 || data[i] == 1000)  len++;//保证了特殊情况的兼容
			continue;
		} 
		else{
			//len = max(len-2, 0);//lenbu是0就是正 可惜这里用不到。。。 
			maxer = max(len-2,maxer);
			len = 1;//一样需要归零
		}
	}
	cout<<maxer<<endl;
	return 0;
} 

hhhh这么辛苦整理的,一定要记得经常回顾吖(/ω\)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值