LA 6979 Known Notation 构造+贪心 铜牌题

6979 Known Notation
Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and
computer science. It is also known as postfix notation since every operator in an expression follows all
of its operands. Bob is a student in Marjar University. He is learning RPN recent days.
To clarify the syntax of RPN for those who haven’t learnt it before, we will offer some examples
here. For instance, to add 3 and 4, one would write “3 4 +” rather than “3 + 4”. If there are multiple
operations, the operator is given immediately after its second operand. The arithmetic expression
written “3 - 4 + 5” in conventional notation would be written “3 4 - 5 +” in RPN: 4 is first subtracted
from 3, and then 5 added to it. Another infix expression “5 + ((1 + 2) × 4) - 3” can be written down
like this in RPN: “5 1 2 + 4 × + 3 -”. An advantage of RPN is that it obviates the need for parentheses
that are required by infix.
In this problem, we will use the asterisk “*” as the only operator and digits from “1” to “9” (without
“0”) as components of operands.
You are given an expression in reverse Polish notation. Unfortunately, all space characters are
missing. That means the expression are concatenated into several long numeric sequence which are
separated by asterisks. So you cannot distinguish the numbers from the given string.
You task is to check whether the given string can represent a valid RPN expression. If the given
string cannot represent any valid RPN, please find out the minimal number of operations to make it
valid. There are two types of operation to adjust the given string:
1. Insert. You can insert a non-zero digit or an asterisk anywhere. For example, if you insert a “1”
at the beginning of “2*3*4”, the string becomes “12*3*4”.
2. Swap. You can swap any two characters in the string. For example, if you swap the last two
characters of “12*3*4”, the string becomes “12*34*”.
The strings “2*3*4” and “12*3*4” cannot represent any valid RPN, but the string “12*34*” can
represent a valid RPN which is “1 2 * 34 *”.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of
test cases. For each test case:
There is a non-empty string consists of asterisks and non-zero digits. The length of the string will
not exceed 1000.
Output
For each test case, output the minimal number of operations to make the given string able to represent
a valid RPN.
Sample Input
3
1*1
11*234**
*ACM-ICPC Live Archive: 6979 – Known Notation 2/2
Sample Output
1
0
2

题意:给出一个字符串,有两种操作: 1.插入一个数字  2.交换两个字符   问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>

#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define CT continue
#define SC scanf

char s[1005];
int main()
{
    int cas;SC("%d",&cas);
    while(cas--){
        SC("%s",s);
        int len=strlen(s),num=0,ln=0,star=0,ans=0;
        for(int i=0;s[i]!=‘\0‘;i++)
            if(s[i]==‘*‘) star++;
            else num++;
        if(num<star+1) {ans+=star+1-num;ln=star+1-num;}

        for(int i=0;s[i]!=‘\0‘;i++){
            if(s[i]>=‘0‘&&s[i]<=‘9‘) ln++;
            else{
                if(ln>=2) ln-=1;
                else {
                    ans++;
                    for(int j=len-1;j>=0;j--) if(s[j]!=‘*‘){
                        swap(s[j],s[i]);ln++;break;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  错因分析:看错题目了,,以为只能相邻两个交换,结果想了两个多小时,,,Orz;

构造:

(1).首先,我们可以看到对于有k个*的话。至少k+1个数字才能保证合法,所以不够的话,需要添加

(2).然后可以构造出11111...**...这样在满足(1)的前提下必然不需要然和操作就能保证合法的式子。

贪心:考虑到当前额*,因为不合法,那么至少需要一个操作才能保证合法,由构造可知,将该*掉到

末尾的数字上去,不仅能保证再次遇到该*时不再浪费额外的操作,也能尽量的贪心,让该*之后的接下来的

*有更多的数字去抵消,尽可能的不需要交换。

时间: 2024-10-29 10:46:37

LA 6979 Known Notation 构造+贪心 铜牌题的相关文章

LA 6533 Inverting Huffman 构造+贪心

题意:给定哈夫曼树的n个叶子节点距离根的距离,求文本至少需要多少个字符可以建出这样的哈夫曼树 思路:策略:对于第i层的叶子节点,赋值为i+1层的节点中权值最大的点这种情况下字符数最少.详见代码: /********************************************************* file name: LA6533.cpp author : kereo create time: 2015年02月07日 星期六 22时05分45秒 *****************

GCJ 2009 Round2 A 构造贪心策略

题意: 给你一个n?n的元素只为0或1的矩阵,要把该矩阵通过行交换变为上三角矩阵,且交换方式只能为相邻行交换.问最小的交换步数是多少? 思路: 首先这个行的利用价值就是最右边的1位置是多少,直接把行抽象为最右边1的数字即可.注意:这里WA了一发,没有注意数字要初始化为0.无论多么简单,写完之后检查代码且保证逻辑没有一丝错误十分重要! 这个题主要的是构造贪心的法则:题中只需要满足第一行的数字小于1,第二行的数字小于2,-..,第n行的数字小于n.那么就从第一行开始,一行一行的来看,注意若当前行的数

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

2014-2015 CT S02E10 C题 Coin Graph 构造+贪心

题意:给定一个数s,构造一个无向图,使得任意两点的最短路径和为s. 思路:二分找到n,满足n×(n-1)/2<=s,且n尽可能大.这样的图,相当于n个点每两个点都连一条边.窝们考虑这样一种构造方法, 假设现在有n个点,我们按照逆时针方向(顺时针也行)排序编号,那么我们间隔删去不相邻点的的边,比如5个点,我们删去 1--3,1--4,...,1--n-2,1---n-1,保留2,删3--5,3--6,...3--n-2,3----n-1,保留4,删5--7,5--8,...,5--n-1.这样可以

Codeforces Round #631 (Div. 2) C. Dreamoon Likes Coloring(贪心好题/意识流题解)

Dreamoon likes coloring cells very much. There is a row of nn cells. Initially, all cells are empty (don't contain any color). Cells are numbered from 11 to nn . You are given an integer mm and mm integers l1,l2,…,lml1,l2,…,lm (1≤li≤n1≤li≤n ) Dreamoo

ZOJ 3829 Known Notation (2014牡丹江H题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. I

UVa 1611 Crane (构造+贪心)

题意:给定一个序列,让你经过不超过9的6次方次操作,变成一个有序的,操作只有在一个连续区间,交换前一半和后一半. 析:这是一个构造题,我们可以对第 i 个位置找 i 在哪,假设 i  在pos 位置,那么如果 (pos-i)*2+i-1 <= n,那么可以操作一次换过来, 如果不行再换一种,如果他们之间元素是偶数,那么交换 i - pos,如果是奇数,交换 i - pos+1,然后再经过一次就可以换到指定位置. 代码如下: #pragma comment(linker, "/STACK:1

hdu 4550 贪心 思维题 不错

http://acm.hdu.edu.cn/showproblem.php?pid=4550 想了挺久,然后各种分类 终于AC,如果是现场,对自己没信心的话,估计还是要WA,,,,,,然后搜题解,发现人家都认为是简单题,看来我还是太弱了,牡丹江没有做出来K看来还是自己贪心和思维有问题 d是一个Deque 最朴素的算法是,如果当前的数<=d.front(),那么插入队列的前面,否则插入队列后面,但是有零所以需要单独处理,还是自己多举例找规律 我的策略: 1.记录0的个数zero,最小非零的数的个数