0
#include <stdio.h>
int main(){ 
    int n, g, i;
    scanf("%d\n", &n);
    while(n--) {
        int l = -1;
        int c = 1;
        scanf("%d", &g);
        while(g--) {
            scanf("%d", &i); 
            if (l == -1) l = i;
            else if (i - 1 != l) break;
            else l++;
            c++;
        }
        fflush(stdin);
        printf("%d\n", c);
    }
} 

I get all the outputs correctly, so I have no idea what could be wrong. On the other hand, Kattis accepts this code below that I found on GitHub, and the outputs are exactly the same as in my code. If anyone can explain to me what is wrong or why Kattis rejects my code I would appreciate it.

#include <bits/stdc++.h>

using namespace std;

int main()
{
    //Initialize n and g, take in n
    int n, g;
    cin >> n;

    //Iterate n times
    while (n--)
    {
        //Take in g, initialize empty vector of size g
        cin >> g;
        vector<int> gnomes(g);

        //Take in all the gnomes
        for (int i = 0; i < g; i++) cin >> gnomes[i];

        //Iterate through without the beginning or end since king won't be there
        for (int i = 1; i < g-1; i++)
        {
            //Must break the order, and if you remove it the gnomes around it should be in order
            if (gnomes[i] < gnomes[i-1] || gnomes[i] > gnomes[i+1] && gnomes[i-1] < gnomes[i+1])
            {
                //Output the 1 based index, so add 1
                cout << i+1 << endl;

                //And exit to the next group
                break;
            }
        }
    }
}
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
  • 1
    [Using fflush(stdin)](https://stackoverflow.com/questions/2979209/using-fflushstdin) may be a problem. – Retired Ninja Jun 06 '22 at 01:57
  • Accepting anything with `#include ` might be another problem. Don't trust anyone accepting such code. – Goswin von Brederlow Jun 06 '22 at 02:26
  • 1
    I don't see any resemblance between the two programs. The one that works performs some comparisons between neighboring elements of the array; yours makes no such comparisons. If nothing else, your program may break out of the loop after reading fewer than `g` numbers, which will throw off subsequent tests; whereas the program that works always reads all `g` numbers for every test. – Igor Tandetnik Jun 06 '22 at 03:39
  • @GoswinvonBrederlow it's horrible practice, but also a standard idiom in competitive programming, so not unusual at all here. – ggorlen Jun 08 '22 at 17:18
  • @ggorlen I think we should patch libstdc++ to change "bits" to some random string on every build making it impossible to include the internal files from one compiler version to the next. – Goswin von Brederlow Jun 08 '22 at 17:32
  • @GoswinvonBrederlow That'll show 'em! – ggorlen Jun 08 '22 at 18:20

1 Answers1

0

First of all, if you're using C++, use C++, not C!

As pointed out, fflush(stdin) is undefined behavior. Instead, you can use std::getline to chew up the rest of the line.

Other than that, your logic looks fine, even though it's a different approach than the overkill C++ solution. There's no need to check i + 1 or use a vector, as you seem to have deduced.

I suggest using clearer naming and whitespace conventions, though.

Here's a C solution, using scanf to read the rest of the line:

#include <stdio.h>

int main() {
    int n;
    scanf("%d\n", &n);

    for (int i = 0; i < n; i++) {
        int previous = -1;
        int g;
        scanf("%d", &g);

        for (int j = 0; j < g; j++) {
            int current;
            scanf("%d", &current);

            if (previous >= 0 && previous + 1 != current) {
                printf("%d\n", ++j);

                for (; j < g; j++) {
                    scanf("%d", &current); 
                }

                break;
            }

            previous = current;
        }
    }
} 

Here's a C++ solution that uses bits/stdc++.h and using namespace std;, which are common idioms in competitive programming but should never be used in any application code.

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int g;
        cin >> g;
        int previous = -1;

        for (int j = 0; j < g; j++) {
            int current;
            cin >> current;

            if (previous >= 0 && previous + 1 != current) {
                cout << (1 + j) << "\n";
                string s;
                getline(cin, s);
                break;
            }

            previous = current;
        }
    }
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106