SGU 191 Exhibition (贪心+构造)
来源:互联网 发布:mac终端命令退出编辑 编辑:程序博客网 时间:2024/06/14 14:52
题意:这题题意好难理解……意思是说有两个公司A、B,他们要展览物品,但是A公司的展柜要放B公司的物品,B公司的展柜要放A公司物品。最开始只有一个空柜台,从指定的一个公司开始,轮流进行操作,可选的操作有两个:①选一个自己公司的空展柜放上对方公司的物品 ②选一个自己公司的空展柜,在这个展柜左边插入一个对方公司的空展柜,再在这个新插入的展柜左边插入一个空展柜并放上自己公司的物品。
题中给出了一个最终物品摆放的序列,问最后物品摆放的序列能否和给定序列相同。
思路:这题乍一看无法下手,其实在纸上画一画可以发现一些规律……如果选择从左数第一个未放物品的空展柜放的话(设这个位置对应左数第i个展柜),那么有两种放法,但无论哪种方法,放完以后第i位上展柜放的物品都不能改变了,因为i左边已经没有空展柜,此时两种方法在i位置放的物品是不同的,因此可以对应最终的序列来选择哪种放法。而且无论哪种选择,其右边空展柜都是AB相互交替的,因此没有太多特殊情况。。。我直接用栈模拟了,放完以后判断一下栈是否为空就行了。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<map>#include<queue>#include<set>#include<stack>#include<cmath>#include<vector>#define inf 0x3f3f3f3f#define Inf 0x3FFFFFFFFFFFFFFFLL#define eps 1e-9#define pi acos(-1.0)using namespace std;typedef long long ll;const int maxn=30000+10;char str[maxn];stack<int>S;int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); char ss[5]; scanf("%s",ss); scanf("%s",str); int n=strlen(str); int turns=(ss[0]=='B'); bool flag=true; int nowlen=0; if(str[0]!=ss[0]) flag=false; else { S.push(turns); S.push(turns^1); nowlen=3; } int x; for(int i=1;i<n;++i) { if(S.empty()) {flag=false;break;} x=S.top();S.pop(); if((x^1)==(str[i]-'A')) continue; if(nowlen+2>n) {flag=false;break;} nowlen+=2; S.push(x); S.push(x^1); } if(!S.empty()) flag=false; if(n==1&&str[0]!=ss[0]) flag=true; if(flag) puts("YES"); else puts("NO"); return 0;}
0 0
- SGU 191 Exhibition (贪心+构造)
- sgu 165 Basketball 贪心构造
- SGU 180 贪心
- sgu 170 贪心
- SGU 361 (构造)
- SGU 171 Sarov zones【贪心】
- SGU 257 Debt 贪心 + 模拟
- SGU 317 Fast Ride 贪心 + dp
- SGU 296 Sasha vs. Kate ( 贪心 )
- SGU 548 Dragons and Princesses(贪心)
- 【贪心】【SGU 410】 Galaxy in danger
- SGU - 186 - The Chain (贪心)
- SGU - 355 - Numbers Painting (贪心)
- SGU 122. The book 哈密尔顿回路 构造
- SGU 138 Games of Chess 构造
- SGU 121 Bridges painting(构造)
- SGU 137 Funny Strings(构造)
- SGU 138 Games of Chess(构造)
- 高精度题目集锦
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal (Java)
- Kendo UI开发教程(1): 概述
- HDU 1408 盐水的故事 数学水题
- 怎样才能成为一个优秀的产品经理?
- SGU 191 Exhibition (贪心+构造)
- MyEclipse 8.5 优化实例
- C# 用NPOI保存为Excel文件
- 几个库
- 微软翻译接口
- [Linux] 软件源码安装 - Tarball
- 手机具体型号对比
- 脱去互联网外衣 黄太吉煎饼还剩什么?
- Freemaker的简述使用