【2018ACM山东省赛 - E】Sequence(树状数组,思维,优化)

题干:

We define an element aia_iai​ in a sequence "good", if and only if there exists a j(1≤j<i)j(1\le j < i)j(1≤j<i) such that aj<aia_j < a_iaj​<ai​.
Given a permutation ppp of integers from 111 to nnn. Remove an element from the permutation such that the number of "good" elements is maximized.

Input

The input consists of several test cases. The first line of the input gives the number of test cases, T(1≤T≤103)T(1\le T\le 10^3)T(1≤T≤103).
For each test case, the first line contains an integer n(1≤n≤106)n(1\le n\le 10^6)n(1≤n≤106), representing the length of the given permutation.
The second line contains nnn integers p1,p2,⋯,pn(1≤pi≤n)p_1,p_2,\cdots,p_n(1\le p_i\le n)p1​,p2​,⋯,pn​(1≤pi​≤n), representing  the given permutation ppp.
It’s guaranteed that ∑n≤2×107\sum n\le 2\times 10^7∑n≤2×107.

Output

For each test case, output one integer in a single line, representing the element that should be deleted. If there are several answers, output the minimal one.
 

Sample Input

2
1
1
5
5 1 2 3 4

Sample Output

1
5

题目大意:

定义a[i]是good,当且仅当数组中他前面的数中 存在比他小的数。给定一个n个数的序列a,保证是1~n的一个排列,现在让你删掉其中一个数(且必须删掉一个),使得good数最多。问你删掉哪个数?如果有多种可能性就输出最小的解。

解题报告:

   这题在重现赛nlogn是可以过的,但是现场赛应该是严格保证线性才能过(因为nlogn那就是个水题了啊怎么可能过的队伍这么少)。保证线性也不难,因为你会发现你只需要记录前缀最小值和次小值然后维护一下就行了。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cctype>
