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 ...
随机推荐
- 你的第一个自动化测试:Selenium 自动化测试
前言: 让你掌握自动化测试暂时脱离手工点点点,本章节让你掌握 Selenium 如何定位.判定.操作元素(实现业务逻辑) 一. 前期准备 1.准备以下工具 1.Selenium下载地址:立即下载 2. ...
- PAT——甲级1065:A+B and C(64bit) 乙级1010一元多项式求导
甲级1065 1065 A+B and C (64bit) (20 point(s)) Given three integers A, B and C in [−263,263], you ...
- php 根据文件内容来判断文件类型
/*文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.doc.ppt *6 ...
- android 在自定义的listview(有刷新加载项)列表中,数据过少时不能铺满整个屏幕时,header和footer同时显示问题
android 在自定义的listview(有刷新加载项)列表中,数据过少时,当刷新时,加载项也会显示,这是很头疼的一个问题,查阅了一些资料,总结了一个比较不错的方法: 原来代码: @Overrid ...
- ubuntu添加新用户并添加管理员权限
Ubuntu创建新用户并增加管理员权限 Family 2014-06-24 22:21:22 $是普通管员,#是系统管理员,在Ubuntu下,root用户默认是没有密码的,因此也就无法使用(据说是为 ...
- VS2013下配置OpenCV 3.0.0 &&& VS2013下配置Opencv2.4.9
最近做图像需要用到Matlab和OpenCV,一些东西真的是要深入的研究进去才会有所发现,但Matlab和C++都不是我擅长的语言,所以要很加油很加油才行啊!! 步入正题. 1. 环境:Win7 6 ...
- Codeforces 498D Traffic Jams in the Land | 线段树
题目大意: 给坐标轴1~n的点,每个点有一个权值,从一个点走到下一个点需要1s,如果当前时间是权值的倍数就要多花1s 给出q组操作,C表示单点修改权值,A表示询问0时刻x出发到y的时间 题解:因为权值 ...
- CF992E Nastya and King-Shamans 解题报告
CF992E Nastya and King-Shamans 题意翻译 给定一个序列 \(a_i\),记其前缀和序列为 \(s_i\),有 \(q\) 个询问,每次单点修改,询问是否存在一个 \(i\ ...
- 【CF Edu 28 B. Math Show】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 重复登录Windows远程桌面-Autoit脚本
非常抱歉,我先临时把脚本放上来,具体的说明有时间再更新: 都是做成快捷方式,用鼠标点击的操作 #include <AutoItConstants.au3> ; Open mstsc pro ...