Particle

競技プログラミングについての雑記

AOJ 0517: Longest Steps

しゃくとり法で数えます。
0 2 3...n
のときに、変な動作をしますが、撃墜はされません(他にコーナーケースがあるかもしれないけど、ACはできました)

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

bool card[100020];
int n,k;

int main(){
	while(scanf("%d%d",&n,&k),n||k){
		memset(card,0,sizeof(card));
		for(int i = 0,a; i < k; i++){
			scanf("%d",&a);
			card[a] = true;
		}
		
		int a = 0,b,res = 1;
		
		while(!card[++a]);
		b = a;
		while(card[++b]);
		if(card[0])while(card[++b]);
		
		while(b<=n){
			res = max(res,b-a);
			while(card[++a]);
			while(a<n&&!card[++a]);
			b = a;
			while(b<=n&&card[++b]);
			if(card[0])while(b<=n&&card[++b]);
		}
		res = max(res,b-a);
		printf("%d\n",res);
	}
	return 0;
}