题目链接:https://codeforces.com/contest/1413/problem/D

题干描述:

Tenten runs a weapon shop for ninjas. Today she is willing to sell n shurikens which cost 1, 2, …, n ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.

Tenten keeps a record for all events, and she ends up with a list of the following types of records:

  • ‘+’ means that she placed another shuriken on the showcase;
  • '- x’means that the shuriken of price x was bought.

Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!

Input
The first line contains the only integer n (1≤n≤105) standing for the number of shurikens.

The following 2n lines describe the events in the format described above. It’s guaranteed that there are exactly n events of the first type, and each price from 1 to n occurs exactly once in the events of the second type.

Output
If the list is consistent, print “YES”. Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print “NO”.

In the first case the second line must contain n space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
Examples
Input

4
+
+
- 2
+
- 3
+
- 1
- 4

Output

YES
4 2 3 1

Input

3
+
+
+
- 2
- 1
- 3

Output

NO

Note
In the first example Tenten first placed shurikens with prices 4 and 2. After this a customer came in and bought the cheapest shuriken which costed 2. Next, Tenten added a shuriken with price 3 on the showcase to the already placed 4-ryo. Then a new customer bought this 3-ryo shuriken. After this she added a 1-ryo shuriken. Finally, the last two customers bought shurikens 1 and 4, respectively. Note that the order [2,4,3,1] is also valid.

In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.

In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price 2. This is impossible since the shuriken was not the cheapest, we know that the 1-ryo shuriken was also there.

题意描述

一家店卖忍者用的手里剑(应该是手里剑)。每天有n把手里剑,每把剑的价格分别为1~n。店员不是一次性把所有手里剑都摆在柜台上,而是每次摆一把,分n次摆。我们不知道摆剑的顺序。
有忍者来买手里剑,他们每次只买当前摆在柜台上的手里剑中最便宜的一把。
问我们:题目中给出的2n个操作,是不是合理的。如果不合理,输出"NO";如果合理,输出"YES",并另起一行输出一行数列,表示店员放置手里剑的顺序(所有可能中的一种即可)。
关于操作的定义:
"+"表示,店员向柜台放了一把手里剑,至于放的是哪把我们不知道。
"- x"表示有忍者买走了价值为x的手里剑。
关于操作合理的定义:
当柜台为空时,忍者仍买走了一把手里剑,显然是不合理的。
当不能满足所有忍者都购买了当前柜台中最便宜的一把手里剑时,显然也不合理。

思路分析

我们可以确定的是,忍者只会买当前柜台中最便宜的一把手里剑。
也就是说当我们遇到"- x"操作时,x必然是当前柜台中最便宜的一把手里剑的价格,即当前柜台中的其他手里剑的价格都比x大。
但是有一个问题,我们根本没有办法确定,当前柜台中价格最低的手里剑是哪一把。因为"+"操作只能告诉我们,店员向柜台中放置了一把手里剑,但是没有告诉我们他放置的手里剑的价格。
那该怎么办呢?有一个想法是,我们根据顾客买的手里剑的价格来确定当前柜台中的手里剑的价格。
所以我们制定一个对我们最有利的规则:忍者买的手里剑总是柜台所有手里剑中最新拿上来的一把。也就是说,最新拿上来的一把就是当前柜台中价格最低的一把。再换句话说,柜员向柜台中摆剑的时候,一定要保证新摆的剑比当前柜台中所有的剑的价格都低!
那有朋友该纳闷了,店员凭啥这么摆啊?咱不是不知道店员摆的顺序吗?
我们之所以这么摆,是因为这样对我们最有利
正如我们所知道的,我们不知道店员摆手里剑的顺序。我们能做的只能是根据顾客买手里剑的顺序来反推操作是否可行。也就是说,只要对我们最有利的情况是可行的,那这个操作就是可行的。
那我们制定的规则最有利的依据是什么呢?我们在这道题中的限制就是,忍者只会买当前柜台中价格最低的一把手里剑。作为一名奸商 有智慧的商人,我摆的时候顺序一定会是n,n-1,……1.因为你不是总买最便宜的吗,那也就意味着越便宜的手里剑所受的限制就越少,那我当然是先把限制大的给卖出去。
但是我们能不能做到整个过程按照降序摆呢?不能。因为忍者买的时候给出了价格x,也就是说我们要想让操作合理,那就必须保证价格为x的我们已经摆进去了。别等着人家买了个价格为2的,你降序摆才摆到价格为4的。那对于我们来说最优解就是:**先摆贵的,并保证刚好在忍者要买价格为x的手里剑之前,把价值为x的手里剑摆上去。**我刚好在你要买之前把你想要的这个给摆上去。这就是我们的最有利的规则。

代码实现

相信大家也品出味了,刚摆的手里剑是最便宜的,而顾客只买最便宜的。也就是说,越先摆进柜台的就越晚出去。先进后出,这不就是个栈吗!现在写代码就很容易了。
代码如下:

