-1

The user inputs n number of datas to be followed in the next line and then it inputs n numbers a1,a2,...,an. These numbers are heights of a some mountains. The set of these numbers is "ACCEPTABLE" if there is only one maxima or minima. for example "1 2 3 2 1", has only one maxima which is 3. Also "1 2 3 4" has one maxima. but "1 10 9 8 7 6 5 6 7" is not acceptable because it has two maxima (10 and 7) or two minima (1 and 5).

In other word, the set is acceptable if and only if it is in one of this forms:

a1<=a2<=a3 ... <= ai > a(i+1) > ... >an

or

a1>=a2>=a3 ... >= ai < a(i+1) < ... < an.

I must submit the answer in a judge system which tests it with unknown test cases. Using any type of Array or Vector is completely prohibited.

My solution is this:

//C code.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,temp;
    scanf("%d",&n);
    if (n==1)
    {
        int l;
        scanf("%d",&l);
        printf("Yes");
    }
    else
{
    int a,b;
    int last;
    int changes =0;
    int dec =0 , inc =0; //flag: checking if the set is incremental or decremental till now
    scanf("%d %d",&a,&b);

    if (a>b)
    {
        dec=1;
    }
    else if (a<b)
    {
        inc = 1;
    }
    else
    {
        inc =1;
        dec = 1;
    }
    last = b;
    for (int i =2;i<n;i++)
    {

        scanf("%d",&temp);
        if (temp>last && dec==1)
        {
            inc = 1;
            dec= 0;
            changes++;
        }
        if (temp<last && inc==1)
        {
            inc =0;
            dec=1;
            changes++;
        }


if (!(inc==1 && dec==1) && temp == last)
        {
            changes++;
        }
    last = temp;
        last = temp;
    }
    if (changes <=1)
    {

        printf("Yes");
    }
    else
    {
        printf("No");
    }
}
    return 0;
}

It gets the right answer for the examples that are in the question but it fails on some unknown test cases. Any idea how to fix this? Can anyone give me a test case that is not solved right in this code?

P.1: I added

if (!(inc==1 && dec==1) && temp == last)
    {
        changes++;
    }

and it accepted one of the failing test cases but still one remains.

P.2:

This is my other algorithm which fails on some test cases but the judge accepts its answers on the failing test cases of the first one:

    #include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    int inc=0;
    int dec=0;
    int peak=0;
    int valley=0;
    int last = -1;
    int a;
    scanf("%d",&n);
    for (int i =0;i<n;i++)
    {
        if (last!=-1)
        {
            last =a;
        }
        scanf("%d",&a);
        if (last!=-1)
        {
            if (a>last)
            {
                if (!(inc==1))
                {
                    valley++;
                    inc =1;
                    dec=0;
                }
            }
            if (a<last)
            {
                if (!(dec==1))
                {
                    peak++;
                    dec=1;
                    inc =0;
                }
            }
        }
        last =0;


    }
    if (valley<=1 && peak<=1)
    {
        // printf("valley: %d , peak:%d",valley,peak);
        printf("Yes");
    }
    else
    {
        printf("No");
    }
    return 0;
}

P.3

New algorithm:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long int n,temp;
    scanf("%lld",&n);
    if (n==1)
    {
        long long int l;
        scanf("%lld",&l);
        printf("Yes");
    }
    else
{
    long long int a,b;
    long long int last;
    long long int changes =0;
    int dec =0 , inc =0; //flag: checking if the set is incremental or decremental till now
    scanf("%lld %lld",&a,&b);

    if (a>b)
    {
        dec=1;
    }
    else if (a<b)
    {
        inc = 1;
    }
    else
    {
        inc =1;
        dec = 1;
    }
    last = b;
    for (long long int i =2;i<n;i++)
    {

        scanf("%lld",&temp);
        if (temp>last && dec==1)
        {
            inc = 1;
            dec= 0;
            changes++;
        }
        if (temp<last && inc==1)
        {
            inc =0;
            dec=1;
            changes++;
        }
        if (changes>=1 && temp == last)//new change
        {
            changes+=100;
        }//end of new change
        last = temp;
    }
    if (changes <=1)
    {

        printf("Yes");
    }
    else
    {
        printf("No");
    }
}
    return 0;
}
miracle173
  • 1,852
  • 16
  • 33
titansarus
  • 136
  • 1
  • 7

5 Answers5

3

scanf("%d",l); should be scanf("%d", &l);, scanf requires the address of the variable.

So the test cases with n == 1 failed.

Always have a look at the compiler warnings: https://ideone.com/MKq3WK

mch
  • 9,424
  • 2
  • 28
  • 42
2

“4 1 1 2 1” says “No” but should say “Yes”. The code does not correctly handle the case where inc and dec are both 1 initially.

Additionally, the code must use different criteria for the first part of the sequence (up until the first change in direction is observed) and the second. In the first part, equality is permitted and does not cause any disqualification or change of state. In the second part, equality is disqualifying.

Joke

I should not do this, but sometimes one cannot resist. The following should solve the problem. Do not use it.

#include <stdio.h>

