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;
}