• [USACO18DEC]Sort It Out P


    初看本题毫无思路,只能从特殊的 (K = 1) 出发。

    但是直接考虑构造一组字典序最小的方案还是不好构造,可以考虑先手玩一下样例。通过自己手玩的样例可以发现,貌似没有被选出来的数在原排列中都是递增的子序列。比如说 (1 6 5 3 4 2) 可以证明 (2 5 6) 是字典序最小的方案,那么剩下的数按照在原排列中的顺序依次写下是 (1 3 4) 是一个递增序列。可以思考一下为什么会有这样的结论出现。

    不难发现不论我们怎么操作,原来序列中顺序对的相对位置必然是不会变动的。比如说上方的 (1 3 4) 不论怎么变换 (1) 都会在 (3) 之前,(3) 都会在 (4) 之前。并且你会发现将剩下的所有数按照字典序依次进行操作是能让这个排列重新排好的。于是我们可以得到一个结论,原排列中的任意一个递增序列保留下来剩下的作为一个操作集合都能时这个排列重新排好。

    既然如此,为了让操作集合字典序最小,相反我们需要让保留集合字典序最大。因此保留集合就一定需要选择最长递增子序列中字典序最大的那个。可以考虑每次贪心地选取能拼成最长递增子序列中的最大的元素即可,具体实现可以先求出 (dp_i) 表示以 (i) 位置结尾的最长递增子序列长度。最后将每个位置按照 (dp) 值为第一关键字,按照 (a_i) 为第二关键字排序即可。

    那么回来思考原问题,字典序第 (K) 小的方案怎么求。不难发现实际上我们还是要求保留集合第 (K) 大的方案。于是可以考虑从低到高位逐步确定选择的集合,那么我们就需要求出 (dp_i) 表示以 (i) 开头的最长递增子序列的数量。可以先考虑一个朴素的求法,令 (f_i) 为以 (i) 开头的最长递增子序列的长度,那么会有转移:

    [dp_i = sumlimits_{f_i = f_j + 1, a_i < a_j, i < j} dp_j ]

    实际上又因为 (f_i = maxlimits_{a_i < a_j, i < j} f_j + 1),实际上我们的 (dp) 就是求权值在一段后缀中 (f) 最大值的数量,这个可以直接在权值线段树上实现,于是我们就将求 (dp_i) 的复杂度降至 (O(n log n))

    那么最后确定第 (K) 大的保留集合时,将所有 (dp) 值相同的位置压入到一个 (vector) 当中并按照权值为第二关键字排序,依次确定每个位即可。

    需要注意的是 (dp_i) 的数量可能会爆 (long long) 但因为我们只需要和 (K) 比大小所以需要随时将 (dp) 值与 (1e18)(min)

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define ls (p << 1)
    #define rs (p << 1 | 1)
    #define mid (l + r >> 1)
    #define rep(i, l, r) for (int i = l; i <= r; ++i)
    #define dep(i, l, r) for (int i = r; i >= l; --i)
    const int N = 100000 + 5;
    const int inf = 1000000000000000001;
    struct tree {
    	int mx, cnt;
    }dp[N], t[N << 2];
    int n, k, len, ans, a[N], book[N];
    vector <int> G[N];
    int read() {
    	char c; int x = 0, f = 1;
    	c = getchar();
    	while (c > '9' || c < '0') { if(c == '-') f = -1; c = getchar();}
    	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    	return x * f;
    }
    tree up(tree x, tree y) {
    	tree ans;
    	if(x.mx > y.mx) ans = x;
    	else if(x.mx < y.mx) ans = y;
    	else ans.mx = x.mx, ans.cnt = min(x.cnt + y.cnt, inf);
    	return ans;
    }
    void update(int p, int l, int r, int x, int y, tree k) {
    	if(l >= x && r <= y) { t[p] = k; return;}
    	if(mid >= x) update(ls, l, mid, x, y, k);
    	if(mid < y) update(rs, mid + 1, r, x, y, k);
    	t[p] = up(t[ls], t[rs]);
    }
    tree query(int p, int l, int r, int x, int y) {
    	if(l >= x && r <= y) return t[p];
    	tree ans; ans.mx = ans.cnt = 0;
    	if(mid >= x) ans = up(ans, query(ls, l, mid, x, y));
    	if(mid < y) ans = up(ans, query(rs, mid + 1, r, x, y));
    	return ans;
    }
    bool cmp(int x, int y) {
    	return a[x] > a[y];
    }
    signed main() {
    	n = read(), k = read();
    	rep(i, 1, n) a[i] = read();
    	dep(i, 1, n) {
    		dp[i] = query(1, 1, n, a[i], n);
    		++dp[i].mx, dp[i].cnt = (dp[i].mx == 1 ? dp[i].cnt + 1 : dp[i].cnt);
    		update(1, 1, n, a[i], a[i], dp[i]);
    		G[dp[i].mx].push_back(i), len = max(len, dp[i].mx);
    	}
    	rep(i, 1, len) sort(G[i].begin(), G[i].end(), cmp);
    	int P = 0; 
    	dep(i, 1, len) {
    		for (int j = 0; j < G[i].size(); ++j) if(G[i][j] > P && a[G[i][j]] > a[P]) {
    			if(dp[G[i][j]].cnt < k) k -= dp[G[i][j]].cnt;
    			else { P = G[i][j]; break;}
    		}
    		book[a[P]] = 1; if(P != n + 1 && !ans) ans = i; 
    	}
    	printf("%lld
    ", n - ans);
    	rep(i, 1, n) if(!book[i]) printf("%lld
    ", i);
    	return 0;
    }
    

    值得一提的是,在这种最优性问题毫无思路时,可以通过手玩样例发现一些策略和性质。

    GO!
  • 相关阅读:
    强连通分量(Kosaraju)
    拓扑排序
    树状数组BIT
    差分
    RMQ(ST表)
    LCA(Tarjan)
    LCA(ST倍增)
    海亮SC2019 树上数数(转载)
    海亮SC
    【十二省联考2019】异或粽子/可持久化01trie
  • 原文地址:https://www.cnblogs.com/Go7338395/p/13708296.html
Copyright © 2020-2023  润新知