test20181024 kun
题意


分析
考场做法
任意状态只有两种决策,进栈或出栈。
由于元素互不相同,所以选择的关键在于栈顶元素是否在它和带插入元素组成的集合中是最大的。
用stack和set维护。时间复杂度\(O(n \log n)\)
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#include<cassert>
#define rg register
#define il inline
#define co const
#pragma GCC optimize ("O0")
using namespace std;
template<class T> il T read()
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return data*w;
}
template<class T> il T read(T&x)
{
return x=read<T>();
}
typedef long long ll;
const int INF=0x7fffffff;
const int MAXN=1e6+7;
int a[MAXN];
stack<int>S;
set<int>H;
int main()
{
freopen("kun.in","r",stdin);
freopen("kun.out","w",stdout);
rg int n=read<int>();
for(rg int i=1;i<=n;++i)
{
read(a[i]);
H.insert(a[i]);
}
rg int p=1;
for(rg int i=1;i<=2*n;++i)
{
// cerr<<"pro "<<i<<endl;
if(!S.empty()&&S.top()==*H.rbegin()) // pop
{
// cerr<<" case 1"<<endl;
printf("%d ",S.top());
H.erase(S.top());
S.pop();
if(!S.empty())
H.insert(S.top());
}
else // push
{
// cerr<<" case 2"<<endl;
if(!S.empty())
H.erase(S.top());
S.push(a[p++]);
}
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
标解
要使序列字典序最大,首先它的第一个元素必须是最大元素,按此思路贪心即可。要求出下一步可输出元素的最大者,只需要取下一个元素所在的后缀与当前栈顶元素中的最大值。后缀最大值可以在线性时间内预处理出。
实际上维护最大值没必要用set,观察发现我们只需了解每个后缀的最大值,所以用数组维护。
时间复杂度\(O(n)\)
//Achen
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
#define Formylove return 0
const int N=1e6+7;
typedef long long LL;
typedef double db;
using namespace std;
int n,a[N],mx[N];
template<typename T> void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int sta[N],top;
#define ANS
int main() {
#ifdef ANS
freopen("kun.in","r",stdin);
freopen("kun.out","w",stdout);
#endif
read(n);
For(i,1,n) read(a[i]);
Rep(i,n,1) {
mx[i]=mx[i+1];
if(a[i]>mx[i]) mx[i]=a[i];
}
For(i,1,n) {
while(top&&sta[top]>mx[i]) {
printf("%d ",sta[top--]);
}
sta[++top]=a[i];
}
while(top) {
printf("%d ",sta[top--]);
}
puts("");
Formylove;
}
test20181024 kun的更多相关文章
- test20181024 nan
题意 nan 问题描述 我们有一个序列,现在他里面有三个数1,2,2.我们从第三个数开始考虑: 第三个数是2,所以我们在序列后面写2个3,变成1,2,2,3,3. 第四个数是3,所以我们在序列后面写3 ...
- test20181024 zi
题意 分析 这种题一般是推公式,发现必须求得的量,然后定义函数记忆化. 然后那些函数里面又是递归处理,合并. 代码 为了不爆空间,用map存记忆化内容. #include<bits/stdc++ ...
- test20181024 ming
题意 分析 考场做法 考虑二分答案,R开到1e9就能过了. 判断答案合法,就判断时间和是否超过拥有的时间就行了.但要把di从小到大排序,不然容易验证贪心是错的. 时间复杂度\(O(n \log n)\ ...
- test20181024 qu
题意 分析 极似UVa11995 I Can Guess the Data Structure! 模拟即可,时间复杂度(--)\(O(n \log n)\) 旁边的L君:这题有两个坑点,我被卡了一次. ...
- test20181024 hao
题意 分析 考场10分 直接\(O(nm)\)模拟即可. #include<cstdlib> #include<cstdio> #include<cmath> #i ...
- 【C#公共帮助类】 Utils 10年代码,最全的系统帮助类
为大家分享一下个人的一个Utils系统帮助类,可能有些现在有新的技术替代,自行修改哈~ 这个帮助类主要包含:对象转换处理 .分割字符串.截取字符串.删除最后结尾的一个逗号. 删除最后结尾的指定字符后的 ...
- 【中文分词】二阶隐马尔可夫模型2-HMM
在前一篇中介绍了用HMM做中文分词,对于未登录词(out-of-vocabulary, OOV)有良好的识别效果,但是缺点也十分明显--对于词典中的(in-vocabulary, IV)词却未能很好地 ...
- Oracle汉字转拼音package
--函数GetHzFullPY(string)用于获取汉字字符串的拼音 --select GetHzFullPY('中华人民共和国') from dual; --返回:ZhongHuaRenMinGo ...
- 搭建一套自己实用的.net架构(3)【ORM-Dapper+DapperExtensions】
现在成熟的ORM比比皆是,这里只介绍Dapper的使用(最起码我在使用它,已经运用到项目中,小伙伴们反馈还可以). 优点: 1.开源.轻量.小巧.上手容易. 2.支持的数据库还蛮多的, Mysql,S ...
随机推荐
- ubuntu16.04(64位)安装 Drcom
一 : 下载DrClient(DrcomAndPPOE) 解压 下载地址 https://www.baidu.com/s?wd=DrClient(DrcomAndPPOE)&rsv_spt ...
- Codeforces Round #528 Solution
A. Right-Left Cipher Solved. 注意长度的奇偶 #include <bits/stdc++.h> using namespace std; string s; i ...
- 哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级) Solution
A: Solved. 分别处理出每个%7后余数的数字个数,再组合一下 #include <bits/stdc++.h> using namespace std; #define ll lo ...
- springboot属性类自动加载配置文件中的值
springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...
- Codeforces Round #526 (Div. 1)
毕竟是上紫之后的第一场div1,还是太菜了啊,看来我要滚回去打div2了. A. The Fair Nut and the Best Path 这题本来是傻逼贪心dfs,结果我越写越麻烦,然后就只有1 ...
- 利用ES6中的Array.find/ Array.findIndex来判断数组中已存在某个对象
前端开发过程中,我们会经常遇到这样的情景:比如选中某个指标obj,将其加入到数组checkedArr中({id: 1234, name: 'zzz', ...}),但是在将其选中之前要校验该指标是否已 ...
- 【eclipse】Server Tomcat v9.0 Server at localhost failed to start.
Server Tomcat v9.0 Server at localhost failed to start. 的一个原因就是启动超时了.
- Ubuntu 安装 Telnet
参考:在 ubuntu 上安裝 telnet service 1.安装: sudo apt-get install xinetd telnetd 2.修改inetd.conf: vim /etc/in ...
- Flask 2 程序的基本结构1
NOTE 1.初始化:所有的Flask程序都必须创建一个程序实例. app = Flask(__name__) # 向Flask的构造函数传入参数__name__ 2.路由和视图函数(VF): Cli ...
- spring boot2 基于百度云apiface实现人脸检测与认证1
原理介绍: 基于百度云的人脸资料库(用户上传),调用本地摄像头抓拍的图像,与百度云的用户图像做比对,实现人脸认证. 主要步骤如下: 1. 创建百度去账号 2. 在百度云控制台中创建人脸识别的应用,并记 ...