[Easy] CodeForces - 897D Ithea Plays With Chtholly | 贪心博弈

题目链接:
http://codeforces.com/problemset/problem/897/D

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 (这里写图片描述) — 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.

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. 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.

题意
- 这是一个交互问题,给一个有n个格子从左到右排列的白板,系统每次随机给你一个1-c的数字, 请你设计一个算法,每轮把系统给你的这个数字填到白板上,如果白板的这一格已经填有数字,就将其擦去填新的数。要求这个算法在给定的m轮内填满n个格子,并保证这n个格子从左至右是一个不减序列
分析
- 这题题意有一点晕,看懂题意以后就明白了。相当于设计算法与机器玩游戏,注意到m的范围是范围,这个m还是很大的,相当于n个格子每个都可以写c/2次,那就把可能出现的数字1-c二分,如果小于等于c/2就从左边开始填,否则从右边开始填。填的时候维护一个不减序列,如果遇到空的格子直接填上去,做不到维护不增就替换第一个大于(右边填起就是小于)这个数的数字。

Code

#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long LL;
int rec[1005];

int main()
{
    int n,m,c,num,mid;
    scanf("%d%d%d",&n,&m,&c);
    mid = c / 2;
    memset(rec,0,sizeof(rec));
    while(m--){
        scanf("%d",&num);
        if(num <= mid){
            for(int i = 1;i <= n;i++){
                if(rec[i] == 0 || rec[i] > num) {
                    rec[i] = num; cout<<i<<endl;
                    break;
                    }
                if(i == n) break;
            }
        }
        else{
            for(int i = n;i >= 1;i--){
                rec[i] = num; cout<<i<<endl;break;
            }
            if(i == 1) break;
            }
        }
        bool flag = true;
        for(int i = 1;i <= n;i++){
            if(rec[i] == 0) flag = false;
        }
        if(flag) break;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值