FOJ Problem 2260 Card Game
Accept: 37 Submit: 117
Time Limit: 3000 mSec Memory Limit : 32768
KB
Problem Description
有如下取牌游戏:
1. 桌面上有n张卡牌从左到右排成一行,每张卡牌上有一个数字;
2. 游戏按轮次进行,每一轮中取掉所有比左边数值小的卡牌;
3. 当无牌可取的时候则游戏结束。
比如初始卡牌为{5, 6, 3, 7, 4, 1, 2},共需2轮取牌。取牌过程如下(小括号为每轮取掉的牌):
{5, 6, 3, 7, 4, 1, 2}
==> {5, 6, (3), 7, (4), (1), 2}
==> {5, 6, 7, 2}
==> {5, 6, 7, (2)}
==> {5, 6, 7}
现按顺序给定初始的卡牌数字,请求出游戏结束时取牌的总轮次,并输出结束时桌上剩余的卡牌序列。
Input
包含多组测试数据。
输入包含两行。
第一行包含一个整数n表示卡牌的数量。
第二行包含n个空格隔开的整数,表示排成一行的卡牌上对应的数字(取值范围[1,1000000000])。
n≤1000000
Output
输出包含两行。
第一行包含一个整数表示游戏的取牌总轮次。
第二行包含游戏结束时桌上剩余的卡牌序列,用空格隔开。
Sample Input
Sample Output
1:栈中只剩栈顶元素的情况,此时栈顶元素的下面已经没有元素了,因此已经无法被消除,则一定会出现在最终的卡牌序列中,将之抛出栈中并记录,再将当前元素放入堆中;
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
const int N_MAX = +;
int a[N_MAX],n;
stack<int>s;
int dp[N_MAX];
vector<int>res;
int main() {
while (scanf("%d",&n)!=EOF) {
memset(dp, , sizeof(dp));
res.clear();
int term = ;
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
for (int i = ; i < n;i++) {
while (!s.empty() && a[i] >= a[s.top()]) {
if (s.size() == ) {
res.push_back(a[s.top()]);
s.pop();
}
else {
dp[i] = max(dp[i],dp[s.top()]+);
term = max(term,dp[i]);
s.pop();
}
}
s.push(i);
} while (!s.empty()) {
if (s.size() == ) {
res.push_back(a[s.top()]);
s.pop();
}
else {
term = max(term, dp[s.top()] + );
s.pop();
}
}
printf("%d\n",term);
for (vector<int>::iterator it = res.begin(); it != res.end();it++) {
if (it + != res.end())cout << *it << " ";
else cout << *it << endl;
}
} return ;
}
FOJ Problem 2260 Card Game的更多相关文章
- FOJ ——Problem 1759 Super A^B mod C
Problem 1759 Super A^B mod C Accept: 1368 Submit: 4639Time Limit: 1000 mSec Memory Limit : 32 ...
- FOJ Problem 1016 无归之室
Problem 1016 无归之室 Accept: 926 Submit: 7502Time Limit: 1000 mSec Memory Limit : 32768 KB Prob ...
- FOJ Problem 1015 土地划分
Problem 1015 土地划分 Accept: 823 Submit: 1956Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- foj Problem 2107 Hua Rong Dao
Problem 2107 Hua Rong Dao Accept: 503 Submit: 1054Time Limit: 1000 mSec Memory Limit : 32768 K ...
- foj Problem 2282 Wand
Problem 2282 Wand Accept: 432 Submit: 1537Time Limit: 1000 mSec Memory Limit : 262144 KB Prob ...
- FOJ Problem 2273 Triangles
Problem 2273 Triangles Accept: 201 Submit: 661Time Limit: 1000 mSec Memory Limit : 262144 KB P ...
- foj Problem 2275 Game
Problem D Game Accept: 145 Submit: 844Time Limit: 1000 mSec Memory Limit : 262144 KB Problem D ...
- foj Problem 2283 Tic-Tac-Toe
Prob ...
- FOJ Problem 2257 Saya的小熊饼干
...
随机推荐
- java基础—object类
一.Object类介绍
- jqury点击返回顶部代码
效果请看右下角:代码如下: <div class="totop"><img src="https://www.cnblogs.com/images/cn ...
- 配置centos7解决 docker Failed to get D-Bus connection 报错
在centos7的容器里面出现了一个BUG,就是serveice启动服务的时候出现报错,不能用service启动服务.[root@e13c3d3802d0 /]# service httpd star ...
- 救援(BFS)
题目描述: 在你的帮助下,Oliver终于追到小X了,可有一天,坏人把小X抓走了.这正是Oliver英雄救美的时候.所以,Oliver又找到哆啦A梦,借了一个机器,机器显示出一幅方格地图,它告诉Oli ...
- 每天一个linux命令(13):less命令
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- MAC系统里安装 Python
首先MAC系统自带Python2.6/2.7.这是因为有些系统文件需要.但是对于我来说,我需要用到Python3,所以需要自己下载安装.这时候,就有一个非常强大的软件 Homebrew.(安装方法见官 ...
- OpenSSL version mismatch. Built against 1000105f, you have 10001060
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=732940 http://ftp.debian.org/debian/pool/main/o/ope ...
- 在spring boot中使用webSocket组件(二)
该篇演示如何使用websocket创建一对一的聊天室,废话不多说,我们马上开始! 一.首先先创建前端页面,代码如下图所示: 1.login.html <!DOCTYPE html> < ...
- STM8 EEPROM心得
对于STM8来说,其内部的EEPROM确实是个不错的东西,而且STM8S103/105价格已经非常便宜了,当然也可以用STM8S003/005代替,而且价格更便宜,大概在,1.2/2.0元左右,比10 ...
- ReportViewer部分使用总结
最近winform上使用ReportViewer做报表,因为之前没弄过,所以遇到了很多问题,现在总结一下. 一.运行环境 .net环境:4.0 开发工具:vs2010 二.开发步骤 第一步,在winf ...