summaryrefslogtreecommitdiff
path: root/2017/5.c
blob: c7828169db892e6c2c6ba959cd767db7713190e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>


int main(void){
	freopen("5.in","r",stdin);

	int capjumps=128,numjumps=0;
	int *jumps=malloc(capjumps*sizeof(int));

	while(true){
		int target;
		scanf("%d",&target);
		if(feof(stdin))break;
		if(numjumps==capjumps){
			capjumps*=2;
			jumps=realloc(jumps,capjumps*sizeof(int));
		}
		jumps[numjumps++]=target;
	}

	int *jumps1=malloc(numjumps*sizeof(int));
	memcpy(jumps1,jumps,numjumps*sizeof(int));

	int at=0;
	int nsteps=0;
	while(0<=at&&at<numjumps){
		at+=jumps1[at]++;
		nsteps++;
	}
	printf("%d\n",nsteps);

	memcpy(jumps1,jumps,numjumps*sizeof(int));

	at=0;
	nsteps=0;
	while(0<=at&&at<numjumps){
		int next=at+jumps1[at];
		if(jumps1[at]>=3)jumps1[at]--;
		else jumps1[at]++;
		at=next;
		nsteps++;
	}
	printf("%d\n",nsteps);

	free(jumps1);
	free(jumps);
}