NBUT 1535
题意:往一个塔里放数,取数,找第 k 个大的数
刚开始用 stack 做的,不知道怎样找第 k 个数
又想到用 vector ,以前用过
注释有点多吧 ......
代码如下:
#include<stdio.h>
#include<vector> //定义 c++ 中的容器所需的头文件
#include<algorithm> //sort 排序所需的头文件
using namespace std; //c++ 要用到
vector<int> qq1, qq2; //定义容器 qq1,qq2,相当于数组,比数组功能大点
int main()
{
int t, n, k;
scanf("%d", &t);
while(t--)
{
qq1.clear(); //由于多组数据,每次都将 qq1 清空
scanf("%d%d", &n, &k);
char ch[];
while(n--)
{
int a;
scanf("%s", ch);
if(ch[] == 'P')
{
scanf("%d", &a);
qq1.push_back(a); //将 a 放到容器 qq1 最后面
}
else if(ch[] == 'T')
{
int len = qq1.size(); //容器 qq1 中元素个数
printf("%d\n", qq1[len - ]); //输出 qq1 中最后面的元素,注意下标:len - 1,从 qq1[0] 开始
qq1.pop_back(); //将 qq1 最后的元素舍弃
}
else
{
int len = qq1.size();
qq2 = qq1; //将 qq1 复制到 qq2,避免影响 qq1
sort(qq2.begin(), qq2.end()); //将 qq2 从小到大排序
if(len >= k) //若元素个数大于 k ,输出第 k 个数
printf("%d\n", qq2[k - ]);
else printf("%d\n", qq2[len - ]); //否则,输出最大的数
}
}
}
return ;
}
NBUT 1535的更多相关文章
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- ACM: NBUT 1107 盒子游戏 - 简单博弈
NBUT 1107 盒子游戏 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format: Practice Appoint ...
- ACM: NBUT 1105 多连块拼图 - 水题 - 模拟
NBUT 1105 多连块拼图 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format: Practice Appoint ...
- ACM: NBUT 1646 Internet of Lights and Switches - 二进制+map+vector
NBUT 1646 Internet of Lights and Switches Time Limit:5000MS Memory Limit:65535KB 64bit IO Fo ...
- NBUT 1525 Cow Xor(01字典树+前缀思想)
[1525] Cow Xor 时间限制: 2000 ms 内存限制: 65535 K 问题描述 农民约翰在喂奶牛的时候被另一个问题卡住了.他的所有N(1 <= N <= 100,000)个 ...
- NBUT 1186 Get the Width(DFS求树的宽度,水题)
[1186] Get the Width 时间限制: 1000 ms 内存限制: 65535 K 问题描述 It's an easy problem. I will give you a binary ...
- NBUT 1635 Explosion(最小顶点覆盖)
[1635] Explosion 时间限制: 10000 ms 内存限制: 65535 K 问题描述 there is a country which contains n cities connec ...
- NBUT 1602 Mod Three(线段树单点更新区间查询)
[1602] Mod Three 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Please help me to solve this problem, if so, Liang ...
- NBUT 1028 该减肥了(简单递推)
[1028] 该减肥了 时间限制: 1000 ms 内存限制: 65535 K 问题描述 由于长期缺乏运动,Teacher Xuan发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥.Teach ...
随机推荐
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- sql server中自连接的使用
一.用SQL自连接查询处理列之间的关系 SQL自身连接,可以解决很多问题.下面举的一个例子,就是使用了SQL自身连接,它解决了列与列之间的逻辑关系问题,准确的讲是列与列之间的层次关系.SQL代码如下: ...
- bootstrap 时间控件带(时分秒)选择器
1.控件下载地址:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm,参数设置说明也在这个链接下面: 2.具体参数说明(复制原链接) ...
- 解决 VMWARE MAC 10.12无法全屏的问题
昨天我在VMware上装了10.10,然后通过APP store 升级了系统到 10.12,升级前安装VMware tools 能自动全屏,可是升级后不行. 然后在网上查了很多资料,发现并没有这方面的 ...
- ios UILabel在storyBoard或xib中如何在每行文字不显示完就换行
大家知道怎么用代码让label中的文字换行,只需要 label.numberOfLines = 0; label.text = @"这是第一行啦啦啦啦,\n这是第二行啦啦,\n这是第三行&q ...
- mysql计划字段中有多少个逗号,或者某个标识符
eg:计划url中有多少个小数点 select length('www.mysql.com')-length(REPLACE('www.mysql.com','.',''));
- (转)java缓存技术,记录
http://blog.csdn.net/madun/article/details/8569860 最近再ITEYE上看到关于讨论JAVA缓存技术的帖子比较多,自己不懂,所以上网大概搜了下,找到一篇 ...
- How to build the Robotics Library from source code on Windows
The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...
- java第6次作业
import java.util.ArrayList; import java.util.Collections; import java.util.Random; import java.util. ...
- Spring Boot + Bootstrap 出现"Failed to decode downloaded font"和"OTS parsing error: Failed to convert WOFF 2.0 font to SFNT"
准确来讲,应该是maven项目使用Bootstrap时,出现 "Failed to decode downloaded font"和"OTS parsing error: ...