12032 - The Monkey and the Oiled Bamboo

All about problems in Volume 120. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Shahidul.CSE
Experienced poster
Posts: 148
Joined: Sun Jul 13, 2014 4:32 am
Location: Rangpur, Bangladesh

Re: 12032

Post by Shahidul.CSE »

My code giving correct output for every sample input given on this topic . But having WA. Whats wrong with my code?

Code: Select all

#include<stdio.h>
int main()
{
    long long int t,n,r[100010],c,i,max,k,dif;
    scanf("%lld",&t);
    for(c=1;c<=t;c++)
    {
        scanf("%lld",&n);
        for(i=1;i<=n;i++)
            scanf("%lld",&r[i]);
        max=0;
        for(i=1;i<n;i++)
        {
            dif=r[i+1]-r[i];
            if(dif>max)
                max=dif;
        }
        k=max;
        for(i=1;i<n;i++)
        {
            dif=r[i+1]-r[i];
            if(dif>k)
            {
                max+=dif-k;
                k+=dif-k;
            }
            if(dif==k)
                k--;
        }
        if(r[1]>max)
            max=r[1];
        printf("Case %lld: %lld\n",c,max);
    }
    return 0;
}
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com
lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 12032

Post by lbv »

Shahidul.CSE wrote:My code giving correct output for every sample input given on this topic . But having WA. Whats wrong with my code?
You may try:

Input

Code: Select all

2
4
4 7 9 11
6
5 9 12 14 15 20
Output

Code: Select all

Case 1: 5
Case 2: 6
RedCode119
New poster
Posts: 13
Joined: Tue Jul 08, 2014 7:14 pm

Re: 12032 - The Monkey and the Oiled Bamboo

Post by RedCode119 »

I check all the test cases, but can't find out why getting WS , Here's the code

Code: Select all

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    long long t,n,h[100005],i,m,check,cnt,k;
    cnt=1;
    cin>>t;
    while(t)
    {
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>h[i];
        }
        m=0;
        for(i=0; i<n-1; i++)
        {
            check=abs(h[i]-h[i+1]);
            if(check>m)
                m=check;
        }
        k=m;

        for(i=0; i<n-1; i++)
        {
            if(i==0)
            {
                if(h[0]>m)
                {
                        k++;
                        m=k;
                        i=-1;
                    continue;
                }
                if(h[0]==m)
                    m--;

            }

            check=abs(h[i]-h[i+1]);
            if(check>m)
            {
                k++;
                m=k;
                i=-1;
                continue;
            }
            if(check==m)
                m--;



        }
        cout<<"Case "<<cnt<<": "<<k<<endl;
        cnt++;
        t--;

    }
    return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 12032 - The Monkey and the Oiled Bamboo

Post by brianfry713 »

Try a case where n = 1.
Check input and AC output for thousands of problems on uDebug!
RedCode119
New poster
Posts: 13
Joined: Tue Jul 08, 2014 7:14 pm

Re: 12032 - The Monkey and the Oiled Bamboo

Post by RedCode119 »

Thnx a lot Brainfry, Accepted
ehsanulbigboss
New poster
Posts: 32
Joined: Tue Jul 22, 2014 1:17 am

Re: 12032 - The Monkey and the Oiled Bamboo

Post by ehsanulbigboss »

Thanks to brianfry713
Last edited by ehsanulbigboss on Wed Dec 03, 2014 3:23 pm, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 12032 - The Monkey and the Oiled Bamboo

Post by brianfry713 »

Change line 21 to:
for (i=1 ; i <= n ; i++)
Change line 29 from max+=2 to break.
Check input and AC output for thousands of problems on uDebug!
garbage
New poster
Posts: 19
Joined: Thu Feb 21, 2013 5:46 am

Re: 12032 - The Monkey and the Oiled Bamboo, WA, Help..

Post by garbage »

Code: Select all

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<long long>v;
	long long n, nxt, fst, T, d, result, last, val;
    
	cin>>T;

    for(long long i=1;i<=T;i++)
    {
        cin>>n;
        
        fst = 0;
        for(long long j=1;j<=n;j++)
        {
            cin>>nxt;
            
            d = nxt - fst;
            fst = nxt;
            v.push_back(d);
        }
        
        sort(v.begin(), v.end());
		
		last = v.size()-1;
		result = v[last];

		for(long long j=result; ;j++)
		{
			val = j;
			bool flag = false;
			for(long long k=last;k>=0;k--)
			{
				if(v[k] > val)
				{
					flag = true;
					break;
				}

				if(v[k] == val)
					val--;
			}

			if(!flag)
			{
				cout<<"Case "<<i<<": "<<j<<endl;
				break;
			}
		}
		v.clear();
    }
    return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 12032 - The Monkey and the Oiled Bamboo

Post by brianfry713 »

Input:

Code: Select all

1
4
3 4 6 7 
AC output 3
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 120 (12000-12099)”