Ejemplo n.º 1
0
int main()
{   
        int n,k,i;
        
        scanf("%d %d",&n,&k);
        
        for(i =0; i < n; i++ )
        {
            scanf("%d",&profit[i] );
            
            if( i == 0 )
                cumprofit[i] = profit[i];
            else
                cumprofit[i] = cumprofit[i-1] + profit[i];
        }
        
        /* 
            0 Empty space here 
            1 Has billboard here
        */
        
        dp[0][0] = 0 , dp[0][1] = profit[0];
        
        for( i = 1; i < n; i++ )
        {
            dp[i][0] = getMAX( dp[i-1][0] , dp[i-1][1] );
            
            dp[i][1] = dp[i-1][0] + profit[i];
            
            int idx = i - k;
            
            int val = 0;
            if( i - k >= 0 )
            {
                // at i - k space is allowed 
                val = dp[i-k][0];
                if( i -k > 0 )
                    // at i -k -1 a bill board is allowed 
                    val = getMAX( val , dp[i-k-1][1] );
            }
            
            dp[i][1] = getMAX( dp[i][1] , val + ( cumprofit[i] - cumprofit[idx] ) );
            
        }
        
        printf("%d\n", getMAX( dp[n-1][0] , dp[n-1][1] ) );
    
       return 0;
}
Ejemplo n.º 2
0
 void activity(){
     
     char arr[SIZE];
     int arr1[SIZE];
     char rarr1[SIZE];
     double arr2[SIZE];
     char rarr2[SIZE];
     
     for(int i=0;i<SIZE;i++){
         cout<<"Enter the character for index "<<i+1<<": ";
         cin>>arr[i];
     }
     
     for(int i=0;i<SIZE;i++){
         cout<<"Enter the number for index "<<i+1<<": ";
         cin>>arr1[i];
     }
     
     for(int i=0;i<SIZE;i++){
         cout<<"Enter the double number for index "<<i+1<<": ";
         cin>>arr2[i];
     }
     cout<<"===========Char Array================"<<endl;
     cout<<"The maximum character is "<<getMAX(arr)<<endl;
     cout<<"The minumum character is "<<getMIN(arr)<<endl;
     cout<<"===========Int Array================"<<endl;
     cout<<"The maximum number is "<<getMAX(arr1)<<endl;
     cout<<"The minumum number is "<<getMIN(arr1)<<endl;
     checkEven(arr1, rarr1);
     cout<<printArry(rarr1)<<endl;
     cout<<"===========Double Array================"<<endl;
     CountDown(arr2);
     cout<<endl;
     cout<<"The maximum number is "<<getMAX(arr2)<<endl;
     cout<<"The minumum number is "<<getMIN(arr2)<<endl;
     checkEven(arr2, rarr2);
     cout<<printArry(rarr2);
 }
