问题 E: Out of Sorts II

时间限制: 1 Sec  内存限制: 128 MB
提交: 68  解决: 4
[提交] [状态] [讨论版] [命题人:]

题目描述

Keeping an eye on long term career possibilities beyond the farm, Bessie the cow has started learning algorithms from various on-line coding websites.
Her favorite algorithm thus far is "bubble sort". Here is Bessie's initial implementation, in cow-code, for sorting an array A of length N.

sorted = false
while (not sorted):
   sorted = true
   moo
   for i = 0 to N-2:
      if A[i+1] < A[i]:
         swap A[i], A[i+1]
         sorted = false
Apparently, the "moo" command in cow-code does nothing more than print out "moo". Strangely, Bessie seems to insist on including it at various points in her code.

After testing her code on several arrays, Bessie learns an interesting observation: while large elements can be pulled to the end of the array very quickly, it can take small elements a very long time to "bubble" to the front of the array (she suspects this is how the algorithm gets its name). In order to try and alleviate this problem, Bessie tries to modify her code so that it scans forward and then backward in each iteration of the main loop, so that both large and small elements have a chance to be pulled long distances in each iteration of the main loop. Her code now looks like this:

sorted = false
while (not sorted):
   sorted = true
   moo
   for i = 0 to N-2:
      if A[i+1] < A[i]:
         swap A[i], A[i+1]
   for i = N-2 downto 0:
      if A[i+1] < A[i]:
         swap A[i], A[i+1]
   for i = 0 to N-2:
      if A[i+1] < A[i]:
         sorted = false
Given an input array, please predict how many times "moo" will be printed by Bessie's modified code.

输入

The first line of input contains N (1≤N≤100,000). The next N lines describe A[0]…A[N−1], each being an integer in the range 0…109. Input elements are not guaranteed to be distinct.

输出

Print the number of times "moo" is printed.

样例输入

5
1
8
5
3
2

样例输出

2
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
pair<int,int>aa[];
int n,ans=;
bool vis[];
bool cmp(pair<int,int>a,pair<int,int>b){
if(a.first!=b.first)return a.first<b.first;
return a.second<b.second;
}
void init(){
cin>>n;
range(i,,n-){
cin>>aa[i].first;
aa[i].second=i;
}
sort(aa,aa+n,cmp);
}
void solve(){
int cnt=;
range(i,,n-){
if(aa[i].second>i)++cnt;
if(vis[i])--cnt;
vis[aa[i].second]=true;
ans=max(ans,cnt);
}
cout<<ans<<endl;
}
int main() {
init();
solve();
return ;
}

Out of Sorts II的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  3. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

  4. 统计分析中Type I Error与Type II Error的区别

    统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...

  5. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  6. [LeetCode] Guess Number Higher or Lower II 猜数字大小之二

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  7. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  8. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  9. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. loadrunner破解出现“license security violation,Operation is not allowed”的错误提示

    1.关闭loadrunner,将破解文件(“lm70.dll”.“mlr5lprg.dll”)放置在LoadRunner\bin下面 2.以管理员身份运行loadrunner,在CONFUGURATI ...

  2. App自动化测试前期准备---android SDK配置

    说明:就是配置android SDK 一.sdk下载 Windows(X64):立即下载 Linux(X64):立即下载 二.Windows配置 1.解压文件 直接解压到指定目录(演示目录:D:/) ...

  3. diskimage-builder element

    root阶段 创建或修改初始根文件系统内容. 这是添加替代分销支持的地方,还是建立在现有图像上的自定义. 只有一个元素可以一次使用它,除非特别注意不要盲目覆盖,而是适应其他元素提取的上下文. -cac ...

  4. Windows7下打开特定的端口

    往往我们发布到IIS的网站多了,80的端口不能满足的情况下,我们就会想到设定其它端口来使用.当然还可以通过改变host文件来实现,这里就不细说了.回到端口,在windows7系统下怎么实现呢?下面将带 ...

  5. MPLAB设置路径

    大家都知道在MPLAB环境下编译程序,c文件.h文件.编译器生成的文件等等,都会被编译器无情的放在一个项目文件夹下. 稍微有些讲究的程序员可能就会觉得用MPLAB项目组织的一团糟.于是大家想到了一种方 ...

  6. [zoj] 1081 Points Within || 判断点是否在多边形内

    原题 多组数据. n为多边形顶点数,m为要判断的点数 按逆时针序给出多边形的点,判断点是否在多边形内,在的话输出"Within",否则输出"Outside" / ...

  7. 洛谷 P4883 mzf的考验 解题报告

    P4883 mzf的考验 题目背景 \(mzf\)立志要成为一个豪杰,当然,他也是一个\(OIer\). 他希望自己除了会\(OI\)之外还会各种东西,比如心理学.吉他.把妹等等. 为了让自己有更大的 ...

  8. NIO的介绍及使用(总结)

    传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...

  9. POJ1018 Communication System

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26738   Accepted: 9546 Description We ...

  10. 当表名存在 点 的时候,EntityFramework的写法

    原文发布时间为:2011-05-17 -- 来源于本人的百度文章 [由搬家工具导入] 需要在 Context 中,重写 DbContext 中的 OnModelCreating 方法 进行 mappi ...