CodeForces - 1304D Shortest and Longest LIS(构造+贪心)
題目鏈接:點(diǎn)擊查看
題目大意:題目給出 n - 1 個(gè)大小關(guān)系,分別代表一個(gè)長(zhǎng)度為 n 的數(shù)列各個(gè)位置的相對(duì)大小,現(xiàn)在需要我們用兩個(gè)符合相對(duì)大小關(guān)系的 1 ~?n 的一個(gè)排列,且該排列的最長(zhǎng)不下降子序列分別最小和最大
題目分析:讀完題后不難想到,如果我們想讓最長(zhǎng)不下降子序列最長(zhǎng)的話,那么整個(gè)序列必然是呈整體上升的趨勢(shì),最小的話相應(yīng)的是整個(gè)序列呈下降趨勢(shì),因?yàn)槎嗔讼鄬?duì)大小這個(gè)約束,我們可以使得符號(hào)相同的連續(xù)序列分成不同的區(qū)塊,每個(gè)區(qū)塊只要保持整體上升的趨勢(shì)就行,換句話說(shuō),必須保證后一個(gè)區(qū)塊的最小值比前一個(gè)區(qū)塊的最大值還要大才行,這樣我們善于運(yùn)用一下reverse函數(shù)就可以輕松實(shí)現(xiàn)了,比賽的時(shí)候因?yàn)锽題讀錯(cuò)題了慌了神,導(dǎo)致這個(gè)簡(jiǎn)單題沒(méi)有出,坐等掉分
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;char s[N];int ans[N];int main() { //#ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); //#endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--){int n;scanf("%d%s",&n,s+1);for(int i=1;i<=n;i++)ans[i]=n-i+1;for(int i=1;i<=n-1;)//整體下降 {int j=i;while(s[j]=='<')j++;reverse(ans+i,ans+j+1);i=j+1;}for(int i=1;i<=n;i++)printf("%d ",ans[i]);printf("\n");for(int i=1;i<=n;i++)ans[i]=i;for(int i=1;i<=n-1;)//整體上升 {int j=i;while(s[j]=='>')j++;reverse(ans+i,ans+j+1);i=j+1;}for(int i=1;i<=n;i++)printf("%d ",ans[i]);printf("\n");}return 0; }?
超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的CodeForces - 1304D Shortest and Longest LIS(构造+贪心)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CodeForces - 1301C A
- 下一篇: CodeForces - 1301E 1