因为每个元素都是移动到比它小1位的元素的后面;

这样的话以后的一定就可以把他们两个打包;

所以用这种方法最多扫一遍就可以了;

但是最小的那个数要不要移动呢?

如果最小的数后面的数都是升序的,那么一直扫到最小的那个数就行了;

不然的话要完整的扫一遍;

这个地方我没想清楚,WA的好惨,最后还是看到斌哥的代码才恍然大悟的;

#include<cstdio>
#include<algorithm>
#define maxn 100005
using namespace std;
int n,t,m;
int num[maxn],f[maxn],r[maxn],cnt[maxn]; bool cmp(const int &x,const int &y)
{
return num[x]<num[y];
} int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
} void merge(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx!=yy)f[yy]=xx,cnt[xx]+=cnt[yy];
} void pre()
{
for(int i=; i<=n; i++)r[i]=i;
sort(r+,r+n+,cmp);
for(int i=; i<=n; i++)num[r[i]]=i;
for(int i=; i<=n; i++)f[i]=i,cnt[i]=;
} void solve()
{
long long ans=;
bool flag=;
for(int i=r[]; i<n; i++)
if(num[i]>num[i+])
{
flag=;
break;
}
if(flag)m=r[]-;
else m=n;
for(int i=; i<=m; i++)
{
int v=num[i];
ans+=cnt[v];
merge(v-,v);
}
printf("%lld\n",ans);
} int main()
{
// freopen("test0.in","r",stdin);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&num[i]);
pre();
solve();
}
return ;
}

csuoj 1352: New Sorting Algorithm的更多相关文章

  1. 中南大学oj:1352: New Sorting Algorithm

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1352 题意:就是要将7 1 5 2这样的序列变成1  2  5  7最少需要多少步?给出变的规律, ...

  2. 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm

    1352: New Sorting Algorithm Time Limit: 1 Sec  Memory Limit: 128 MB Description We are trying to use ...

  3. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  4. Bubble sort of sorting algorithm

    Bubble sort,It's a relatively basic algorithm.The core implementation ideas are as follows: 1.Define ...

  5. 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)

    http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...

  6. Sorting Algorithm

    sorting 应该是最容易被考到的东西,自己老是学了背,背了忘.为了方便复习,这里进行总结 1. Bubble Sort 定义:每两个两个比较,每扫完一次,当前扫过的最大值放在了末尾. for i ...

  7. 排序算法(sorting algorithm) 之 选择排序(selection sort)

    https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...

  8. 排序算法(sorting algorithm)之 插入排序(insertion sort)

    https://en.wikipedia.org/wiki/Insertion_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 loop2: 4,6,1,3,7 -> ...

  9. Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

随机推荐

  1. 【原】NGUI中的UIRoot脚本功能

    UIRoot是NGUI控件的根节点,使用是根据屏幕尺寸自动(或手动)调节节点下子控件的大小. 这个组件声明了在编辑模式下运行:[ExecuteInEditMode],在Inspector编辑修改属性值 ...

  2. 程序员你该学学如何设计PPT了,设计PPT的几大原则

    写代码也要读书,爱全栈,更爱生活.每日更新原创IT编程技术及日常实用视频. 我们的目标是:玩得转服务器Web开发,搞得懂移动端,电脑客户端更是不在话下. 大量的汇报及介绍工作,都离不开一个辅助工具-P ...

  3. 兼容IE6/IE7/IE8/FireFox的css hack

    兼容IE6/IE7/IE8/FireFox的css hack .color{ background-color: #CC00FF; background-color: #FF00009; *backg ...

  4. PS定位

    一.定位:按标准看谁离得近来进行覆盖:什么都没有(最远).浮动(远).定位(近)   二.position absolute 绝对定位,能堆叠在上一层下面,脱离文档流 relative  相对定位,按 ...

  5. 对css float 浮动的学习心得

    css float浮动详解 @(css float)[hasLayout|clear float|妙瞳] css float的定义和用法 float 属性定义元素在哪个方向浮动.以往这个属性总应用于图 ...

  6. 一点关于this的理解

    关于this,是很多前端面试必考的题目,有时候在网上看到这些题目,自己试了一下,额,还真的错了!在实际开发中,也会遇到 this 的问题(虽然一些类库会帮我们处理),例如在使用一些框架的时候,例如:k ...

  7. 在DataTable中添加行和列数据

    DataRow newRow = dtResult.NewRow(); newRow["ProName"] = "名字"; newRow["ProPr ...

  8. GridView中的超级链接技巧

    GridView中的超级链接,可以设置一个模版列,放入超级链接的控件,设置绑定参数即可. 数据绑定方式有两种,如下示例: Eval方式 <%# Eval("id") %> ...

  9. Android--LowMemoryKiller知识点补充

    Android在内存管理上与linux有些小的区别.其中一个就是引入了Low memory killer . 1.引入原因: Android是一个多任务系统,也就是说可以同时运行多个程序,这个大家应该 ...

  10. HDU 4430 Yukari's Birthday(二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430 题目大意:给定n个蜡烛,围绕蛋糕的中心插同心圆,从里往外分别是第1圈.第2圈....第r圈,第 ...