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

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

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

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

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

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

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

  1. #include<cstdio>
  2. #include<algorithm>
  3. #define maxn 100005
  4. using namespace std;
  5. int n,t,m;
  6. int num[maxn],f[maxn],r[maxn],cnt[maxn];
  7.  
  8. bool cmp(const int &x,const int &y)
  9. {
  10. return num[x]<num[y];
  11. }
  12.  
  13. int find(int x)
  14. {
  15. return f[x]==x?x:f[x]=find(f[x]);
  16. }
  17.  
  18. void merge(int x,int y)
  19. {
  20. int xx=find(x);
  21. int yy=find(y);
  22. if(xx!=yy)f[yy]=xx,cnt[xx]+=cnt[yy];
  23. }
  24.  
  25. void pre()
  26. {
  27. for(int i=; i<=n; i++)r[i]=i;
  28. sort(r+,r+n+,cmp);
  29. for(int i=; i<=n; i++)num[r[i]]=i;
  30. for(int i=; i<=n; i++)f[i]=i,cnt[i]=;
  31. }
  32.  
  33. void solve()
  34. {
  35. long long ans=;
  36. bool flag=;
  37. for(int i=r[]; i<n; i++)
  38. if(num[i]>num[i+])
  39. {
  40. flag=;
  41. break;
  42. }
  43. if(flag)m=r[]-;
  44. else m=n;
  45. for(int i=; i<=m; i++)
  46. {
  47. int v=num[i];
  48. ans+=cnt[v];
  49. merge(v-,v);
  50. }
  51. printf("%lld\n",ans);
  52. }
  53.  
  54. int main()
  55. {
  56. // freopen("test0.in","r",stdin);
  57. scanf("%d",&t);
  58. while(t--)
  59. {
  60. scanf("%d",&n);
  61. for(int i=; i<=n; i++)
  62. scanf("%d",&num[i]);
  63. pre();
  64. solve();
  65. }
  66. return ;
  67. }

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. PullToRefresh的个性化扩展

    一:实现区别下拉刷新和上拉加载 参考资料:http://blog.csdn.net/losetowin/article/details/18261389 在PullToRefresh的类库的com.h ...

  2. 猪满满 购物APP

    猪满满是专注“省钱,赚钱”的购物App,使用自定义tabar分为四大类,分别是首页,超返,发现,我的. 首页:使用UItableview,自定义cell展示商品. 超返:自定义Button分为综合,返 ...

  3. spark stream初探

    spark带了一个NetworkWordCount测试程序,用以统计来自某TCP连接的单词输入: /usr/local/spark/bin/run-example streaming.NetworkW ...

  4. SQL数据库开发知识总结:提高篇

    1.联合结果集 (1) 简单的结果集联合 select FNumber,FName,Fage from T_Employee\ union select FidCardNumber,FName,Fag ...

  5. Aix命令大全

    AIX服务器系统命令简介 在AIX操作系统上有很多的命令.这里介绍一些系统级的命令,它将有助于回答一些常见问题.大家以此做参考,并补充修改. 以下命令在AIX 5.1上测试通过. 正文 以下命令在AI ...

  6. 源代码jar包中中文注释乱码

    目前公司开发的多个组件有打包源代码并发布到nexus,但是很多同事通过maven使用组件时,直接通过eclipse浏览源代码时,发现中文注释为乱码的问题.其实这个eclipse默认编码造成的问题.可以 ...

  7. WTL 中的常见问题汇总

    1.CRect,CPoint,CSize的使用 WTL提供了CString,CRect,CPoint和CSize,可能后来版本的ATL也提供了,WTL作者推荐使用ATL的实现,所以:#include ...

  8. hdu 1316 How many Fibs?(高精度斐波那契数)

    //  大数继续 Problem Description Recall the definition of the Fibonacci numbers:  f1 := 1  f2 := 2  fn : ...

  9. OpenJudge 2747 数字方格

    1.链接地址: http://bailian.openjudge.cn/practice/2747 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 如上图,有3个 方格,每个 ...

  10. linux系统制作简单流程

    制作嵌入式平台使用的Linux内 核, 方法和制作PC平台 的Linux内 核基本一致, 下面使用 对比的方式介绍如何制作用 于6410开发板的内 核. 1. 清除原有配置与中间文件x86: make ...