#include<iostream>
#include<stack>
using namespace std;
int n,ans[100005],x,num;
char ch;
stack<int> v;
int main(){cin>>n;num=1;for(int i=1;i<=2*n;i++){cin>>ch;if(ch=='+'){v.push(num++);//如果是加的话,就代表向栈中添加元素,num表示这是第num个进栈的元素 }else{cin>>x;if(v.empty()) {//如果顾客要买手里剑时,柜台却空了,那显然是不合理的 cout<<"NO";return 0;}else if(x<ans[v.top()+1]){//ans[n]表示第n个放进栈的元素的价格//v.top()表示当前栈顶元素的序号,v.top()+1表示当前栈顶元素的下一个元素的序号//之所以满足该条件不合理,是因为,既然x更小,而且x还在ans[v.top()+1]之前入栈。那当初就应该时x出栈。 cout<<"NO";return 0;}ans[v.top()]=x;//第v.top()个进栈的元素的价格确定了 v.pop();//他被买走了,所以出栈 }}cout<<"YES"<<endl;for(int i=1;i<=n;i++){cout<<ans[i];if(i!=n) cout<<' '; }return 0;
}

我也是看到了codeforce上的题解才把这个题搞明白的。
第一次写题解,叙述有些啰嗦,大家包涵!
若有不足之处,还望指出!

Codeforce题解:Shurikens相关推荐

  1. CodeForce题解——Thanos Sort

    题目链接 CodeForce网站.http://codeforces.com/problemset/problem/1145/A. 我的小破站.http://47.110.135.197/proble ...

  2. 1255B. Fridge Lockers Codeforce题解

    原题链接 思路:这题问的是,给你n个冰箱,每个冰箱重量为a[i],冰箱由几条链连接,解锁冰箱的条件是解锁这个冰箱的链,冰箱的主人拥有他的冰箱所连接的链的钥匙,故他自己可以解锁冰箱,但是其他人需要合作解 ...

  3. 370A. Rook, Bishop and King codeforce题解

    题目链接 思路:给个8*8的棋盘,有三种棋,给你初始位置和目标位置,输出三种棋到达目标的最小步数. 我们先看rook,由于他可以横着竖着走任意距离,所以,他最多走两步(一横一竖),最少走一步(如果竖行 ...

  4. Codeforce Round #827 (Div4) 题解(ABCEG)

    本文 G 来自于幻想家协会会长的个人空间_哔哩哔哩_bilibili的题解 A.Sum 解法:暴力枚举即可 代码: #include<bits/stdc++.h> /*Writer: Zh ...

  5. 【题解】CodeForce 1060E-Sergey and Subway(树形DP)

    题意:给你n个顶点的一棵树,距离为3的两个点之间连一条新的边(原边留下),问所有点对的距离之和. 题解:考虑两个问题, 第一,如果两个点之间的距离是偶数的话,那么连新边之后的距离就变成了原来就距离/2 ...

  6. codeforce 555 div3 题解报告

    a. 给每次位置后面补上1,然后用10-当前位置上的数,只有一位数的时候直接+10: #include<bits/stdc++.h> using namespace std; #defin ...

  7. 9.5 考试 第三题 奇袭题解(codeforce 526f)

    问题 C: 奇袭 时间限制: 1 Sec  内存限制: 256 MB 题目描述 由于各种原因,桐人现在被困在Under World(以下简称UW)中,而UW马上 要迎来最终的压力测试--魔界入侵. 唯 ...

  8. 【自用】Codeforce入门六题题解

    A 解题思路:用字符串数组做,这里没有QA开头和结尾限制. 遍历数组,Q则加一,A则减一,结果为正即为YES. 测试语句: 5 4 QQAA 4 QQAQ 3 QAA 1 Q 14 QAQQAQAAQ ...

  9. 1560A. Dislike of Threes codeforce比赛8.19题解

    原题链接 思路:有一个序列,由从1开始递增,且不包括能整除3或者个位有3的数字. 如:1,2,4,5,6,7,8,11,14,16-(索引从1开始) 输入一个k,输出这个序列第k个数 这题就是记忆化存 ...

最新文章

  1. c++ opencv实现区域填充_使用OpenCV实现图像覆盖
  2. mysql命令行各个参数解释
  3. Esper应用以及原理解析
  4. 如何测试一个网页登陆界面
  5. Asp.Net Core 中IdentityServer4 实战之 Claim详解
  6. 常见排序算法_解释的算法-它们是什么以及常见的排序算法
  7. WinAPI: SetTextColor - 设置设备环境的文本颜色
  8. python 类变量(属性)和实例变量(属性
  9. mysql通过存储过程批量造测试数据
  10. ubuntu下使用visual studio code来编译和调试C++
  11. eeglab加载显示脑电数据,eeglab简单操作
  12. 我的第一个C语言:用点阵显示出我的名字拼音首字母。
  13. 用HTML来做导航栏
  14. 流媒体 直播细节优化
  15. 为什么程序员喜欢用dark mode深色模式
  16. c语言如何算字节,C语言中结构字节的计算方法
  17. 【计算视觉】人体姿态识别研究综述(详细归纳!)
  18. 小米HTML查看器记住密码,小米路由器 SSH 密码计算工具,开启小米SSH访问
  19. Hazelcast IMDG参考中文版手册-第一章-前言
  20. css3 animation 实现帧动画

热门文章

  1. items中多个checkgroup在IE6下无法完整显示
  2. 华为手机摄影入门到精通pdf_手机摄影入门教程视频 手机摄影技巧视频教程
  3. 使用ffmpeg将mkv,rmvb转换成mp4
  4. 科研人员必贴春联,总有一款适合你!
  5. 【navicat 密码查看】小技巧navicat 如何查看密码
  6. 21世纪发展最快的数据科学的总结
  7. 新华都总裁兼CEO给唐骏留言
  8. Android 开发艺术探索——第十章 Android的消息机制
  9. Windows 系统文件资源管理器的命令行参数(如何降权打开程序,如何选择文件)
  10. python 字符画