You are not logged in. Please login at www.codechef.com to post your questions!

×

test case is decrementing for two times???

this is a problem in which I have to calculate the median in a sorted array after insertion and deletion each time but the test case is decrementing for 2 times i each iteration ?? I am unable to find it :(

My code is :-----

include<iostream>

include<set>

include<stdio.h>

using namespace std; multiset<int> myset; int main() {

int test;
cin>>test;
int e=test;
while(test)
{
    test=test-1;
    //test++;
    char input;
    int value;
    //scanf("%c",&input);
    cin>>input;

    //scanf("%d",&value);
    cin>>value;
    if(input=='a')
    {
        //cout<<"byeeeeeeeee\n"<<test<<"lsfhvfduhvl";
        myset.insert(value);
        int a=myset.size();
        if(a%2!=0)  //odd case
        {
            multiset<int> :: const_iterator it=myset.begin();
            for(int i=0;i<(a/2);i++)
            {
                it++;
            }
            printf("%d\n",*it);
            //cout<<(*it)<<"\n";
        }
        else
        {
            int b,c;
            multiset<int> :: const_iterator it=myset.begin();
            for(int i=0;i<(a/2);i++)
            {
                it++;
            }
            b=(*it);
            it--;
            c=(*it);
            double x= 1.0*(b+c)/2;
            printf("%.10g\n",x);
            //cout<<setprecision(9)<<x<<"\n";

        }
    }
    else if(input=='r')
    {
        //cout<<"hiiiiiiiiiiiii\n"<<test<<"lufhvfdhsv";

        if(myset.find(value)!=myset.end())
        {
            cout<<"hiiiiiiiiiiiii";
            myset.erase(myset.find(value));

            if(!myset.empty())
            {
                int a=myset.size();

        if(a%2!=0)  //odd case
        {
            multiset< int > :: const_iterator it=myset.begin();
            for(int i=0;i<(a/2);i++)
            {
                it++;
            }
            printf("%d\n",*it);
            //cout<<(*it)<<"\n";
        }
        else
        {
            int b,c;
            multiset< int > :: const_iterator it=myset.begin();
            for(int i=0;i<(a/2);i++)
            {
                it++;
            }
            b=(*it);
            it--;
            c=(*it);
            //cout<<1.*(b+c)/2<<"\n";
            double x= 1.0*(b+c)/2;
            printf("%.10g\n",x);
            //cout<<setprecision(9)<<x<<"\n";

        }
        }
        else
        cout<<"Wrong!\n";

        }
        else
        {
            cout<<"Wrong!\n";
        }

    }

}
return 0;

}

asked 28 Dec '12, 00:37

vivek07672's gravatar image

2★vivek07672
15122124
accept rate: 0%

First of all you should use scanf instead of printf... By all means avoid using cin and cout... scanf and printf are STRONGLY recommended... And can you give me the problem statement? I think that way it will be easier to help you... If I can of course.

(28 Dec '12, 00:45) junior944★

it is a problem of interview street https://www.interviewstreet.com/challenges/dashboard/#problem/4fcf919f11817 why the test case is giving me the problem ie, it is decrementing by 2 instead of 1 in each iteration ??

link

answered 28 Dec '12, 00:52

vivek07672's gravatar image

2★vivek07672
15122124
accept rate: 0%

http://ideone.com/JnykoC see this... its working fine here...

(28 Dec '12, 03:33) shivamrana5★

no,its not working http://ideone.com/JnykoC if i change cin with scanf then still it is decremeting by two . i am getting the problem in it

link

answered 28 Dec '12, 09:50

vivek07672's gravatar image

2★vivek07672
15122124
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×946

question asked: 28 Dec '12, 00:37

question was seen: 1,322 times

last updated: 06 Dec '16, 14:54