csuoj 1352: New Sorting Algorithm
因为每个元素都是移动到比它小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的更多相关文章
- 中南大学oj:1352: New Sorting Algorithm
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1352 题意:就是要将7 1 5 2这样的序列变成1 2 5 7最少需要多少步?给出变的规律, ...
- 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm
1352: New Sorting Algorithm Time Limit: 1 Sec Memory Limit: 128 MB Description We are trying to use ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- Bubble sort of sorting algorithm
Bubble sort,It's a relatively basic algorithm.The core implementation ideas are as follows: 1.Define ...
- 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)
http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...
- Sorting Algorithm
sorting 应该是最容易被考到的东西,自己老是学了背,背了忘.为了方便复习,这里进行总结 1. Bubble Sort 定义:每两个两个比较,每扫完一次,当前扫过的最大值放在了末尾. for i ...
- 排序算法(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 ...
- 排序算法(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 -> ...
- 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 ...
随机推荐
- PullToRefresh的个性化扩展
一:实现区别下拉刷新和上拉加载 参考资料:http://blog.csdn.net/losetowin/article/details/18261389 在PullToRefresh的类库的com.h ...
- 猪满满 购物APP
猪满满是专注“省钱,赚钱”的购物App,使用自定义tabar分为四大类,分别是首页,超返,发现,我的. 首页:使用UItableview,自定义cell展示商品. 超返:自定义Button分为综合,返 ...
- spark stream初探
spark带了一个NetworkWordCount测试程序,用以统计来自某TCP连接的单词输入: /usr/local/spark/bin/run-example streaming.NetworkW ...
- SQL数据库开发知识总结:提高篇
1.联合结果集 (1) 简单的结果集联合 select FNumber,FName,Fage from T_Employee\ union select FidCardNumber,FName,Fag ...
- Aix命令大全
AIX服务器系统命令简介 在AIX操作系统上有很多的命令.这里介绍一些系统级的命令,它将有助于回答一些常见问题.大家以此做参考,并补充修改. 以下命令在AIX 5.1上测试通过. 正文 以下命令在AI ...
- 源代码jar包中中文注释乱码
目前公司开发的多个组件有打包源代码并发布到nexus,但是很多同事通过maven使用组件时,直接通过eclipse浏览源代码时,发现中文注释为乱码的问题.其实这个eclipse默认编码造成的问题.可以 ...
- WTL 中的常见问题汇总
1.CRect,CPoint,CSize的使用 WTL提供了CString,CRect,CPoint和CSize,可能后来版本的ATL也提供了,WTL作者推荐使用ATL的实现,所以:#include ...
- hdu 1316 How many Fibs?(高精度斐波那契数)
// 大数继续 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn : ...
- OpenJudge 2747 数字方格
1.链接地址: http://bailian.openjudge.cn/practice/2747 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 如上图,有3个 方格,每个 ...
- linux系统制作简单流程
制作嵌入式平台使用的Linux内 核, 方法和制作PC平台 的Linux内 核基本一致, 下面使用 对比的方式介绍如何制作用 于6410开发板的内 核. 1. 清除原有配置与中间文件x86: make ...