Codeforces 467E Alex and Complicated Task(高效),codeforces467e

來源:互聯網
上載者:User

Codeforces 467E Alex and Complicated Task(高效),codeforces467e

題目連結:Codeforces 467E Alex and Complicated Task

題目大意:給定一個長度為n序列,然後從中挑選盡量多的4元組(不能重疊)。

解題思路:每次找的四元組的左端肯定是要盡量小的。所以用一個單調棧維護,如果新加入的數x在棧中出現過,那麼就將兩個數之間的數標記為在x。如果一個數的標記不為空白,就意味著找到對應的四元組。有因為序列是從左遍曆過去的,所以找到的一定是最優的。

#include <cstdio>#include <cstring>#include <map>#include <stack>#include <vector>#include <algorithm>using namespace std;const int maxn = 5 * 1e5 + 5;int N, B[maxn];void solve () {    vector<int> ans;    map<int, int> pre, sum;    int mv = 0;    while (mv < N) {        pre.clear();        sum.clear();        stack<int> sta;        for (int& i = mv; i < N; i++) {            if (pre[B[i]]) {                for (int j = 0; j < 2; j++) {                    ans.push_back(pre[B[i]]);                    ans.push_back(B[i]);                }                break;            }            while (!sta.empty() && (sum[B[i]] > 1 || (sum[B[i]] == 1 && sta.top() != B[i]))) {                pre[sta.top()] = B[i];                sum[sta.top()]--;                sta.pop();            }            sta.push(B[i]);            sum[B[i]]++;        }        mv++;    }    printf("%lu\n", ans.size());    for (int i = 0; i < ans.size(); i++)        printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' ');}int main () {    scanf("%d", &N);    for (int i = 0; i < N; i++)        scanf("%d", &B[i]);    solve();    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.