题解 | 【CF896B】 Ithea Plays With Chtholly

题目链接:Here

(m) 个数,放到 (1→n) 一个位置上,若 (1→n) 都被填满且不下降就胜。强制在线。

看到题忽然觉得是水题,这不就最长不下降子序列的那个吗!直接上个二分就准备交了。

事实证明头铁了(怎么可能这么简单,2000分啊!)

反例:

15 5 9 
29 1 8 1 7

若一个从前往后一个从后往前就可以完成,而那个做法还只填了 1 1 7 呢!

问题出在哪里?

  • 最长不下降子序列要求相对位置不变,而这个可以变。

但别着急放弃。这是一个对的做法,只是浪费的步数多一点,那么浪费多少步呢?每一个最多被填 (1) 次并覆盖 (c−1) 次,所以最多要 (n×c) 步。
啊!那 (m) 的限制不是这个步数的一半吗?那么就搞两个上面的,头一个尾一个,然后按照和 (frac c2) 的大小关系分开就好了。

【AC Code】

const int N = 1e3 + 10;
int a[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, m, c, cnt = 0;
    cin >> n >> m >> c;
    for (int i = 1, x; i <= m; ++i) {
        cin >> x;
        if (x <= c / 2) {
            int j;
            for (j = 1; j <= n && a[j] != 0 && a[j] <= x; ++j);
            cout << j << endl;
            a[j] = x;
        } else {
            int j;
            for (j = n; j >= 1 && a[j] != 0 && a[j] >= x; j -= 1);
            cout << j << endl;
            a[j] = x;
        }
    }
}

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/15243322.html