Codeforces Round #449 (Div. 1) B. Ithea Plays With Chtholly

B. Ithea Plays With Chtholly
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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 (, means 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 pi (1 ≤ pi ≤ 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

题意,有n块板子,m次操作,c是每次给出的数的最大值,然你对于每次输入都输出一块板子,要求在m次操作内每个板子都有数,并且从左到右非严格递增
做法:对于大于c/2的数,从n到1遍历,找到第一个小于它或者没有填过的板子,对于大于c/2的数,从1到n找到第一个大于它,或者为0的板子,因为小于c/2的数最多进过c/2-1次就可以变成1,右边的数最多经过c/2-1就可以变成c,所以n*c/2次就可了。

#include<bits/stdc++.h>
using namespace std;
int n,m,c;
int num[1005];
int main(){
    cin >> n >> m >> c;
    for(int i = 1;i <= m;i ++){
        int now;
        cin >> now;
        if(now*2 <= c){
            for(int j = 1;j <= n;j ++){
                if(num[j] > now||num[j] == 0){
                    num[j] = now;
                    printf("%d\n",j);
                    fflush(stdout);
                    break;
                }
            }
        }
        else{
            for(int j = n;j >= 1;j --){
                if(num[j] < now || num[j] == 0){
                    num[j] = now;
                    printf("%d\n",j);
                    fflush(stdout);
                    break;
                }
            }
        }
        bool tmp = true;
        for(int i = 1;i < n;i ++){
            if(num[i] > num[i+1] || num[i] == 0) tmp = false;
        }
        if(num[n] == 0) tmp = false;
        if(tmp) return 0;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zstu_zy/article/details/78701603