首页 > 其他分享 >CF2001D Color Rows and Columns

CF2001D Color Rows and Columns

时间:2024-09-23 16:24:26浏览次数:1  
标签:begin Rows Color 位置 second st 选择 lst Columns

题目链接

题解

知识点:贪心,STL。

显然,子序列最长长度是数的种类数,即保证每个数都会被选到。子序列的奇数位要尽可能大、偶数位尽可能小。

我们从左到右依次选择子序列的数,为了保证每个数都能被选到,我们预处理出每个数的最晚出现位置 \(lst\) 。每次选择,只有在当前还未选择的数的 \(lst\) 的最小值之前(包括最小值位置),上一个选择位置之后(不包括上一个选择位置),并且还未被选择的数,是可以被选择的。这些可选数中,我们需要选择最大(最小)、位置靠前(给后面的数最多的被选机会)的数。

因此,我们要维护 \(lst\) 值的大小顺序,并且支持删除,需要用一个 set<int> 维护。

同时,再用两个 set<pair<int,int>> 维护待选数的大小顺序、位置顺序。

每次选择之前,将位置在最小位置之前的数加入待选数集合,将在上一个选择位置之前的数都删除。注意,每次选择时,遍历待选数集合删除效率很低,因此我们只对选择时遇到的数判断是否删除,如此每个数只会判断一次。

在所有数都被选择过后,生成的序列即为答案。

时间复杂度 \(O(n \log n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int a[300007], lst[300007];
bool solve() {
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i], lst[a[i]] = i;

    set<int> st;
    for (int i = 1;i <= n;i++) if (lst[i]) st.insert(lst[i]);

    int pos = 1, pre = 0;
    set<pair<int, int>> st_min, st_max;
    vector<int> ans;
    while (st.size()) {
        while (pos <= *st.begin()) {
            if (lst[a[pos]]) {
                st_min.insert({ a[pos], pos });
                st_max.insert({ -a[pos], pos });
            }
            pos++;
        }
        while (!lst[st_min.begin()->first] || st_min.begin()->second <= pre) st_min.erase(st_min.begin());
        while (!lst[-st_max.begin()->first] || st_max.begin()->second <= pre) st_max.erase(st_max.begin());
        int val = ans.size() & 1 ? st_min.begin()->first : -st_max.begin()->first;
        pre = ans.size() & 1 ? st_min.begin()->second : st_max.begin()->second;
        ans.push_back(val);
        st.erase(lst[val]);
        lst[val] = 0;
    }

    cout << ans.size() << '\n';
    for (auto val : ans) cout << val << ' ';
    cout << '\n';

    return true;
}

int main() {
    std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << -1 << '\n';
    }
    return 0;
}

标签:begin,Rows,Color,位置,second,st,选择,lst,Columns
From: https://www.cnblogs.com/BlankYang/p/18427231

相关文章

  • Java反序列化利用链篇 | JdbcRowSetImpl利用链分析
    JdbcRowSetImpl利用链前言首先说明一下:利用链都有自己的使用场景,要根据场景进行选择不同的利用链。JdbcRowSetImpl利用链用于fastjson反序列化漏洞中。为什么?因为fastjson会在反序列化类时自动调用set开头的方法(不一定是setter方法),而JdbcRowSetImpl中存在一个set开头的方法,即......
  • WPF dynamically generate grid with calculated rows and columns, put the custom c
    privatevoidFillGridRandomly(){rowsColsSet=newHashSet<XY>();if(gd!=null){gd.Children.Clear();}for(inti=0;i<50;i++){while(true){XYxy=newXY();......
  • WPF System.Windows.Media.Color A value must be set, display ball and number in c
    privateColorGetRndColor(){Colorcr=newColor();cr.A=255;cr.R=(byte)(rnd.Next(0,255));cr.G=(byte)(rnd.Next(0,255));cr.B=(byte)(rnd.Next(0,255));returncr;}         //usercontrol.......
  • Python使用browser_cookie3库来读取浏览器Cookies
    browser_cookie3是一个用于从浏览器中提取Cookies的Python模块。下面是使用该模块的步骤:1.安装browser_cookie3模块。pipinstallbrowser_cookie32.导入browser_cookie3模块。 import browser_cookie33.提取浏览器Cookies。可以使用下面的代码提取GoogleC......
  • WPF WebBrowser suppress script errors
    ScriptError,Anerrorhasoccuredinthescriptonthispage.Doyouwanttocontinuerunningscriptsonthispage?   //xaml<Windowx:Class="WpfApp378.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/present......
  • AGC026D Histogram Coloring 题解
    [AGC026D]HistogramColoring题解给定\(n\)列的网格,每列高为\(h_i\),将每个格子染色成红色或蓝色,使得每个\(2\times2\)的区域都恰好有两个蓝格子和两个红格子,求方案数(对\(10^9+7\)取模)。\(1\leqn\leq100,1\leqh_i\leq10^9\)性质为了方便讲述,先假设\(h_i=h_{i+......
  • 【鸿蒙应用开发】常见的容器组件:ColumnSplit、RowSplit和Flex
    上一章已经了解了Column和Row的一些属性,以下是几个案例:设置子组件水平方向的间距为:5@Entry@Preview@ComponentstructIndex{@Statemessage:string='Hello鸿蒙';controller:webview.WebviewController=newwebview.WebviewController();build(){Column(......
  • EmbeddedBrowserWebView.dll文件丢失导致程序无法运行问题
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个EmbeddedBrowserWebView.dll文件(挑选合适的......
  • GEOG 2500 Web Browsing
    GEOG2500–Fall2024Lab1:WebBrowsing&IntroductiontoESRIWebTraining  Objectives:•   Becomefamiliarwiththewwwto learnaboutGIS andto access Geographic Data•   Locatewebsitesthatcan be useful inyourGISworld• ......
  • PDshell16反向pgsql中 Unable to list the columns. SQLSTATE = 22003不良的类型值 sh
    问题原因:pdshell逆向pg的sql脚本滞后,与pg新版本不兼容,解决方案:修改掉不兼容的sql代码1、Database->EditCurrentDBMS,如下 2、PostgreSQL9.x->Script->Objects找到Column和Key;如下 3、将Column->SqlListQuery选项里SELECT中的c.attnotnull替换为cast(nullif(c.att......