codeforces Out of Sorts

题面

outputstandard output
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 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.

Given an input array, please predict how many times “moo” will be printed by Bessie’s code.

Input
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.

Output
Print the number of times “moo” is printed.

题意

这牛写了一个冒泡排序,执行一次代码就嗷一次,给你数组问你嗷几次。

分析

我们都知道冒泡是O(N2),直接用他的函数必然超时。
然后你发现这冒泡每次就是把小的数往前挪1步,所以我们只要排序然后再遍历求出原先位置-当前位置+1(检查嗷一次)的最大值即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
struct node{
	int num,index;
}a[100005];
bool cmp(node a,node b){
	if(a.num!=b.num) return a.num<b.num;
	return a.index<b.index;
}
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",&a[i].num),a[i].index=i;
	sort(a,a+n,cmp);
	int maxn=0;
	for(int i=0;i<n;i++){
		maxn=max(maxn,a[i].index-i+1);
	}
	printf("%d\n",maxn);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值