April Fools Day Contest 2019 A. Thanos Sort
1 second
256 megabytes
standard input
standard output
Thanos sort is a supervillain sorting algorithm, which works as follows: if the array is not sorted, snap your fingers* to remove the first or the second half of the items, and repeat the process.
Given an input array, what is the size of the longest sorted array you can obtain from it using Thanos sort?
*Infinity Gauntlet required.
The first line of input contains a single number nn (1≤n≤161≤n≤16) — the size of the array. nn is guaranteed to be a power of 2.
The second line of input contains nn space-separated integers aiai (1≤ai≤1001≤ai≤100) — the elements of the array.
Return the maximal length of a sorted array you can obtain using Thanos sort. The elements of the array have to be sorted in non-decreasing order.
4
1 2 2 4
4
8
11 12 1 2 13 14 3 4
2
4
7 6 5 4
1
In the first example the array is already sorted, so no finger snaps are required.
In the second example the array actually has a subarray of 4 sorted elements, but you can not remove elements from different sides of the array in one finger snap. Each time you have to remove either the whole first half or the whole second half, so you'll have to snap your fingers twice to get to a 2-element sorted array.
In the third example the array is sorted in decreasing order, so you can only save one element from the ultimate destruction.
解题思路:就是告诉你有一种算法,它只能整体删除前半部分或者后半部分,使其升序的数目最多,我们可以先判断是否改数列使升序,然后如果不是,则不断递归左半部分和右半部分,取升序数目最大的;
代码如下:
#include<iostream>
using namespace std; int n ;
int a[];
int mmx(int x , int y)
{
int sum = ; //注意这里sum从1开始;因为一个数字就算是升序;
int flag = ;
for(int i = x ; i < y ;i++)
{
if(a[i]<=a[i+]) //看是否该序列已经升序;
{
sum++;
}else
{
flag = ; //一旦有不升序的就标记;
}
}
if(flag==)
{
return sum ;
}else
if(flag==) //不断递归;
{
int m = (x+y)/;
return max(mmx(x,m),mmx(m+,y));//取最大;
}
}
int main()
{
int ans = ;
cin>>n;
for(int i = ; i <= n ;i++)
{
cin>>a[i];
}
ans = mmx(,n);
cout<<ans;
}
April Fools Day Contest 2019 A. Thanos Sort的更多相关文章
- April Fools Day Contest 2019: editorial回顾补题
A. Thanos Sort time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- April Fools Day Contest 2014
April Fools Day Contest 2014 A.C.H三道题目 ============================================================= ...
- 坑爹CF April Fools Day Contest题解
H - A + B Strikes Back A + B is often used as an example of the easiest problem possible to show som ...
- April Fools Day Contest 2014 H. A + B Strikes Back
H. A + B Strikes Back time limit per test 1 second memory limit per test 256 megabytes input standar ...
- April Fools Day Contest 2016 D. Rosetta Problem
D. Rosetta Problem 题目连接: http://www.codeforces.com/contest/656/problem/D Description ++++++++[>+& ...
- April Fools Day Contest 2016 G. You're a Professional
G. You're a Professional 题目连接: http://www.codeforces.com/contest/656/problem/G Description A simple ...
- April Fools Day Contest 2016 F. Ace It!
F. Ace It! 题目连接: http://www.codeforces.com/contest/656/problem/F Description Input The only line of ...
- April Fools Day Contest 2016 E. Out of Controls
E. Out of Controls 题目连接: http://www.codeforces.com/contest/656/problem/E Description You are given a ...
- April Fools Day Contest 2016 C. Without Text 信号与系统
C. Without Text 题目连接: http://www.codeforces.com/contest/656/problem/C Description You can preview th ...
随机推荐
- Handler消息传送机制
一.什么是UI线程 当程序第一次启动的时候,Android会同时启动一条主线程( Main Thread). 主要负责处理与UI相关的事件. 二.UI线程存在的问题 出于性能优化考虑,Android的 ...
- No mapping found for HTTP request with URI [/jiaoyu/student/add] in DispatcherServlet with name 'SpringMVC'
项目是使用spring MVC (1)在浏览器中访问,后台总报错: No mapping found for HTTP request with URI [/exam3/welcome] in Dis ...
- [原创]Spring JdbcTemplate 使用总结与经验分享
引言 近期开发的几个项目,均是基于Spring boot框架的web后端项目,使用JdbcTemplate执行数据库操作,实际开发过程中,掌握了一些有效的开发经验,踩过一些坑,在此做个记录及总结,与各 ...
- ASP.NET MVC 基于表达式的动态查询
项目源码地址:https://gitee.com/zhengwei804/DynamicCustomSearch
- Android独立交叉编译环境搭建
我们经常需将一些C/C++源码编译成本地二进制,直接在android的linux内核上运行,这是就需要进行交叉编译.由于Android的运行环境核普通Linux又区别,所以常规方式针对ARM进行交叉编 ...
- swift之弹出一个背景半透明的UIViewController
坑爹的背景半透明,按网上给出oc的方法,动画结束之后,半透明效果消失.通过各种折腾,各种试验,终于搞定了. let viewController=storyboard.instantiateViewC ...
- 201671010140. 2016-2017-2 《Java程序设计》java学习第一周
java学习第一周 本周是新学期的开端,也是新的学习进程的开端,第一次接触java这门课程,首先书本的厚度就给我一种无形的压力,这注定了,这门课程不会是轻松的,同时一种全新的学习方 ...
- Hadoop之HDFS(二)HDFS基本原理
HDFS 基本 原理 1,为什么选择 HDFS 存储数据 之所以选择 HDFS 存储数据,因为 HDFS 具有以下优点: 1.高容错性 数据自动保存多个副本.它通过增加副本的形式,提高容错性. 某一 ...
- Web项目开发性能优化解决方案
web开发性能优化---安全篇 1.ip验证 2.操作日志.安全日志.登录日志 3.SQL注入校验 4.权限管理 5.验证规范(前端.后端.数据库约束) 2014-10-29 08:04 2773 ...
- ThinkPHP3.2 插入数据库数据,缓存问题
每个字段都是有数据的,可是就是is_new插不进去, 最后的原因就是TP有数据库字段缓存,FUCK 把RunTIme下面的Data文件夹删除掉,在开发模式中就把自动缓存改成Fasle