C - Cats Gym - 102875C
生活随笔
收集整理的這篇文章主要介紹了
C - Cats Gym - 102875C
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C - Cats Gym - 102875C
題意:
n個貓,貓的身高在1到20之間,現在求這些貓的排列,滿足一樣高的貓不靠著,且他們之間的最矮的貓不比他們高
輸出任意符合條件的排列
題解:
構造題
題目的限制條件決定了,矮貓的周圍都是高的貓,所以我們可以這樣,一開始1,然后1的左右生成2,也就是2 1 2 ,然后2的左右生成3,也就是3 2 3 1 3 2 3 ,然后3的左右生成4…
如何實現?
用兩個棧來回導即可
代碼:
#include<bits/stdc++.h> typedef long long ll; using namespace std; inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);return s*w; } stack<int>s1; stack<int>s2; int main() {int n;cin>>n;s1.push(1);int tot=1;//總量 int ans=1;//加入的當前高度 int f=0;while(tot<n){if(f==0){while(s1.empty()==0){if(s1.top()==ans){s2.push(ans+1);s2.push(s1.top());s2.push(ans+1);tot+=2;}else {s2.push(s1.top());}s1.pop();}ans++;}else {while(s2.empty()==0){if(s2.top()==ans){s1.push(ans+1);s1.push(s2.top());s1.push(ans+1);tot+=2;}else {s1.push(s2.top());}s2.pop();}ans++;}f^=1;}if(f==0){for(int i=1;i<=n;i++){cout<<s1.top()<<" ";s1.pop();} }else {for(int i=1;i<=n;i++){cout<<s2.top()<<" ";s2.pop();}}return 0; }總結
以上是生活随笔為你收集整理的C - Cats Gym - 102875C的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长存 TLC 颗粒:光威弈系列 4T 固
- 下一篇: J - Just Multiplicat