ListNode* mergeKLists(vector<ListNode*>& lists) {
		 std::priority_queue<minp> min_heap;

		 ListNode* nhead = NULL, *cur = NULL;
		 nhead = new ListNode(1);
		 ListNode* curr;
		 cur = nhead;

		 for (int i = 0; i < lists.size(); i++) {
			 if(lists[i])
				min_heap.push(minp(lists[i]->val, lists[i]));
		 }

		 while (min_heap.size()) {
			 cur->next = new ListNode(min_heap.top().val);
			 curr = min_heap.top().add;
			 min_heap.pop();
			 
			 if (curr->next) {
				 curr = curr->next;
				 min_heap.push(minp(curr->val, curr));
			 }
			 cur = cur->next;
		 }

		 return nhead->next;
	 }
Exemplo n.º 2
0
double min_cost(int a[],int n,int p,int q){
	 int i;
	 double mint;



	 if(p>n-1 || q>n-1)return 9999.0000;
	 if(p>q)return 9999.0000;
    if(q-p==1)return sqrt(a[q]-a[p]+1);
	 if(p==q)return sqrt(1.0000);

	 mint=9999.0000;



	 for(i=p;i<n-1;i++)
			mint=minp(mint,minp(sqrt((double)a[q]-a[p]+1),min_cost(a,n,p,i)+min_cost(a,n,i+1,q)));
			return mint;



}
Exemplo n.º 3
0
float PortraitCut::BVZ_interaction_penalty(Coord p, Coord np, ushort l, ushort nl) { 
  int c,  k;
  float a,M=0;

  assert(l<_n);
  assert(l<_n);

  if (l==nl) return 0;


  unsigned char *Il, *Inl;

  if (_cuttype == C_NORMAL || _cuttype == C_GRAD)
  {
    // difference at p pixel
    a=0;
    Il = _imptr(l,p); 
    Inl = _imptr(nl,p);  
    for (c=0; c<3; ++c) {
      k = Il[c] - Inl[c];
      a += k*k;    
    }
    M = sqrt(a);
    
    // difference at np pixel
    a=0;
    Il = _imptr(l,np); 
    Inl = _imptr(nl,np);  

    for (c=0; c<3; ++c) {
      k = Il[c] - Inl[c];
      a += k*k;    
    }
    M += sqrt(a);
    
    
    M /=6.f;
    
    // gradient denominator
    if (_cuttype == C_GRAD) 
	{
      float G;
      if (p.x!=np.x) {  // vertical cut, vertical Sobel filter
	Coord minp(min(p.x,np.x), p.y);
	if (p.y>0 && p.y<_h-1) {
	  G = .5f*(_idata->vertGradMagLookup(l,minp) + _idata->vertGradMagLookup(nl,minp));
	  //G = MIN(vertGradMagLookup(l,minp), vertGradMagLookup(nl,minp));
	}
	else
	  G = 1.f;
      }
      else {  // horizontal cut, horizontal Sobel filter
	Coord minp(p.x, min(p.y,np.y));
	if (p.x>0 && p.x<_w-1)
	{
	  G = .5f*(_idata->horizGradMagLookup(l,minp) + _idata->horizGradMagLookup(nl,minp));
	  //G = MIN(horizGradMagLookup(l,minp), horizGradMagLookup(nl,minp));
	}
	else
	  G = 1.f;
      }
      
      
		  if (G==0)
		M = A_INFINITY;
		  else
		M /= G;
    }
  }

  else if (_cuttype == C_GRAD2) {
    // difference at p pixel    
    const CGrad& acgrad = _idata->cgradLookup(l,p);
    const CGrad& bcgrad = _idata->cgradLookup(nl,p);
    M = acgrad.normFrom(bcgrad);

    // difference at np pixel
    const CGrad& acgrad2 = _idata->cgradLookup(l,np);
    const CGrad& bcgrad2 = _idata->cgradLookup(nl,np);
    M += acgrad2.normFrom(bcgrad2);
  }

  else if (_cuttype == C_BOTH) {
    // difference at p pixel    
    const CGrad& acgrad = _idata->cgradLookup(l,p);
    const CGrad& bcgrad = _idata->cgradLookup(nl,p);
    M = acgrad.normFrom(bcgrad);

    // difference at np pixel
    const CGrad& acgrad2 = _idata->cgradLookup(l,np);
    const CGrad& bcgrad2 = _idata->cgradLookup(nl,np);
    M += acgrad2.normFrom(bcgrad2);
  
    // difference at p pixel
    a=0;
    Il = _imptr(l,p); 
    Inl = _imptr(nl,p);  
    for (c=0; c<3; ++c) {
      k = Il[c] - Inl[c];
      a += k*k;    
    }
    M += sqrt(a);
    
    // difference at np pixel
    a=0;
    Il = _imptr(l,np); 
    Inl = _imptr(nl,np);  
    
    for (c=0; c<3; ++c) {
      k = Il[c] - Inl[c];
      a += k*k;    
    }
    M += sqrt(a);

    M *= .1;
  }

  if (M>A_INFINITY) M = A_INFINITY;
  //printf("%d ",d);
  //assert(_finite(M) && !_isnan(M));
  return M;
}
Exemplo n.º 4
0
/**
 * This callback routine updates the SPRoot object when its attributes have been changed.
 */
