因为每个元素都是移动到比它小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. s15day12作业:MySQL练习题参考答案

    MySQL练习题参考答案   导出现有数据库数据: mysqldump -u用户名 -p密码 数据库名称 >导出文件路径           # 结构+数据 mysqldump -u用户名 -p ...

  2. ServletContext当全局变量的使用

    ServletContext对象 1,作用:JavaWeb应用的一个全局变量,一个应用只有一个ServletContext对象,在应用启动时,容器就会创建该对象 2,获得ServletContext对 ...

  3. postgresql cast转换类型

    代码 CAST(aa as NUMERIC)

  4. ASP.NET MVC 4应用程序文件夹

    App_Start It has configuration classes to reduce clutter code in the Global.asax 它包含了配置类来减少在Global.a ...

  5. win32项目设置窗口全屏

    创建窗口的时候设置样式:CreateWindow(,,WS_MAXIMIZE,……): 显示窗口的时候设置:ShowWindow(hWnd,SM_MAXIMIZE);

  6. oc语言学习之基础知识点介绍(一):OC介绍

      一.第一个OC程序 #import <Foundation/Foundation.h> //导入头文件 int main(int argc, const char * argv[]) ...

  7. python输出1到100之和的几种方法

    1. 使用内建函数range print sum(range(1,101)) 2. 使用函数reduce print reduce(lambda a,b:a+b,range(1,101)) 3. 使用 ...

  8. Lisp与JAVA的酷毙结合——abcl

    最近看了一本叫做<黑客与画家>的书,其中对于Lisp语言大加褒奖.自己试着用了一下,虽然确实有反人类之嫌,但是确实是一门不错的语言,New Architect杂志上有一篇介绍ITA软件公司 ...

  9. 帝国CMS附件大小限制

    做文件的上传下载,在我们本地测试总是顺利通过,一上传到服务器各种问题都来了. 帝国CMS,我们先看网站配制中附件大小限制. 附件存放目录设置: 单击菜单“系统”>“系统设置”>“系统参数设 ...

  10. HowTo: SVN undo add without reverting local changes

    Reference: http://stackoverflow.com/questions/5083242/undo-svn-add-without-reverting-local-edits svn ...