Category Archives: GoogleCodeJam

Google CodeJam – Problem B. Rank and File

https://code.google.com/codejam/contest/4304486/dashboard#s=p1&a=2

the idea is very simple, every items on the matrix will appear even time, therefore, for the row which is missing, all items should have odd times.

the code is following

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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
int main(int argc,char **argv) 
{ 
  int N;
  int array[3000];
  scanf("%d\n",&N);
 
  for(int i=0;i<N;i++)
  {
	  int T;
	  scanf("%d\n",&T);
	  memset(array,0,sizeof(int)*3000);
 
	  int temp;
	  for(int m=0;m<2*T-1;m++)
	  {
		  for(int n=0;n<T;n++)
		  {
			scanf("%d",&temp);
			array[temp]++;
		  }
	  }
 
	  printf("Case #%d: ",(i+1));
 
	  for(int m=0;m<3000;m++)
	  {
		  if(array[m]>0 && array[m]%2==1)
		  {
			  printf("%d ",m);
		  }
	  }
	  printf("\n");
  }
 
  return 0; 
}

Google CodeJam – Problem A. The Last Word

https://code.google.com/codejam/contest/4304486/dashboard#s=p0&a=2

This problem is very simple, use basic linked operation will be enough,

it only compare the head item and tail item.

if new item > head item, then new item will be inserted into head.

if new item <= head item, then it will be inserted int tail.

check following code.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
struct element
{
	char c;
	struct element *next;
};
 
int main(int argc,char **argv) 
{ 
	int N;
	char c;
	scanf("%d\n",&N);
 
	char array[2000];
 
	for(int i=0;i<N;i++)
	{
		memset(array,0,sizeof(char)*2000);
 
		scanf("%s\n",array);
 
 
		struct element *tail;
		struct element *head = (struct element *)malloc(sizeof(struct element));
		memset(head,0,sizeof(struct element));
		head->c = array[0];
		head->next = NULL;
		tail = head;
 
		for(int j=1;array[j]!='\0';j++)
		{
			if(array[j]>=head->c)
			{
				struct element *e = (struct element *)malloc(sizeof(struct element));
				e->c = array[j];
				e->next = head;
				head = e;
			}
			else
			{
				struct element *e = (struct element *)malloc(sizeof(struct element));
				e->c = array[j];
				e->next = NULL;
				tail->next = e;
				tail = e;
			}
		}
		printf("Case #%d: ",i+1);
		struct element *p = head;
		for(;;)
		{
			if(p!=NULL){
				printf("%c",p->c);
				p=p->next;
			}
			else
				break;
		}
		printf("\n");
 
	}
 
	return 0; 
}

Google CodeJam – Mushroom Monster

Question URL is https://code.google.com/codejam/contest/4224486/dashboard

This question could be become two sub question.
1. less mushroom to eat.
for array 10 5 15 5, what is the sum of decreased numbers?
10 is decreased to 5, the decreased number is (10-5).
5 raises to 15, it will be ignored.
15 is decreased to 5, the decreased number is (15-5)

the sum of all decreased numbers that is (10-5)+(15-5) will be the answer.

2. eating mushroom in static speed.

find the max gap of decreased numbers. (15-5) is the max speed in 10 seconds.
there for, if mushroom number on the dish is greater than (15-5), Kaylin only eats(15-5).

if mushroom number on the dish is less that (15-5), Kaylin will finish all of them and waiting for next refill.

Here is Clang code.

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
  int N;
  int L;
  int i,j,k;
  int array[MAXSIZE];
  int sum1 = 0;
  int sum2 = 0;
 
  scanf("%d\n",&N);
 
  for(i=0;i<N;i++)
  {
    scanf("%d",&L);
    memset(array,0,sizeof(int)*MAXSIZE);
    sum1 = 0;
    sum2 = 0;
    for(j = 0;j<L;j++){
      scanf("%d",&array[j]);
    }
 
    //start
    for(j = 0;j<L - 1;j++){
      if(array[j]>array[j + 1])
        sum1 += (array[j] - array[j + 1]); 
    }
 
    int max = 0;
 
    for(j = 0;j<L - 1;j++){
      if(array[j]>array[j + 1]){
        if(max<(array[j] - array[j + 1])){
          max = array[j] - array[j + 1];
        }
      }
    }
 
    for(j = 0;j<L - 1;j++){
      sum2 += (array[j]>max?max:array[j]); 
    }
 
    printf("Case #%d: %d %d\n",(i + 1),sum1,sum2);
  }