using namespace std;
typedef long long ll;
const int maxn=2e7+5;
int n,m;
int val[maxn],suf[maxn]; //suf[val[i]]记录依附于val[i]的数的个数 
//cnt[i]求每个数前小于它的数的个数, minn[]同时记录前缀1~i的最小值 
int minn[maxn],cnt[maxn];
bool is[maxn];//is[i]记录val[i]是否不是前缀1~i的最小值 
inline int lowbit(int t){return t&-t;}
void add(int p,int val,int op)
{if(op==0){for(;p<=n;p+=lowbit(p))cnt[p]+=val;}else {for(;p<=n;p+=lowbit(p))minn[p]=min(minn[p],val);}
}
int query(int p,int op)
{int res;if(op){for(res=1e9;p;p-=lowbit(p))res=min(minn[p],res);}else {for(res=0;p;p-=lowbit(p))res+=cnt[p];}return res;
}
int main()
{int t,q,i,j,k;int ans,maxx,tmp,num,sum,Min;cin>>t;for(;t;t--){sum=0; maxx=-1; ans=1e9;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",val+i);cnt[i]=suf[i]=is[i]=0; minn[i]=1e9;}for(i=1;i<=n;i++){num=query(val[i]-1,0);if(num){sum++; is[i]=1;if(num==1){tmp=query(val[i]-1,1);suf[tmp]++;}}add(val[i],1,0); add(val[i],val[i],1);}for(i=1;i<=n;i++){tmp=sum-(int)is[i]-suf[val[i]];if(tmp>maxx||tmp==maxx&&val[i]<ans) {maxx=tmp; ans=val[i];}}printf("%d\n",ans);}return 0;
}
/*
7
1
1
5
5 1 2 3 4
5
4 3 5 2 1  
5
1 3 2 4 5  1
5
1
2
*/

线性AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cctype>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
int n,m1,m2;
int val[maxn],suf[maxn]; //suf[i]记录依附于val[i]的数的个数 
//cnt[i]求每个数前小于它的数的个数, minn[]同时记录前缀1~i的最小值 
bool is[maxn];//is[i]记录val[i]是否不是前缀1~i的最小值 
int main()
{int t,q,i,j,k;int ans,maxx,tmp,num,sum;cin>>t;for(;t;t--){sum=0; maxx=-1; ans=m1=m2=1e9;scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",val+i);suf[i]=is[i]=0; }for(i=1;i<=n;i++){if(m1<val[i]){sum++; is[i]=1;if(m2>val[i]) {suf[m1]++; m2=val[i];}}else {m2=m1; m1=val[i];}}for(i=1;i<=n;i++){tmp=sum-(int)is[i]-suf[val[i]];if(tmp>maxx||tmp==maxx&&val[i]<ans) {maxx=tmp; ans=val[i];}}printf("%d\n",ans);}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440292.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Apollo自动驾驶入门课程第②讲 — 高精地图

目录 1. 高精地图与传统地图 2. 高精地图与定位、感知规划的关系 2.1 高精地图用于定位 2.2 高精地图用于感知 2.3 高精地图用于规划 3. Apollo高精度地图与构建 3.1 Apollo高精地图 3.2 Apollo高精地图的构建 本文转自微信公众号&#xff1a; Apollo开发者社区 原创&a…

项目总结2:ionic3开发跨平台App如何设置和替换应用图标及启动图

前言&#xff1a; 和原生开发一样&#xff0c;ionic官方提供的设置方式也很简单&#xff0c;只不过多了一个步骤&#xff1a;基于ionic命令的方式自动修改全局的配置文件config.xml。 设置或替换应用图标和应用启动图&#xff1a; 把UI提供的图标拿过来改成特定的名称"i…

【2018ACM山东省赛 - G】Games(Nim博弈 + dp)

题干&#xff1a; Problem Description Alice and Bob are playing a stone game. There are nnn piles of stones. In each turn, a player can remove some stones from a pile (the number must be positive and not greater than the number of remaining stones in the …

LeetCode刷题实战(13):Roman to Integer

题目描述&#xff1a; 13 Roman to Integer 49.5%Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D …

项目总结3:ionic3开发的App在启动过程中部分手机出现白屏或黑屏问题之终极解决方案

问题场景&#xff1a;采用ionic3开发的App&#xff0c;当项目比较大的时候&#xff0c;会出现部分真机设备在启动的过程中有白屏或黑屏的情况。 原因预测&#xff1a;个别手机&#xff0c;尤其是安卓手机的性能比较差&#xff0c;App在启动后进入首页或登录页前的初始化工作还…

1.Intro to Deep Learning and Computer Vision

Intro 这是Kaggle深度学习教育课程的第一课。 在本课程结束后&#xff0c;您将了解卷积。 卷积是计算机视觉&#xff08;以及许多其他应用程序&#xff09;中深度学习模型的基本构建块。 之后&#xff0c;我们将很快开始使用世界一流的深度学习模型。 Lesson [1] from IPy…

【POJ - 2253】Frogger(floyd,或 部分瓶颈生成树的最大边)

题干&#xff1a; 湖中有n块石头&#xff0c;编号从1到n&#xff0c;有两只青蛙&#xff0c;Bob在1号石头上&#xff0c;Alice在2号石头上&#xff0c;Bob想去看望Alice&#xff0c;但由于水很脏&#xff0c;他想避免游泳&#xff0c;于是跳着去找她。但是Alice的石头超出了他…

SpringMVC常用的视图接口分类及实现类

SpringMVC中常用的视图接口分类及对应的实现类&#xff1a; URL资源视图&#xff1a;InternalResourceView、JstlView 文档视图&#xff1a;AbstractExcelView、AbstractPdfView 报表视图&#xff1a;ConfigurableJsperReportsView等JasperReports报表技术的视图 JSON视图&…

2.Building Models from Convolutions

Intro 这是深度学习课程的第2课 在本课程结束时&#xff0c;您将了解如何将卷积结合起来&#xff0c;以实现计算机视觉中的超人成就。 Lesson [1] from IPython.display import YouTubeVideo YouTubeVideo(ToBPiUlLFEY, width800, height450) Keep Going 现在你理解了模…

*【POJ - 2796】 Feel Good (前缀和优化+单调栈维护)

题干&#xff1a; Feel Good Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 12409 Accepted: 3484Case Time Limit: 1000MS Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedi…

SpringMVC配置视图的直接映射view-controller命名空间

通常情况下&#xff0c;如果直接通过url来访问具体的视图会报404错误&#xff0c;这个时候最容易想到的解决办法是通过转发或重定向机制&#xff0c;也就说走一遍目标控制器方法拦截一次。但是最好的方法是配置视图的直接映射关系。 <mvc: view-controller path"url访…

【nyoj-456】 邮票分你一半 (dp,0-1背包的中点问题)

题干&#xff1a; 邮票分你一半 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;3 描述 小珂最近收集了些邮票&#xff0c;他想把其中的一些给他的好朋友小明。每张邮票上都有分值&#xff0c;他们想把这些邮票分成两份&#xff0c;并且使…

Angular中父子组件传值@Input @Output @ViewChild最全面最简单的总结

父组件传递给子组件&#xff1a; 值传递方式&#xff1a;Input既可以传递数据也可以传递方法 传递数据&#xff08;不举例了&#xff09;传递方法 // 父组件定义方法 parentRun(){alert(这是父组件的 run 方法); } 调用子组件时传入当前方法&#xff08;是传递方法不是调用方…

《TCP/IP详解》学习笔记(七):广播和多播、IGMP协议

单播&#xff0c;多播&#xff0c;广播的介绍 1单播(unicast) 单播是说&#xff0c;对特定的主机进行数据传送。例如给某一个主机发送 IP 数据包。这时候&#xff0c;数据链路层给出的数据头里面是非常具体的目的地址&#xff0c;对于以太网来说&#xff0c;就是网卡的 MAC 地…

【 POJ - 3628 】Bookshelf 2(dfs 或 dp,0-1背包)

题干&#xff1a; Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top. FJ has N cows (1 ≤ N ≤ 20) each with some height of Hi (1 ≤ Hi ≤ 1,0…

ES5总结1:数组Array新特性最全最精简的详解

2个位置方法&#xff1a;indexOf lastIndexOf 5个迭代方法&#xff1a;forEach every some filter map 2个高阶函数&#xff1a;reduce reduceRight &#xff08;可用于数组求和&#xff09; API: 1、indexOf(searchElement: T, fromIndex?: n…

Apollo自动驾驶入门课程第③讲 — 定位

目录 1. 定位的概述 2. 定位方法介绍 2.1 GNSS RTK 2.2 惯性导航 2.3 激光雷达定位 2.4 视觉定位 2.5 Apollo定位 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 8月17日 上周我们发布了 Apollo入门课堂第②讲—高精地图&…

【HRBUST - 1621】迷宫问题II (bfs)

题干&#xff1a; 小z身处在一个迷宫中&#xff0c;小z每分钟可以走到上下左右四个方向的相邻格之一。迷宫中有一些墙和障碍物。 同时迷宫中也有一些怪兽&#xff0c;当小z碰到任意一个怪兽时&#xff0c;小z需要将怪兽消灭掉才可以离开此方格。但消灭 怪兽会花费一定的时间。…

Linux与Bash 编程——Linux文件处理命令-L1

目录&#xff1a; linux系统与shell环境准备 Linux系统简介操作系统简史Linux的发行版&#xff1a;Linux与Windows比较&#xff1a;Linux安装安装包下载Linux的访问方式远程登录方式远程登录软件&#xff1a;mobaxterm的使用&#xff1a;使用电脑命令行连接&#xff1a;sshd的…

4.Transfer Learning

Intro 这是深度学习第4课。 在本课程结束时&#xff0c;您将能够使用迁移学习为您的自定义目标构建高度准确的计算机视觉模型&#xff0c;即使您的数据相对较少。 Lesson [1] from IPython.display import YouTubeVideo YouTubeVideo(mPFq5KMxKVw, width800, height450) S…