Ejemplo n.º 3
0
static int check_collision(dude_t *a, dude_t *b)
{
	/* Use pos_t and bounds_t to calculate where in space dudes are */
	/* Get the x,y,z max and min bounds of dudes */
	GLfloat a_x_min = getMIN(a, x, 0);
	GLfloat a_x_max = getMAX(a, x, 0);

	GLfloat a_y_min = getMIN(a, y, 1);
	GLfloat a_y_max = getMAX(a, y, 1);

	GLfloat a_z_min = getMIN(a, z, 2);
	GLfloat a_z_max = getMAX(a, z, 2);

	GLfloat b_x_min = getMIN(b, x, 0);
	GLfloat b_x_max = getMAX(b, x, 0);

	GLfloat b_y_min = getMIN(b, y, 1);
	GLfloat b_y_max = getMAX(b, y, 1);

	GLfloat b_z_min = getMIN(b, z, 2);
	GLfloat b_z_max = getMAX(b, z, 2);

	/* Calculate collisions in all planes */
	unsigned char x_collision = (a_x_max >= b_x_min) && (a_x_min <= b_x_max);
	unsigned char y_collision = (a_y_max >= b_y_min) && (a_y_min <= b_y_max);
	unsigned char z_collision = (a_z_max >= b_z_min) && (a_z_min <= b_z_max);

#ifdef DEBUG
	printf("a:\tx:(%d, %d)\ty:(%d, %d)\tz:(%d, %d)\n",
	       (int)a_x_min, (int)a_x_max,
	       (int)a_y_min, (int)a_y_max,
	       (int)a_z_min, (int)a_z_max);
	printf("b:\tx:(%d, %d)\ty:(%d, %d)\tz:(%d, %d)\n",
	       (int)b_x_min, (int)b_x_max,
	       (int)b_y_min, (int)b_y_max,
	       (int)b_z_min, (int)b_z_max);

	printf("x:%d y:%d z:%d\n", x_collision, y_collision, z_collision);
#endif

	/* Collision only if all planes show collisions,
	   otherwise just in line
	*/
	if(x_collision && y_collision && z_collision) {
		printf("Bounding box collision\n");
		/* Check at finer granualrity */
#define OPTION2

#ifdef OPTION1
		/* Option 1: blanket check - compare all voxels in a and b */
		{
			pos_t *_a = NULL, *_b = NULL;
			unsigned int _aI = 0, _bI = 0;
			/* For each voxel in a */
			for(_a = &(a->dude_item.blocks[_aI++]); _aI <= a->dude_item.count; _a = &(a->dude_item.blocks[_aI++]) ) {

				/* For each voxel in b */
				for(_bI = 0, _b = &(b->dude_item.blocks[_bI++]); _bI <= b->dude_item.count; _b = &(b->dude_item.blocks[_bI++]) ) {
					
					/* Check for collisions */
					/* Current voxel plus the current dude position */
					if(((_a->x + a->position.x) == (_b->x + b->position.x))
					   &&
					   ((_a->y + a->position.y) == (_b->y + b->position.y))
					   &&
					   ((_a->z + a->position.z) == (_b->z + b->position.z))) {
						/* Collision */
						printf("Voxel collision\n");
						return 1;
					}
				}
			}
		}
#elif defined OPTION2
		/* Option 2: */
		{
			unsigned char *dude_a_collision = NULL;
			
			/* work out size of dude a */
			int x_size = (int)(a_x_max - a_x_min) + 1;
			int y_size = (int)(a_y_max - a_y_min) + 1;
			int z_size = (int)(a_z_max - a_z_min) + 1;
			
			pos_t *p = NULL;
			unsigned int i = 0;
			
			/* Allocate space */
			if((dude_a_collision = (unsigned char *)calloc((x_size * y_size * z_size), 1)) == NULL) {
				fprintf(stderr, "Unable to allocate memory: dude_a_collision\n");
				exit(-1);
			}
			/* dude_a_collision being used as [z][y][x] */
			
			/* Fill in map of voxels in dude a */
			/* This required normalising the positions relative to a point where all values
			   end up starting from zero. Using the min bounds for this.
			*/
			for(p = &(a->dude_item.blocks[i++]); i <= a->dude_item.count; p = &(a->dude_item.blocks[i++]) ) {
				dude_a_collision[(int)((p->x - a->dude_item.bounds[0].min) +
				                       ((p->y - a->dude_item.bounds[1].min) * x_size) +
				                       ((p->z - a->dude_item.bounds[2].min) * y_size * x_size))] = 1;
			}
			
			/* Check against dude b */
			/* Need to translate dude b voxels in relation to the point used in above step
			   (voxel offset + center point(b)) - (center point(a) + point of reference)
			*/
			
			for(i = 0, p = &(b->dude_item.blocks[i++]); i <= b->dude_item.count; p = &(b->dude_item.blocks[i++]) ) {
				int x = (int)((p->x + b->position.x) - (a->position.x + a->dude_item.bounds[0].min));
				int y = (int)((p->y + b->position.y) - (a->position.y + a->dude_item.bounds[1].min));
				int z = (int)((p->z + b->position.z) - (a->position.z + a->dude_item.bounds[2].min));
				if( ((x >= 0) && (x < x_size)) && ((y >= 0) && (y < y_size)) && ((z >= 0) && (z < z_size)) ) {
					if(dude_a_collision[(x + (y * x_size) + (z * y_size * x_size))]) {
						printf("Voxel collision\n");
						free(dude_a_collision);
						return 1;
					}
				}
			}
			free(dude_a_collision);
		}
#else
#error "You need to specify a collision check"
#endif
		return 0;
	}
	return 0;
}