Google Code Jam 2013: Round 1B Problam A

Armin is playing Osmos, a physics-based puzzle game developed by Hemisphere Games. In this game, he plays a “mote”, moving around and absorbing smaller motes.

A “mote” in English is a small particle. In this game, it’s a thing that absorbs (or is absorbed by) other things! The game in this problem has a similar idea to Osmos, but does not assume you have played the game.

When Armin’s mote absorbs a smaller mote, his mote becomes bigger by the smaller mote’s size. Now that it’s bigger, it might be able to absorb even more motes. For example: suppose Armin’s mote has size 10, and there are other motes of sizes 9, 13 and 19. At the start, Armin’s mote can only absorb the mote of size 9. When it absorbs that, it will have size 19. Then it can only absorb the mote of size 13. When it absorbs that, it’ll have size 32. Now Armin’s mote can absorb the last mote.

Note that Armin’s mote can absorb another mote if and only if the other mote is smaller. If the other mote is the same size as his, his mote can’t absorb it.

You are responsible for the program that creates motes for Armin to absorb. The program has already created some motes, of various sizes, and has created Armin’s mote. Unfortunately, given his mote’s size and the list of other motes, it’s possible that there’s no way for Armin’s mote to absorb them all.

You want to fix that. There are two kinds of operations you can perform, in any order, any number of times: you can add a mote of any positive integer size to the game, or you can remove any one of the existing motes. What is the minimum number of times you can perform those operations in order to make it possible for Armin’s mote to absorb every other mote?

For example, suppose Armin’s mote is of size 10 and the other motes are of sizes [9, 20, 25, 100]. This game isn’t currently solvable, but by adding a mote of size 3 and removing the mote of size 100, you can make it solvable in only 2 operations. The answer here is 2.


The first line of the input gives the number of test cases, T. T test cases follow. The first line of each test case gives the size of Armin’s mote, A, and the number of other motes, N. The second line contains the N sizes of the other motes. All the mote sizes given will be integers.


For each test case, output one line containing “Case #x: y”, where x is the case number (starting from 1) and y is the minimum number of operations needed to make the game solvable.


1 ≤ T ≤ 100.
Small dataset

1 ≤ A ≤ 100.
1 ≤ all mote sizes ≤ 100.
1 ≤ N ≤ 10.

Large dataset

1 ≤ A ≤ 106.
1 ≤ all mote sizes ≤ 106.
1 ≤ N ≤ 100.

Sample Input

2 2
2 1
2 4
2 1 1 6
10 4
25 20 9 100
1 4
1 1 1 1

Sample Output

Case #1: 0
Case #2: 1
Case #3: 2
Case #4: 4

My Solution

A variation of the knapsack problem, so I used a dynamic programming approach to solve, with a table for memoization of the results. It solved both problem sets correctly.

#include <stdio.h>
#include <stdlib.h>

int table[101][1000001];

int motes[101];
int a,n;

int min(int a, int b){
  if (a<b)
    return a;
    return b;

void swap(int i, int j){
  int temp = motes[i];

int partition(int start, int end){
  int x,l,j;

  x = motes[end-1];
  l = start-1;

  for (j=start;j<end-1;j++){
    if (motes[j]<x){
  return l;

void quickSort(int start, int end){
  int q;

  if (end>start){
    q = partition(start,end);

int process(int size, int pos){
  int count = 0;  
  int counta,countb;
  int i;

    return 0;

    return 0;

    return table[pos][size];

    count = process(size,pos+1);
    counta = 1 + process(size,pos+1);
      countb = 1 + process(size+size-1,pos);
      countb = 1000000000;
    count = min(counta,countb);
  table[pos][size] = count;
  return count;

int main(){
        int t,k;
    int i;
    int w,l;


      fprintf(stderr,"case %dn",k+1);

          table[w][l] = -1;                
              scanf("%d %d",&a,&n);

                printf("Case #%d: %d",k+1,process(a,0));

return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *