这是2014年ACM亚洲区预赛牡丹江现场赛的一道题,铜牌题,可惜当时一路WA到死。。。

只有乘法的后缀表达式的特点有两个:(1)数字的数量一定大于‘*’的数量(2)最后一位一定是‘*’;

数字比*多的话,*怎么排列都无所谓,因为1111即可以看成4个1,也可以看成1111,总可以与‘*’

结合。所以在模拟之前做以下预处理:(1)把缺少的数字补上(2)把最后一位1与前面的任何‘*’交换

开始模拟,一个‘*’能被结合,必须前面有大于或者等于2个数字,所以设置一个栈,遇到一个栈,就入

栈一个数字,遇到一个‘*’,如果栈里面1的个数少于2个,则把‘*’与后面的数字交换,等stack里的数字》=2时;

出栈一次,模拟到字符串尾即可;

至于why,只要数字够用即可,所以当遇到‘*’前面的数字不够时,不是插入1,而是将*与后面的1交换。。总之是fuck the dog!。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
typedef long long ll; int t;
string ss;
int len; int main()
{
//freopen("in","r",stdin);
scanf("%d",&t);
while(t--)
{
stack<int> s;
queue<int> que;
ss = "";
cin>>ss;
len = ss.size();
int op = ;
int sch = ,snum = ;
int pos;
for(int i = ; i < len; ++i)
{
if(ss[i] == '*') sch++;
else snum++;
}
if(sch >= snum){
for(int i = ; i < sch-snum+; ++i)
{
ss=''+ss;
}
op+=sch-snum+;
}
if(sch > &&ss[ss.size() - ] != '*'){
for(int i = ; i < ss.size(); ++i)
if(ss[i] == '*') {
pos = i;break;
}
swap(ss[pos],ss[ss.size() - ]);
op+=;
}
for(int i = ss.size() - ; i >= ; --i)
{
if(ss[i] != '*') que.push(i);
}
for(int i = ; i < ss.size(); ++i)
{
if(ss[i] == '*')
{
if(s.size() == ){
pos = que.front();//cout<<pos<<endl;
swap(ss[i],ss[pos]);
que.pop();
op++;
s.push();
}
else if(s.size() == ){
int pos = que.front();
swap(ss[i],ss[pos]);
que.pop();
pos = que.front();
swap(ss[i],ss[pos]);
que.pop();
op+=;
s.push();
s.push();
}
else s.pop();
}
else{
s.push();
}
}
printf("%d\n",op);
}
}

ZOJ3829---模拟,贪心的更多相关文章

  1. BZOJ1029: [JSOI2007]建筑抢修[模拟 贪心 优先队列]

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MBSubmit: 3785  Solved: 1747[Submit][Statu ...

  2. Codeforces Round #375 (Div. 2) A B C 水 模拟 贪心

    A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...

  3. Codeforces 452D [模拟][贪心]

    题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...

  4. bzoj 2457 [BeiJing2011]双端队列 模拟+贪心

    [BeiJing2011]双端队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 457  Solved: 203[Submit][Status][D ...

  5. bzoj 3671: [Noi2014]随机数生成器【模拟+贪心】

    降智好题 前面随机部分按照题意模拟,然后字典序贪心,也就是记录每个值的位置从1~nm依次看能不能取,能取的话更新行的取值范围(它上面的行一定取的列小于等于这个数取的列,下面行大于等于) #includ ...

  6. ZOJ 3829 模拟贪心

    2014牡丹江现场赛水题 给出波兰式,推断其是否合法.假设不合法有两种操作: 1:任何位置加一个数字或者操作符 2:随意两个位置的元素对调 贪心模拟就可以 先推断数字数是否大于操作符数,若不大于 an ...

  7. 洛谷 P1016 旅行家的预算 模拟+贪心

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 总结 题面 题目链接 P1016 旅行家的预算 题目描述 一个旅行家想驾驶汽车 ...

  8. 洛谷P5019 铺设道路 题解 模拟/贪心基础题

    题目链接:https://www.luogu.org/problemnew/show/P5019 这道题目是一道模拟题,但是它有一点贪心的思想. 我们假设当前最大的深度是 \(d\) ,那么我们需要把 ...

  9. Codeforces Round #370 (Div. 2) A B C 水 模拟 贪心

    A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. BZOJ_1028_[JSOI2007]_麻将_(模拟+贪心)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1028 同一种花色的牌,序数为\(1,2,...,n\).定义"和了"为手上 ...

随机推荐

  1. zoj3640:概率(期望)dp

    题目大意:有一个吸血鬼,初始攻击力为f,每天随机走到n个洞里面,每个洞有一个c[i],如果他的攻击力f>c[i] 则可以花费t[i] 的时间逃走,否则则花费一天时间使自己的攻击力增加c[i],求 ...

  2. Bring it on

    I am going to open a whole new English Blog here. Most blogs here would be computer technologies, in ...

  3. 集成Dubbo服务(Spring)

    Dubbo是什么? Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点. Dubbo[]是 ...

  4. poj 3692 Kindergarten (最大独立集之逆匹配)

    Description In a kindergarten, there are a lot of kids. All girls of the kids know each other and al ...

  5. Chrome浏览器查看cookie

    原文:http://jingyan.baidu.com/article/6b18230954dbc0ba59e15960.html 1. 查看页面的cookie 方法: a). 点击地址栏前面的文档薄 ...

  6. [RxJS] Error Handling in RxJS

    Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thro ...

  7. 解析Function.prototype.bind

    简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. bind的作用 bind最直接的作用就是改变this的 ...

  8. ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法

    转载:http://blog.sina.com.cn/s/blog_629e606f01014d4b.html ComboBox最经常使用的事件就是SelectedIndexChanged.但在将Co ...

  9. iOS_SN_BlueTooth( 一)蓝牙相关基础知识

    原文  http://www.cocoachina.com/ios/20150915/13454.html 作者:刘彦玮 蓝牙常见名称和缩写 MFI ======= make for ipad ,ip ...

  10. sqlite的事务

    好久没用数据库,知识都忘了,之前用sqlite 逐行insert数据,发现这性能实在是太坑,10w条数据,插入大约花了100来分钟. 后来发现还有事务这么一个东西,可以大幅度降低对io的操作,测试插入 ...