文章

211

粉丝

0

获赞

999

访问

33.7k

头像
巨人排队 题解:经典的Dilworth定理应用:最少递减序列数 = 最长递增子序列长度
P1677 中南大学机试题
发布于2026年2月11日 22:19
阅读数 315

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

int main() {  
    int n;
    while (cin >> n) {
        vector<int> h(n);
        for (int i = 0; i < n; i++) 
			cin >> h[i];      
        // tails[i] = 所有长度为 i+1 的递增子序列中,结尾元素的最小值
        vector<int> tails;        
        for (int x : h) {
            // 在 tails 中找第一个 >= x 的位置
            // 如果找不到,返回 tails.end()
            auto it = lower_bound(tails.begin(), tails.end(), x);           
            if (it == tails.end()) {
                // x 比所有 tails 元素都大
                // 可以形成更长的递增子序列
                tails.push_back(x);
            } else {
                // *it >= x,用 x 替换 *it
                // 这样同样长度的子序列有更小的结尾,更优
                *it = x;
            }
        }        
        // tails 的长度 = 最长递增子序列的长度
        cout << tails.size() << endl;
    }    
    return 0;
}

 

登录查看完整内容


登录后发布评论

1 条评论
yauqq
2026年2月11日 22:23

关键结论:最少队伍数 = 最长递增子序列(LIS)的长度

原因:递增序列中的每个元素都必须放在不同队伍(因为递减队伍中不能有递增关系)。

赞(0)
回复给: