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的小熊饼干
...
随机推荐
- npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\James\package.json'
在运行如下命令时, 遇到了问题: npm install --registry=https://registry.npm.taobao.org npm run dev 错误提示: 解决办法: 生成一个 ...
- 《毛毛虫团队》第八次团队作业:ALPHA冲刺
一:实验名称:软件测试与ALPHA冲刺 二:实验目的与要求 (1)掌握软件测试基础技术. (2)学习迭代式增量软件开发过程(Scrum). 三:实验步骤 任务一:各个成员今日完成的任务: 任务二:明日 ...
- Python读写文件实际操作的五大步骤
Python读写文件在计算机语言中被广泛的应用,如果你想了解其应用的程序,以下的文章会给你详细的介绍相关内容,会你在以后的学习的过程中有所帮助,下面我们就详细介绍其应用程序. 一.打开文件 Pytho ...
- Html5的等学习
看了w3c感觉是说明文档,没有详细的说明,然后就去看其他的 html5其实就是在html的基础上做了一些改变,感觉html5的推广还是需要时间的,因为习惯问题,虽然html5有很多很方便的标签如art ...
- python class 巩固
class 类定义 语法格式如下: class ClassName: <statement-1> ... <statement-N> 类属性与方法 属性 操作属性 getatt ...
- Java微信公众号开发----定时获取access_token并保存到redis中
本人原本是想做微信公众号菜单的创建修改删除等操作的,但是发现需要access_token,通过阅读文档,发现文档要求有以下几点: 1.access_token 获取后有效期是2小时 2.access_ ...
- Mysql数据库插入中文出现乱码相关
查看数据库编码的命令:show variables like "character%"; mysql> show variables like "character ...
- Re:从零开始的Linux之路(目录配置)
基于 Red Hat Enterprise Linux 7.5 或者 CentOS 7.4 FHS协议(Filesystem Hierarchy Standard)——文件系统层次化标准 该标准定义了 ...
- springboot maven 多环境配置
1.使用Intellij IDEA创建Spring Boot和Maven项目 2.Spring Boot项目下application.yaml(yaml支持中文)或者application.prope ...
- python学习博客推荐
https://www.liaoxuefeng.com/