static void sp_root_update(SPObject *object, SPCtx *ctx, guint flags)
{
    SPRoot *root = SP_ROOT(object);
    SPItemCtx *ictx = (SPItemCtx *) ctx;

    /* fixme: This will be invoked too often (Lauris) */
    /* fixme: We should calculate only if parent viewport has changed (Lauris) */
    /* If position is specified as percentage, calculate actual values */
    if (root->x.unit == SVGLength::PERCENT) {
        root->x.computed = root->x.value * ictx->viewport.width();
    }
    if (root->y.unit == SVGLength::PERCENT) {
        root->y.computed = root->y.value * ictx->viewport.height();
    }
    if (root->width.unit == SVGLength::PERCENT) {
        root->width.computed = root->width.value * ictx->viewport.width();
    }
    if (root->height.unit == SVGLength::PERCENT) {
        root->height.computed = root->height.value * ictx->viewport.height();
    }

    /* Create copy of item context */
    SPItemCtx rctx = *ictx;

    /* Calculate child to parent transformation */
    root->c2p.setIdentity();

    if (object->parent) {
        /*
         * fixme: I am not sure whether setting x and y does or does not
         * fixme: translate the content of inner SVG.
         * fixme: Still applying translation and setting viewport to width and
         * fixme: height seems natural, as this makes the inner svg element
         * fixme: self-contained. The spec is vague here.
         */
        root->c2p = Geom::Affine(Geom::Translate(root->x.computed,
                                                 root->y.computed));
    }

    if (root->viewBox_set) {
        double x, y, width, height;
        /* Determine actual viewbox in viewport coordinates */
        if (root->aspect_align == SP_ASPECT_NONE) {
            x = 0.0;
            y = 0.0;
            width = root->width.computed;
            height = root->height.computed;
        } else {
            double scalex, scaley, scale;
            /* Things are getting interesting */
            scalex = root->width.computed / root->viewBox.width();
            scaley = root->height.computed / root->viewBox.height();
            scale = (root->aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
            width = root->viewBox.width() * scale;
            height = root->viewBox.height() * scale;
            /* Now place viewbox to requested position */
            /* todo: Use an array lookup to find the 0.0/0.5/1.0 coefficients,
               as is done for dialogs/align.cpp. */
            switch (root->aspect_align) {
                case SP_ASPECT_XMIN_YMIN:
                    x = 0.0;
                    y = 0.0;
                    break;
                case SP_ASPECT_XMID_YMIN:
                    x = 0.5 * (root->width.computed - width);
                    y = 0.0;
                    break;
                case SP_ASPECT_XMAX_YMIN:
                    x = 1.0 * (root->width.computed - width);
                    y = 0.0;
                    break;
                case SP_ASPECT_XMIN_YMID:
                    x = 0.0;
                    y = 0.5 * (root->height.computed - height);
                    break;
                case SP_ASPECT_XMID_YMID:
                    x = 0.5 * (root->width.computed - width);
                    y = 0.5 * (root->height.computed - height);
                    break;
                case SP_ASPECT_XMAX_YMID:
                    x = 1.0 * (root->width.computed - width);
                    y = 0.5 * (root->height.computed - height);
                    break;
                case SP_ASPECT_XMIN_YMAX:
                    x = 0.0;
                    y = 1.0 * (root->height.computed - height);
                    break;
                case SP_ASPECT_XMID_YMAX:
                    x = 0.5 * (root->width.computed - width);
                    y = 1.0 * (root->height.computed - height);
                    break;
                case SP_ASPECT_XMAX_YMAX:
                    x = 1.0 * (root->width.computed - width);
                    y = 1.0 * (root->height.computed - height);
                    break;
                default:
                    x = 0.0;
                    y = 0.0;
                    break;
            }
        }

        /* Compose additional transformation from scale and position */
        Geom::Scale const viewBox_length( root->viewBox.dimensions() );
        Geom::Scale const new_length(width, height);

        /* Append viewbox transformation */
        /* TODO: The below looks suspicious to me (pjrm): I wonder whether the RHS
           expression should have c2p at the beginning rather than at the end.  Test it. */
        root->c2p = Geom::Translate(-root->viewBox.min()) * ( new_length * viewBox_length.inverse() ) * Geom::Translate(x, y) * root->c2p;
    }

    rctx.i2doc = root->c2p * rctx.i2doc;

    /* Initialize child viewport */
    if (root->viewBox_set) {
        rctx.viewport = root->viewBox;
    } else {
        /* fixme: I wonder whether this logic is correct (Lauris) */
        Geom::Point minp(0,0);
        if (object->parent) {
            minp = Geom::Point(root->x.computed, root->y.computed);
        }
        rctx.viewport = Geom::Rect::from_xywh(minp[Geom::X], minp[Geom::Y], root->width.computed, root->height.computed);
    }

    rctx.i2vp = Geom::identity();

    /* And invoke parent method */
    if (((SPObjectClass *) (parent_class))->update)
        ((SPObjectClass *) (parent_class))->update(object, (SPCtx *) &rctx, flags);

    /* As last step set additional transform of drawing group */
    for (SPItemView *v = root->display; v != NULL; v = v->next) {
        Inkscape::DrawingGroup *g = dynamic_cast<Inkscape::DrawingGroup *>(v->arenaitem);
        g->setChildTransform(root->c2p);
    }
}