Thread: Getting Wrong answer ???

  1. #1
    Registered User
    Join Date
    Jul 2012

    Exclamation Getting Wrong answer ???

    Here is the question :-
    The government of Siruseri has just commissioned one of the longest and most modern railway routes in the world. This route runs the entire length of Siruseri and passes through many of the big cities and a large number of small towns and villages in Siruseri.The railway stations along this route have all been constructed keeping in mind the comfort of the travellers. Every station has big parking lots, comfortable waiting rooms and plenty of space for eateries. The railway authorities would like to contract out the catering services of these eateries.Unfortunately, catering contractors are not philanthropists and would like to maximise their profits. The Siruseri Economic Survey has done a through feasibility study of the different stations and documented the expected profits (or losses) for the eateries in all the railway stations on this route. Every contractor would like to run the catering service only in the profitable stations and stay away from the loss making ones.
    On the other hand the authorities would like to ensure that every station was catered to. Towards this end, authorities offered to contract out stations in groups. They would fix a minimum size K and a contractor was only allowed to bid for any contiguous sequence of K or more stations.For example suppose there are 8 stations along the line and their profitability is as follows:
    Station 1 2 3 4 5 6 7 8Expected Profits -20 90 -30 -20 80 -70 -60 125If K was fixed to be 3, a contractor could pick stations 3, 4, 5 and 6. This would give him a total profit of -40 (i.e. a loss of 40). He could have picked 3, 4 and 5 giving him a profit of 30. On the other hand if he picked stations 2, 3, 4 and 5, he would make a profit of 120. You can check that this is the best possible choice when K = 3.If K = 1, then the best choice would be to bid for just station 8 and make a profit of 125.You have been hired by a contractor. Your task is to help him identify the segment of stations to bid for so at to maximize his expected profit.Input formatThe first line of the input contains two integers N and K, where N is the number of stations and K is the minimum number of contiguous stations that must form part or the bid. The next N+1 lines (lines 2,...,N+1) describe the profitability of the N stations. Line i+1 contains a single integer denoting the expected profit at station i.Output formatA single integer P, indicating the maximum possible profit.Test Data:You may assume that 1 ≤ N ≤ 1000000 and 1 ≤ K ≤ N. You may further assume that in 50% of the inputs N≤ 5000.Example:We illustrate the input and output format using the above example:Sample Input 1:
    8 3-2090-30-2080-70-60125 Sample Output 1:
    120Sample Input 2:
    8 1-2090-30-2080-70-60125 Sample Output 2:
    And here is my solution :-
    using namespace std;
    int main( ) {
        int i,j,n,k,stations[100000],summed[100000],sum=0,msum=0;
        for (i=1;i<=n;i++) {
        for (i=1;i<=n-k+1;i++) {
            for (j=i+k-1;j<=n;j++) {
                if (msum<sum) msum=sum;
    But i am getting wrong answer for 6 test case out of 10 . Could anybody give example where my programme is wrong or correct my code ?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Given that your post evidently concerns a homework exercise, it would be appropriate for you to understand this site's homework policy. Follow this link.

    The short summary: you need to do your own homework. Not make an initial stab and then beg people to fix it.

    One thing you definitely need to be aware of, technically, is that array indices in C++ start at zero. Not one. If an array has ten elements, valid indices for accessing its elements are 0 through 9. Using invalid indices is a really bad idea.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Always give me wrong answer
    By amroto in forum C Programming
    Replies: 8
    Last Post: 03-29-2012, 10:43 AM
  2. Replies: 3
    Last Post: 09-06-2009, 02:48 PM
  3. Wrong answer?
    By eViLrAcEr in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2009, 07:04 AM
  4. Question 26 Answer seems wrong on site quiz
    By Kleid-0 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2005, 04:03 PM
  5. wrong answer?
    By cman in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 02:17 AM

Tags for this Thread

Website Security Test