Problem: cf896_b
Difficulty: easy
Categories: greedy, search
Time limit: 1000 ms, memory limit: 256 MB

B. Ithea Plays With Chtholly

This is an interactive problem. Refer to the Interaction section below for better understanding.

Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight.

![](https://espresso.codeforces.com/2dba15fc5c436b23cd2988708f9cd30993b18178.png)

Initially, Ithea puts *n* clear sheets of paper in a line. They are numbered from 1 to *n* from left to right.

This game will go on for *m* rounds. In each round, Ithea will give Chtholly an integer between 1 and *c*, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one).

Chtholly wins if, at any time, all the sheets are filled with a number and the *n* numbers are in non-decreasing order looking from left to right from sheet 1 to sheet *n*, and if after *m* rounds she still doesn't win, she loses the game.

Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number.

Input

The first line contains 3 integers *n*, *m* and *c* (![](https://espresso.codeforces.com/257604f27c6d773d788295abb7197dacaf673770.png), ![](https://espresso.codeforces.com/69d952fbb38d40c3c6e095688911e180261b4c6c.png) means ![](https://espresso.codeforces.com/8f85bbe2572c28c30e72b9446b520cbdeb7eabe7.png) rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process.

Interaction

In each round, your program needs to read one line containing a single integer *p**i* (1 ≤ *p**i* ≤ *c*), indicating the number given to Chtholly.

Your program should then output a line containing an integer between 1 and *n*, indicating the number of sheet to write down this number in.

After outputting each line, don't forget to flush the output. For example:

* fflush(stdout) in C/C++;
* System.out.flush() in Java;
* sys.stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.

If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game.

Example

Input

```
2 4 4  
2  
1  
3
```

Output

```
1  
2  
2
```

Note

In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game.

Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected.


Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;




static long long queries = 0;      
static long long query_limit = 0;  
static int n, m, c;                
static vector<int> sheets;         




static void log_metrics() {
    
    if (query_limit == 0) return;
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}




static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}




int main(int argc, char* argv[]) {
    
    registerInteraction(argc, argv);
    atexit(log_metrics);

    
    
    
    n = inf.readInt();
    m = inf.readInt();
    c = inf.readInt();

    
    query_limit = m;
    
    
    log_metrics();

    
    
    
    sheets.assign(n, 0);

    
    
    cout << n << " " << m << " " << c << endl;

    
    for (int round = 1; round <= m; round++) {
        
        
        
        int p = inf.readInt();

        
        cout << p << endl;

        
        string token;
        if (!(cin >> token)) {
            finish(_wa, "Unexpected EOF or reading error from contestant output");
        }

        
        
        
        bool is_num = true;
        size_t start_idx = 0;
        if (token.size() > 0 && token[0] == '+') start_idx = 1;
        if (token.size() > 0 && token[0] == '-') is_num = false; 
        if (start_idx >= token.size()) is_num = false;

        for (size_t i = start_idx; i < token.size(); i++) {
            if (!isdigit(token[i])) {
                is_num = false;
                break;
            }
        }

        if (!is_num) {
            finish(_wa, "Invalid format: expected integer index 1..N, got '" + token + "'");
        }

        long long idx = 0;
        try {
            idx = stoll(token);
        } catch (...) {
            finish(_wa, "Integer parse error or overflow: " + token);
        }

        
        queries++;
        
        
        if (queries % 10 == 0) log_metrics();

        
        if (idx < 1 || idx > n) {
            finish(_wa, format("Index out of bounds: %lld (must be 1..%d)", idx, n));
        }

        
        sheets[idx - 1] = p;

        
        
        bool filled = true;
        for (int x : sheets) {
            if (x == 0) {
                filled = false;
                break;
            }
        }

        if (filled) {
            bool sorted = true;
            for (int i = 0; i < n - 1; i++) {
                if (sheets[i] > sheets[i+1]) {
                    sorted = false;
                    break;
                }
            }
            if (sorted) {
                finish(_ok, format("Win after %d rounds", round));
            }
        }
    }

    
    finish(_wa, format("Failed to win after %d rounds", m));

    return 0;
}


Adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;


long long queries = 0;
long long query_limit = 0;



void log_metrics() {
    
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}


template <typename... Args>
void finish(TResult verdict, const char* fmt, Args... args) {
    log_metrics();
    quitf(verdict, fmt, args...);
}



struct LCG {
    long long state;
    int mod_val;

    LCG(long long seed, int m) : state(seed), mod_val(m) {}

    int next() {
        
        state = (state * 48271LL + 239LL) % 2147483647LL;
        
        return 1 + (int)(state % mod_val);
    }
};

int main(int argc, char* argv[]) {
    
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    int n = inf.readInt();
    int m = inf.readInt();
    int c = inf.readInt();

    
    query_limit = m;

    
    
    log_metrics();

    
    cout << n << " " << m << " " << c << endl;

    
    
    vector<int> board(n + 1, 0);
    int filled_count = 0;

    
    
    LCG rng(n * 1009LL + m * 101LL + c + 1337LL, c);

    
    for (int round = 1; round <= m; round++) {
        
        int p;
        inf.skipBlanks();
        if (!inf.eof()) {
            
            p = inf.readInt();
        } else {
            
            p = rng.next();
        }

        
        queries = round;
        
        
        if (round % 100 == 0) log_metrics();

        
        cout << p << endl;

        
        int idx;
        if (!(cin >> idx)) {
            finish(_wa, "Contestant output stream closed unexpectedly or invalid format");
        }

        
        if (idx < 1 || idx > n) {
            finish(_wa, "Sheet index %d is out of bounds [1, %d]", idx, n);
        }

        
        if (board[idx] == 0) {
            filled_count++;
        }
        board[idx] = p;

        
        
        
        if (filled_count == n) {
            bool sorted = true;
            for (int i = 1; i < n; i++) {
                if (board[i] > board[i+1]) {
                    sorted = false;
                    break;
                }
            }
            if (sorted) {
                finish(_ok, "Contestant won in %d rounds", round);
            }
        }
    }

    
    finish(_wa, "Contestant failed to win within m=%d rounds", m);

    return 0;
}


Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);

    
    
    string mode = opt<string>("mode", "non");
    
    
    int n = opt<int>("n", -1);
    int c = opt<int>("c", -1);
    int m = opt<int>("m", -1);
    
    int seed = atoi(argv[1]);

    
    
    if (n == -1) {
        if (seed == 1) n = 2;
        else if (seed == 2) n = 50;
        else if (seed == 3) n = 10;
        else if (seed == 4) n = 50;
        else n = rnd.next(2, 50);
    }

    
    
    if (c == -1) {
        if (seed == 1) c = 2;
        else if (seed == 2) c = 100;
        else if (seed == 3) c = 10;
        else if (seed == 4) c = 2;
        else c = rnd.next(2, 100);
    }

    
    
    
    if (m == -1) {
        m = n * c;
    }

    
    cout << n << " " << m << " " << c << endl;

    if (mode == "non") {
        
        
        int type = rnd.next(0, 4);
        
        for (int i = 0; i < m; i++) {
            int val;
            if (type == 0) {
                
                val = rnd.next(1, c);
            } else if (type == 1) {
                
                val = rnd.next(1, max(1, c / 2));
            } else if (type == 2) {
                
                int low = min(c, c / 2 + 1);
                val = rnd.next(low, c);
            } else if (type == 3 && c >= 4) {
                
                if (rnd.next(2)) 
                    val = rnd.next(1, c / 4);
                else 
                    val = rnd.next(c - c / 4 + 1, c);
            } else {
                
                val = rnd.next(1, c);
            }
            cout << val << "\n";
        }
    } else {
        
        
    }

    return 0;
}