Description
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次轮回中能实现所有的数能够满足非递减序列。给定的数的范围为[1,c],对于每次给定的数,输出对应的选择的纸片的位置,一旦满足非递减序列,终止程序。
解题思路
由题意可知, n∗⌈c2⌉<=m 。假如填入数字的策略只是选择从前向后或者从后向前的话,最差情况下每个纸片都需要填写c次,则总共需要n*c次轮回,不符合条件。所以我们采取分开前后两侧判断的方法,以 c2 为分界点,当给定的数字<= c2 时,则从前向后遍历,选择第一个大于给定数字或者还未填写过数字的纸片;否则,从后向前遍历,选择第一个小于给定数字或者还未填写过数字的纸片。这样最差情况下每个纸片需要填写的次数降低为 c2 ,对应总的填写次数为 n∗c2 ,满足题意。
代码实现
#include<bits/stdc++.h>
using namespace std;
#define maxn 1007
#define maxx 0x3f3f3f
int a[maxn];
int n;
bool judge()
{
bool flag=false;
for(int i=1; i<n; i++)
if(a[i]>a[i+1]||a[i]==0)
{
flag=true;
break;
}
if(a[n]==0) flag=true;
return flag;
}
int main()
{
ios::sync_with_stdio(false);
int m,c,t,i;
cin>>n>>m>>c;
a[1]=maxx;
if(n==1)
{
cin>>t;
cout<<"1"<<endl;
return 0;
}
else
{
while(judge())
{
cin>>t;
if(t*2<=c)
{
for(i=1; i<n; i++)
if(a[i]>t||a[i]==0) break;
cout<<i<<endl;
fflush(stdout);
a[i]=t;
}
else
{
for(i=n; i>1; i--)
if(a[i]<t||a[i]==0) break;
cout<<i<<endl;
fflush(stdout);
a[i]=t;
}
}
}
return 0;
}