hey utkarsh i can't understand your algorithm ....cn u explain it
again....


On Aug 6, 11:34 pm, UTKARSH SRIVASTAV <usrivastav...@gmail.com> wrote:
> My accepted solution is this  and I have told you my logic
>
> #include<stdio.h>
> struct node
> {
>        int data;
>        int add;
>        struct node *next;}*front,*rear;
>
> int flag,c;
> int fun(int item)
> {
>      struct node *temp,*temp2,*r,*p,*q;
>       int k,i;
>      r=(struct node*)malloc(sizeof(struct node));
>      temp=(struct node*)malloc(sizeof(struct node));
>      r->next=NULL;
>      temp->data=item;
>      temp->add=0;
>      temp->next=NULL;
>      c++;
>      //rear->next=r;
>      //printf("hi");
>   //   scanf("%d",&k);
>      if(front==NULL)
>      {
>                     front=temp;
>                     rear=front;
>                  //   rear->next=r;
>             //        c++;
>                 //    temp2=(struct node *)malloc(sizeof(struct node));
>                   //  temp->
>      }
>      else if(front->data>temp->data)
>      {
>           temp->next=front;
>           front=temp;
>           //c++;
>      }
>      else if(front->data==temp->data)
>      {
>           flag=1;
>           return 1;
>      }
>      else
>      {
>            p=front;
>          while(p->next!=NULL&&p->next->data<temp->data)
>          {
>                                              p=p->next;
>          }
>          if(p->next&&(p->next->data==temp->data))
>          {
>                                       flag=1;
>                                       return 1;
>          }
>          if(p->next==NULL)
>          {
>                        temp->next=p->next;
>                        p->next=temp;
>                        rear=temp;
>                        //c++;
>                        //r=rear;
>           }
>           else
>           {
>                temp->next=p->next;
>                        p->next=temp;
>                        //c++;
>           }
>
>      }
>       p=front;
>       //printf("hi");
>   // scanf("%d",&k);
>   while(p!=NULL)
>   {
>                 if(p->data!=temp->data&&p->add==0)
>                 {
>                                                  temp2=(struct
> node*)malloc(sizeof(struct node));
>
> temp2->data=p->data+item;
>
> temp2->next=NULL;
>
> temp2->add=1;
>                                                                         q=p;
>
> while(q->next!=NULL&&q->next->data<temp2->data)
>                                                                         {
>
> q=q->next;
>                                                                         }
>
> if(q->next==NULL)
>                                                                         {
>
> temp2->next=q->next;
>
> q->next=temp2;
>
> rear=temp2;
>                                                                         }
>                                                                         else
> if(q->data==temp2->data)
>                                                                         {
>
> flag=1;
>
> return 1;
>                                                                         }
>                                                                         else
>                                                                         {
>
> temp2->next=q->next;
>
> q->next=temp2;
>
> }
>
>                 }
>                 p=p->next;
>   }
>
>       /*for(i=1;i<=c;i++)
>       {
>                        if(p->data!=item)
>                        {
>                          temp2=(struct node*)malloc(sizeof(struct node));
>
> temp2->data=p->data+item;
>
> temp2->next=NULL;
>                                                                         q=p;
>
> while(q->next!=NULL&&q->next->data<temp2->data)
>                                                                         {
>
> q=q->next;
>                                                                         }
>
> if(q->next==NULL)
>                                                                         {
>
> temp2->next=q->next;
>
> q->next=temp2;
>
> rear=temp2;
>                                                                         }
>                                                                         else
>                                                                         {
>
> temp2->next=q->next;
>
> q->next=temp2;
>
> }
>
> //rear->next=temp2;
>
> //rear=rear->next;
>
> //free(temp2);
>                          }
>                          p=p->next;
>       }*/
>       //printf("hiii");
>     //scanf("%d",&k);
>       p=front;
>       while(p!=NULL)
>       {
>                    p->add=0;
>                  p=p->next;
>                    // p=p->next;
>                    //scanf("%d",&k);
>       }
>       p=front;
>       while(p!=NULL)
>       {
>                    if(p->next&&(p->data==p->next->data))
>                    flag=1;
>                  //  printf("%d ",p->data);
>                    p=p->next;
>
>                    // p=p->next;
>                    //scanf("%d",&k);
>       }}
>
> /*void fun(int item)
> {
>      struct noode *temp,*temp2,*r;
>      temp=(struct node*)malloc(sizeof(struct node));
>      temp->data=item;
>      temp->next=NULL;
>      r=rear;
>      if(front==NULL)
>      {
>                     front=temp;
>                     rear=front;
>                 //    temp2=(struct node *)malloc(sizeof(struct node));
>                   //  temp->
>      }
>      else
>      {
>                     p=front;
>                     r=rear;
>                     if(temp->data<front->data)
>                     {
>                            temp->next=front;
>                            front=temp;
>                              temp2=(struct node*)malloc(sizeof(struct
> node));
>                            temp2->data=p->data+item;
>                            temp2->next=NULL;
>                            rear->next=temp2;
>                            rear=rear->next;
>                            free(temp2);
>                     }
>                     else if(temp->data==front->data)
>                     {
>                          flag=1;
>                     }
>                           // p=front;
>
>                                          temp2=(struct
> node*)malloc(sizeof(struct node));
>
> temp2->data=p->data+item;
>
> temp2->next=NULL;
>
> rear->next=temp2;
>
> rear=rear->next;
>
> free(temp2);
>
>                            while(p->next!=r&&p->next->data<temp->data)
>                            {
>                               temp2=(struct node*)malloc(sizeof(struct
> node));
>                                temp2->data=p->data+item;
>                            temp2->next=NULL;
>                            rear->next=temp2;
>                            rear=rear->next;
>                            p=p->next;
>                            free(temp2);
>                            }
>                            if(p->next==r)
>                            {
>                                          if(p->next->data<temp->data)
>                                          {
>
> temp->next=r->next;
>
> r->next=temp;
>
> temp2=(struct node*)malloc(sizeof(struct node));
>
> temp2->data=r->data+item;
>
> temp2->next=NULL;
>
> rear->next=temp2;
>
> rear=rear->next;
>
> free(p);
>                                           }
>                                           else if(p->next->data==temp->data)
>                                           {
>                                                flag=1;
>                                           }
>                                           else
>                                           {
>
> temp->next=p->next;
>
> p->next=temp;
>
> temp2=(struct node*)malloc(sizeof(struct node));
>
> temp2->data=r->data+item;
>
> temp2->next=NULL;
>
> rear->next=temp2;
>
> rear=rear->next;
>                                           }
>                            }
>                            else
>                            {
>
>                            temp->next=p->next;
>                            p->next=temp;
>                            temp2=(struct node*)malloc(sizeof(struct node));
>
> temp2->data=p->data+item;
>
> temp2->next=NULL;
>
> rear->next=temp2;
>
> rear=rear->next;
>
>                            while(p->next!=r)
>                            {
>                                           temp2=(struct
> node*)malloc(sizeof(struct node));
>
> temp2->data=p->data+item;
>
> temp2->next=NULL;
>
> rear->next=temp2;
>
> rear=rear->next;
>
> p=p->next;
>                            }
>
>              }} */
>
> main()
> {
>      int i,n,k,t,y;
>       scanf("%d",&t);
>       while(t--)
>       {
>                 scanf("%d",&n);
>                 front=NULL;
>                 flag=0;
>                 c=0;
>                 rear=NULL;
>                 for(i=1;i<=n;i++)
>                 {
>                                 scanf("%d",&y);
>                                  if(flag!=1)
>                                  fun(y);
>                  }
>                  if(flag==1)
>                  printf("YES\n");
>                  else
>                  printf("NO\n");
>       }
>       return 0;
>
>
>
>
>
>
>
> }
> On Sat, Aug 6, 2011 at 11:11 AM, Amol Sharma <amolsharm...@gmail.com> wrote:
> > anyone plz give hint how to solve this problem !!
>
> > --
>
> > Amol Sharma
> > Third Year Student
> > Computer Science and Engineering
> > MNNIT Allahabad
>
> > On Sat, Aug 6, 2011 at 2:53 PM, Amol Sharma <amolsharm...@gmail.com>wrote:
>
> >>http://www.spoj.pl/SPOJ/problems/TUG/
>
> >> earlier i thought it would be easy to do it knapsack...but when i started
> >> coding it....i feel lost.....i have failed to relate it with 0/1
> >> knapsack......
>
> >> plz tell me how this problem can be solved using dp solution of
> >> knapsack......i mean in knapsack you form a matrix    b[ ][ ] in which
> >> b[n][w] denotes the maximum benefit with n items available having weight
> >> exactly w.......
> >> .....in tug of war how can we use this matrix ??
>
> >> --
>
> >> Amol Sharma
> >> Third Year Student
> >> Computer Science and Engineering
> >> MNNIT Allahabad
>
> >> On Thu, Aug 4, 2011 at 9:55 PM, Don <dondod...@gmail.com> wrote:
>
> >>> As some have said, this is NP, so for larger values of N it takes a
> >>> very long time. For N=20 it runs quite quickly.
>
> >>> // True if some set of strengths from s[n] sum to t
> >>> bool divide(unsigned int *s, int n, int t)
> >>> {
> >>>        if (t == 0) return true;             // We reached the goal
> >>>        if (n == 0) return false;            // No people left to assign
> >>>        if (*s > t) return false;            // Smallest person exceeds
> >>> goal
> >>>        if (*s * n < t) return false;      
>
> ...
>
> read more »

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to