因为每个元素都是移动到比它小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. 类名.class与类名.this详解

    类名.class      我们知道在java中,一个类在被加载的时候虚拟机就会自动的生成一个这个类的一个Class类型的“类对象”,每个类都对应着一个这样的类对象,通过这个Class类型的类对象,我 ...

  2. 在.bashrc中,使用python获取本机IP地址(现在只支持wlan)

    其实最好的办法是写个单独的脚本去查找IP,但是如果实在不愿意单写一个脚本文件,也可以直接将代码嵌入.bashrc中 在~/.bashrc下加入下面这行代码即可使用python获取本机的wlan的IP地 ...

  3. (转)css3前缀

    CSS3的前缀是一个浏览器生产商经常使用的一种方式.它暗示该CSS属性或规则尚未成为W3C标准的一部分.看看都有哪些前缀: -webkit(chrome) -moz(firefox) -ms(ie) ...

  4. SQL*PLUS中批量执行SQL语句

    SQL*PLUS中批量执行SQL语句 今天由于工作的需要,要在CMD中批量执行大量的SQL语句,对于Oracle学习还处在入门阶段的我,只能硬着头皮到处去寻找资料(主要是网络资料,也包括自己的电子书) ...

  5. NPOI操作EXCEL 类代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...

  6. Cordova+angularjs+ionic+vs2015开发(一)

    欢迎加群学习:457351423 这里有4000多部学习视频,涵盖各种技术,有需要的欢迎进群学习! 一.Windows环境配置 1.如果已经安装VS2015,打开[工具]-[选项]找到Cordova选 ...

  7. VBA文件处理

    Option Explicit ' ▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽ ' Excel对象 ' △△△△△△△△△△△△△△△△△△ ' Open Public Function FileOpen_ ...

  8. 【html】【9】div布局[div层叠]

    让DIV重叠并按想要顺序重叠需要CSS来实现,即CSS绝对定位进行实现. 重叠样式需要主要CSS样式解释1.z-index 重叠顺序属性2.position:relative和position:abs ...

  9. ceilometer

    控制节点: ceilometer-api: /etc/init.d/openstack-ceilometer-api  status ceilometer-collector /etc/init.d/ ...

  10. OCI_ERROE - errcode[1591],errmsg[ORA-01591:

    CEASYDAO: 错误码[1591],错误信息[Error - OCI_ERROE - errcode[1591],errmsg[ORA-01591: lock held by in-doubt d ...