Out of Sorts II
问题 E: Out of Sorts II
时间限制: 1 Sec 内存限制: 128 MB
提交: 68 解决: 4
[提交] [状态] [讨论版] [命题人:]
题目描述
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.
输入
输出
样例输入
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的更多相关文章
- 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 ...
- 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 ...
- 函数式Android编程(II):Kotlin语言的集合操作
原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...
- 统计分析中Type I Error与Type II Error的区别
统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- [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 ...
- [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 ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Spring+SpringMVC+MyBatis+Redis框架学习笔记
在Java互联网中,以Spring+Spring MVC+MyBatis (SSM) 作为主流框架. SSM+Redis的结构图 在这种框架系统中: Spring IoC 承担了一个资源管理.整合.即 ...
- Python 3基础教程13-写入文件
前面介绍了函数,这篇我们就利用Python 内构函数open来写入字符串到txt文件里. 直接看demo.py 这里有一个小问题,如果我要输入时中文到txt文件会报unicode错误,暂时没法解决.
- install.cloudinit.qga.bat
@echo off title Auto Install color 1F ::CloudBase-Init echo. msiexec /i \\192.168.122.47\cloudbase\C ...
- Python中的单元测试模块Unittest快速入门
前言 为什么需要单元测试? 如果没有单元测试,我们会遇到这种情况:已有的健康运行的代码在经过改动之后,我们无法得知改动之后是否引入了Bug.如果有单元测试的话,只要单元测试全部通过,我们就可以保证没有 ...
- altera ip 核小究
用quartus的MegaWizard工具生成一个乘法器multiplier,会在工程目录下产生 multiplier.qip (可选) multiplier_bb.v (可选) multip ...
- Echarts 基础知识浅析
1. 引入Echarts Echarts是基于canvas的数据可视化产品,由百度公司推出 参考官方文档,引入教程示例即可,注意有两种引入方式: (1)直接引入 (2)模块化引入 2. 基本API使用 ...
- linux--lsof
lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控制协议 ...
- hdu 6119 …&&百度之星 T6
小小粉丝度度熊 Problem Description 度度熊喜欢着喵哈哈村的大明星——星星小姐. 为什么度度熊会喜欢星星小姐呢? 首先星星小姐笑起来非常动人,其次星星小姐唱歌也非常好听. 但这都不是 ...
- html 过滤器 c#
using System.Text.RegularExpressions; using System.Web; internal class HtmlHelper { /// <summ ...
- V++ MFC CEdit输出数组 UNICODE TO ASCII码
MFC怎么在静态编辑框中输出数组 //字符转ASCII码void CUTF8Dlg::OnBnClickedButtonCharAscii(){ // TODO: 在此添加控件通知处理程序代码 Upd ...