int main(void)
{
    int c, n, p, s;
    scanf("%d%d", &n, &c);

    #define Table   \
        {  1,  0,  3 }, \
        {  1,  1,  2 }, \
        { -1, -1,  2 }, \
        {  4,  3,  3 }, \
        {  4, -1, -1 }, \

    for (s = 0; 0 <= s && --n; s = (int [][3]) {Table} [s] [1+(p>c)-(p<c)])
        { p = c; scanf("%d", &c); }

    printf("%s\n", 0 <= s ? "Yes" : "No");
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Damn "smart" quotes! – pmg Oct 11 '18 at 12:27
  • @pmg: No smart quotes were used. I typed the open and close quotation marks explicitly, because I was referring to text, not quoting C strings or source code. – Eric Postpischil Oct 11 '18 at 12:28
  • It has 2 maximum ( 4 and 2 ) and 2 minimum ( the 2 1s in the middle and the 1 at the end), so it should be no. – mch Oct 11 '18 at 12:30
  • Smart quotes (normal quotes: """" / smart open quotes: ““““ / smart close quotes: ””””) appear funny on my screen :) – pmg Oct 11 '18 at 12:30
  • @pmg: Ah. To my mind “smart quotes” are the “service” the software provides when transforming plain quotes into open and close quotes based on context. Open and close quotes are simply correct quotes. What is your interface set to, some non-English character set? I would have expected open and close quotes to be sufficient non-exotic that they would be handled reasonably. – Eric Postpischil Oct 11 '18 at 12:33
  • 1
    @mch The first 4 is the number elements to be entered (= the number of elements in the set), it's not part of the set. – Jabberwocky Oct 11 '18 at 12:35
  • I'm on a very **very** old computer with FreeBSD 11.2. Here's a screenshot of the funny quotes: http://www.awesomescreenshot.com/image/3656858/e711a33f711fa6b453373f1c24db5efa – pmg Oct 11 '18 at 12:49
  • On my phone the "funny" quotes look reasonable. I still don't like them though :) – pmg Oct 11 '18 at 18:29
1

With the P.2 solution, "4 1 2 1 1" is accepted, but it should not, since 1 is not greater than 1!

1

We have to detect a change of direction of the inequality, then disallow a further change. Anyway, as long as the elements are equal, you cannot make up your mind between growing or decreasing.

I would use a state variable with six possible values:

  • x: no element has been input yet;

  • 0: we don't know anything;

  • 1: we are in the initial raising section;

  • -1: we are in the initial falling section;

  • 2: we are in the final raising section;

  • -2: we are in the final falling section.

Considering the previous and current input values, the following transitions apply:

State  x: -> 0 (unconditionally)
State  0: p < c ->  1, p > c -> -1
State  1: p > c -> -2
State -1: p < c ->  2
State  2: p >= c -> Fail
State -2: p <= c -> Fail

The initial state is x. If we never fail, success is assumed when the input has been exhausted. This can be implemented with a simple switch statement and two static variables to remember the previous value and the state.

The specifications are unclear about whether some section can be empty, so I'll leave it as is.

Python proof of concept:

Input= [1, 2, 3, 2, 1]
#Input= [1, 10, 9, 8, 7, 6, 5, 6, 7]
#Input= [1, 2, 3, 4]
#Input= [4, 1, 2, 1, 1]

def Process(c):
    global p, s

    if s == None:
        s= 0
    elif s == 0:
        if p < c:
            s= 1
        elif p > c:
            s= -1
    elif s == 1:
        if p > c:
            s= -2
    elif s == -1:
        if p < c:
            s= 2
    elif s == 2:
        if p >= c:
            exit(-1)
    elif s == -2:
        if p <= c:
            exit(-1)
    p= c

s= None
for c in Input:
    Process(c)
0

The judge finally accepted this code. The main problems was with the conditions were some numbers get equal like 111222. Also the problem statement was right and it was not <= or >= after the ai.

Code in C:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long long int n,temp;
    scanf("%lld",&n);
    if (n==1)
    {
        long long int l;
        scanf("%lld",&l);
        printf("Yes");
    }
    else
{
    long long int a,b;
    long long int last;
    int flag =1;
    long long int changes =0;
    int incr=0,decr=0;
    int equ=0;
    int dec =0 , inc =0; //flag: checking if the set is incremental or decremental till now
    scanf("%lld %lld",&a,&b);

    if (a>b)
    {
        dec=1;
    }
    else if (a<b)
    {
        inc = 1;
    }
    else
    {
        equ=1;
    }
    last = b;
    for (long long int i =2;i<n;i++)
    {

        scanf("%lld",&temp);
        if (temp > last && equ==1)
        {
            inc = 1;
            dec=0;
            equ=0;
        }
        else if (temp <last && equ==1)
        {
            inc = 0;
            dec = 1;
            equ= 0;
        }
       else if (temp>last && dec==1)
        {
            inc = 1;
            dec= 0;
            changes++;
            incr++;
        }
        else if (temp<last && inc==1)
        {
            inc =0;
            dec=1;
            changes++;
            decr++;
        }
        if (changes>=1 && temp == last && incr>=0 && decr >=0)
        {
            flag = 0;
            changes+=100;
        }
        last = temp;
    }
    if (changes <=1&& flag)
    {

        printf("Yes");
    }
    else
    {
        printf("No");
    }
}
    return 0;
}
titansarus
  • 136
  • 1
  • 7