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 ...
随机推荐
- 关于SWT的线程问题
大部分情况下,GUI界面编程是不用考虑线程问题的,SWT已经帮助我们隐藏了底层的线程调用. 但是一些特殊应用的实现,却不得不涉及SWT线程编程.比如说当进度条的例子(以后要加上,现在还没有做,没有总结 ...
- 搭建高可用的MongoDB集群
http://www.csdn.net/article/2014-04-09/2819221-build-high-avialable-mongodb-cluster-part-1/1 在大数据的时代 ...
- ACM——Digital Roots
http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 Digital Roots 时间 ...
- Sql Server通过BCP数据导出Excel
1.1. bcp的主要参数介绍 bcp共有四个动作可以选择. (1) 导入. 这个动作使用in命令完成,后面跟需要导入的文件名. (2) 导出. 这个动作使用out命令完成,后面跟需要导出的文件名. ...
- WebService简单介绍
什么是Web Services 一直没有一个明确的答案,最近听了一个大牛讲WebService,顿时明了了,作个记录免得以后忘了. N年之前,各公司都在炒作SOA,不过现在已经没人再提了,现在都改叫W ...
- 第八篇、UITableView常用功能(左滑出现多个按钮,多选删除等)
1.左滑动出现多个按钮 /** * 只要实现了这个方法,左滑出现按钮的功能就有了 (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES) */ ...
- XMPP即时通讯(代码实现)
1.配置XMPP(XMPPConfig.m) 2.配置XMPPFramework框架 3.创建单例类(XMPPManager.h/XMPPManager.m)管理器 XMPPManager.m: #i ...
- UITableViewCell和UITableViewHeaderFooterView的重用
不管是系统自带的还是自定义的UITableViewCell,对于它们合理的使用都是决定一个UITableView的性能的关键因素.应该确保以下三条: UITableViewCell的重复利用:首先对象 ...
- .net 生成pdf表格
只需要建一个类文件就搞定了 public class CreatePDF { public static CreatePDF Current { get { return new CreatePDF( ...
- Wix: Using Patch Creation Properties - Small Update
Source Reference: wix help document -- WiX Toolset License Using Patch Creation Properties A patch ...