【CodeForces - 897D】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.
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 3Output
1 2 2Note
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?3in 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. Also because of this, please be careful when hacking others' codes.?In the sample, Chtholly won the game after the?3rd round, so it is required that your program doesn't read the number of the remaining?4th round.
The input format for hacking:
- The first line contains 3 integers?n,?m?and?c;
- The following?m?lines each contains an integer between?1?and?c, indicating the number given to Chtholly in each round.
題目大意:
給一個(gè)有n個(gè)格子從左到右排列的白板,系統(tǒng)每次隨機(jī)給你一個(gè)1-c的數(shù)字, 請(qǐng)你設(shè)計(jì)一個(gè)算法,每輪把系統(tǒng)給你的這個(gè)數(shù)字填到白板上,如果白板的這一格已經(jīng)填有數(shù)字,就將其擦去填新的數(shù)。要求這個(gè)算法在給定的m輪內(nèi)填滿n個(gè)格子,并保證這n個(gè)格子從左至右是一個(gè)不減序列?。每次輸出填到哪里。
解題報(bào)告:
? ?注意到m的范圍,給了很明顯的提示了,這個(gè)m還是很大的,相當(dāng)于n個(gè)格子每個(gè)都可以寫c/2次,那就把可能出現(xiàn)的數(shù)字1-c二分,如果小于等于c/2就從左邊開始填,否則從右邊開始填,那么一定可以構(gòu)造出來。填的時(shí)候左側(cè)維護(hù)一個(gè)遞增序列,右側(cè)維護(hù)一個(gè)遞減序列,如果遇到空的格子直接填上去,做不到維護(hù)不增就替換第一個(gè)大于(右邊填起就是小于)這個(gè)數(shù)的數(shù)字。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; int now; int n,m,c,a[MAX]; int main() {cin>>n>>m>>c;c/=2;while(m--) {int x;scanf("%d",&x);if(x <= c) {for(int i = 1; i<=n; i++) {if(a[i] == 0 || a[i] > x) {a[i] = x;printf("%d\n",i);//cout << i << endl;fflush(stdout);break;}}}else {for(int i = n; i>=1; i--) {if(a[i] == 0 || a[i] < x) {a[i] = x;printf("%d\n",i);//cout << i << endl;fflush(stdout);break;}}}now = 0;for(int i = 1; i<=n; i++) {if(a[i] != 0) now++;}if(now == n) break;}return 0 ;}?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 897D】Ithea Plays With Chtholly (交互题型,贪心,思维构造,题目信息)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【LightOJ - 1030】Disc
- 下一篇: wincomp.exe - wincom