[蓝桥杯2020国赛]游园安排
生活随笔
收集整理的這篇文章主要介紹了
[蓝桥杯2020国赛]游园安排
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
題解:
本質就是求最長上升子序列,只不過這里是字符串版本的,我們都知道有n^2的LIS,但其實還有O(nlogn)版本的,詳細看這里,套上就行
另外我發現這里竟然有藍橋杯全套的編程題離譜,貼上本題地址
代碼:
#include <iostream> #include<algorithm> #include<cstring> #include<string> #include<bits/stdc++.h> using namespace std; const int maxn = 1e6; string a[maxn]; string b[maxn]; int pos[maxn], ans[maxn]; int main() {string s;cin >> s;int cnt = 0;for (int i = 0; i < s.size(); i++) {if (s[i] >= 'A' && s[i] <= 'Z') a[++cnt] = s[i];else a[cnt] += s[i];}int len = 1;b[1] = a[1];pos[1] = len;for (int i = 2; i <= cnt; i++) {if (a[i]>b[len]) {//a大于bb[++len] = a[i];//加入bpos[i] = len;//a中第i個數在b中的位置}else {int id = lower_bound(b + 1, b + 1 + len, a[i]) - b;//找到b中大于a[i]的第一個位置b[id] = a[i];//該位置用當前a替換pos[i] = id;}}int tmp = len;string maxx = "Zzzzzzzzzz";for (int i = cnt; i >= 1; i--) {if (!tmp)break;if (pos[i] == tmp && maxx>a[i]) {ans[tmp] = i;--tmp;maxx = a[i];}}for (int i = 1; i <= len; i++) {cout << a[ans[i]];}cout << endl;return 0; }總結
以上是生活随笔為你收集整理的[蓝桥杯2020国赛]游园安排的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何使用多种方法将图像添加到PDF文件?
- 下一篇: 新手学习电脑渗透的基础知识掌握