# 特殊的排序算法，用C语言大神都来讨论下谢谢

Problem Description

Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage Sherlock's milking equipment, Sherlock would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes Sherlock a total of X + Y units of time to exchange two cows whose grumpiness levels are X and Y.

Please help Sherlock calculate the minimal time required to reorder the cows.

Input

Line 1: A single integer: N

Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.

Output

Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.

Sample Input

3

2

3

1

Sample Output

7

- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答

*1*条回答

#### 相关推荐

- 回答 1 已采纳 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage Sherlock's milking equipment, Sherlock would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes Sherlock a total of X + Y units of time to exchange two cows whose grumpiness levels are X and Y. Please help Sherlock calculate the minimal time required to reorder the cows. Input Line 1: A single integer: N Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i. Output Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness. Sample Input 3 2 3 1 Sample Output 7
- 回答 2 已采纳 用C语言编写一个对数组排序的程序，要求使用递归算法实现。
- 回答 1 已采纳 Problem Description QXJ has N robots on the plane, the i-th is at (xi,yi), numbereded 1 to N. Every robot is painted by one kind of color, numbered 1 to M. Each robots can move K times. In one move,a robot at (x,y) can move to (x−1,y),(x,y+1),(x+1,y),(x,y−1). After exactly K moves, she wants robots with same color to gather at the same postion and the robot on the i-th color gather at different postion with robots on (i-1)-th or (i+1)-th color. Now she wants to know how many ways of moving these robots following to rules above. Two ways are different if one of final postions of certain robot is different or there is at least one robot whose moving path is different. Input The first line is the number of test cases T(T≤10). The first line of each case contains three integer N(1≤N≤200),M(1≤M≤20),K(1≤K≤500), indicating the number of robots ,the number of color and the number of steps robots can move. The second line,contains M integer mi, indicating the number of robots with the i-th color. The robots numbered [1,m1] are on the 1st color.The robots numbered [m1+1,m1+m2] are one the 2nd color, ans so on. The next N line,each contains two integers xi,yi, indicating the postion of i-th robots.. (0≤|xi,yi|≤250). Output For each test case, output a single line "Case #x: y", where x is the case number, starting from 1. And y is the answer(module 109+7). Sample Input 2 3 3 1 1 1 1 1 0 0 1 1 2 4 2 2 2 2 0 1 0 3 0 2 0 4 Sample Output Case #1: 49 Case #2: 256
- 回答 1 已采纳 Problem Description There is a directed acyclic graph with n vertices and m edges. You are allowed to delete exact k edges in such way that the lexicographically minimal topological sort of the graph is minimum possible. Input There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case: The first line contains three integers n, m and k (1≤n≤100000,0≤k≤m≤200000) -- the number of vertices, the number of edges and the number of edges to delete. For the next m lines, each line contains two integers ui and vi, which means there is a directed edge from ui to vi (1≤ui,vi≤n). You can assume the graph is always a dag. The sum of values of n in all test cases doesn't exceed 106. The sum of values of m in all test cases doesn't exceed 2×106. Output For each test case, output an integer S=(∑i=1ni⋅pi) mod (109+7), where p1,p2,...,pn is the lexicographically minimal topological sort of the graph. Sample Input 3 4 2 0 1 2 1 3 4 5 1 2 1 3 1 4 1 2 3 2 4 4 4 2 1 2 2 3 3 4 1 4 Sample Output 30 27 30
- 5年前回答 7 已采纳 直接上代码，图： #include #include #include typedef struct{ int*pt; int cur; int len; }intArr; void bubblesort(intArr*ia){ int i,j,t,n=ia->cur; for(i=n;i>2;i--) for(j=1;jpt[j]>ia->pt[j-1]){ t=ia->pt[j-1]; ia->pt[j-1]=ia->pt[j]; ia->pt[j]=t; } } void printarray(intArr*ia){ int n=ia->cur; while(n>0) printf("%d\t",ia->pt[--n]); printf("\n"); } void insertarray(intArr*ia,int t){ if(ia->cur>=ia->len) ia->pt=(int*)realloc(ia,ia->len+=10); if(ia->pt) ia->pt[ia->cur++]=t; bubblesort(ia); printarray(ia); } int main(){ intArr *ia,iA; int i; ia=&iA; ia->pt=(int*)calloc(10,sizeof(int)); ia->cur=0,ia->len=10; printf("输入待排序数列，以空格间隔，以0结尾\n"); scanf("%d",&i); while(i!=0) insertarray(ia,i), scanf("%d",&i); system("pause"); return 0; } 上面是完整代码，下面截图： ![图片说明](https://img-ask.csdn.net/upload/201604/03/1459673062_5488.png) ![图片说明](https://img-ask.csdn.net/upload/201604/03/1459673080_613667.png) ![图片说明](https://img-ask.csdn.net/upload/201604/03/1459673144_603157.png) 最后一张是测试结果，可以看到在中间的某些排序中，最后两位总是错位，而且，在bubblesort()排序算法中，if（）里面的>也让我很费解，按理应该降序，实际却是升序，求助大神解答
- 回答 2 已采纳 Problem Description Mike has many friends. Here are nine of them: Alice, Bob, Carol, Dave, Eve, Frank, Gloria, Henry and Irene. Mike is so skillful that he can master n languages (aka. programming languages). His nine friends are all weaker than he. The sets they can master are all subsets of Mike's languages. But the relations between the nine friends is very complex. Here are some clues. 1. Alice is a nice girl, so her subset is a superset of Bob's. 2. Bob is a naughty boy, so his subset is a superset of Carol's. 3. Dave is a handsome boy, so his subset is a superset of Eve's. 4. Eve is an evil girl, so her subset is a superset of Frank's. 5. Gloria is a cute girl, so her subset is a superset of Henry's. 6. Henry is a tall boy, so his subset is a superset of Irene's. 7. Alice is a nice girl, so her subset is a superset of Eve's. 8. Eve is an evil girl, so her subset is a superset of Carol's. 9. Dave is a handsome boy, so his subset is a superset of Gloria's. 10. Gloria is a cute girl, so her subset is a superset of Frank's. 11. Gloria is a cute girl, so her subset is a superset of Bob's. Now Mike wants to know, how many situations there might be. Input The first line contains an integer T(T≤20) denoting the number of test cases. For each test case, the first line contains an integer n(0≤n≤3000), denoting the number of languages. Output For each test case, output ''Case #t:'' to represent this is the t-th case. And then output the answer. Sample Input 2 0 2 Sample Output Case #1: 1 Case #2: 1024
- 回答 2 已采纳 因为想在C语言的基础上实现随机森林，但是搜集到的大多都是基于python。 所以，如果有大神能指教一下就太好了！跪谢~
- 回答 1 已采纳 Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of the Ancient). If you have no idea of him, here is some brief introduction of his legendary antecedents: Twin sons to the great Prophet, Terrorblade and Magina were blessed with divine powers: Terrorblade granted with an unnatural affinity with life forces; Magina gifted with energy manipulation. Magina's eventual overexposure to the magic gradually augmented his elemental resistances and bestowed him the unique ability to move faster than light itself. Now, broken by Terrorblade's fall to the dark side, Magina answers the Sentinel's call in a desperate bid to redeem his brother. Every bitter strike turns the Scourge's evil essences upon themselves, culminating in a finale that forces his enemy to awaken to the void within and spontaneously implode. Magina has a very IMBA (imbalanced) skill – Blink, yes, move from one place to another place in a wink. Our problem begins at there. As a formidable hero in the later stage, Magina always farm with the wild monsters for a long time. To make the farming more efficient, Magina use Blink frequently to jump here and there. Here we assume Blink skill has no CD, that is, we can use this skill at any time we want. There are N spots of the wild monsters, and Magina can choose any one to begin. For every spots, Magina may use Ti time to kill the monsters and gain Gi units money, or he choose blink to other spots, which is known to our brilliant Magina. If the monsters in a spot were killed, it won’t appear any more. Now Magina want to get M units money to but some excellent equipment, say Battle Fury for example. As a hero to save the world, there is no much time left for Magina, he wonders the minimum time for him to gain at least M units money. Input The first line contains a single integer T, indicating the number of test cases. Each test case begins with two integers N, M. Their meanings are the same as the description. Then N blocks follow, each one describes a spot of wild monsters. The first line of each block is there integers Ti, Gi and Ki. Ti is the time, Gi is the units of money, Ki is the number of spots Magina can blink to from here. Then Ki integer Cij follow, indicating the spots’ ID Magina can blink to. You may assume no ID would appear twice. The spots are described with ID increasing from 1 to N. Input ensure if you can blink from i to j, you can also blink from j to i. Technical Specification 1. 1 <= T <= 50 2. 1 <= N <= 50 3. 1 <= Ti <= 10000000 4. 1 <= M, Gi <= 1000000000 5. 1 <= Ki < N 6. 1 <= Cij <= N Output For each test case, output the case number first, then the minimum time for Magina to gain at least M units money, if can’t, output “Poor Magina, you can't save the world all the time!”. Sample Input 3 1 4 2 5 0 1 5 1 4 0 4 10 1 9 0 3 3 1 3 3 3 2 2 4 4 4 1 3 Sample Output Case 1: 2 Case 2: Poor Magina, you can't save the world all the time! Case 3: 10
- 回答 3 已采纳 ![图片说明](https://img-ask.csdn.net/upload/201611/25/1480009578_857936.jpg) 求解答代码加一点解释= =
- 回答 1 已采纳 Problem Description We all understand equations such as: 3 + 8 = 4 + 7 But what happens if we look at equations with strings instead of numbers? What would addition and equality mean? Given two strings x and y, we define x + y to be the concatenation of the two strings. We also define x = y to mean that x is an anagram of y. That is, the characters in x can be permuted to form y. You are given n distinct nonempty strings, each containing at most 10 lowercase characters. You may also assume that at most 10 distinct characters appear in all the strings. You need to determine if you can choose strings to put on both sides of an equation such that the "sums" on each side are "equal" (by our definitions above). You may use each string on either side 0 or more times, but no string may be used on both sides. Input The input consists of a number of cases. Each case starts with a line containing the integer n (2 <= n <= 100). The next n lines contain the n strings. The input is terminated with n = 0. Output For each case, print either "yes" or "no" on one line indicating whether it is possible to form an equation as described above. If it is possible, print on each of the next n lines how many times each string is used, with the strings listed in the same order as the input. On each line, print the string, followed by a space, followed by the letter "L", "R", or "N" indicating whether the string appears on the left side, the right side, or neither side in the equation. Finally, this is followed by a space and an integer indicating how many times the string appears in the equation. Each numeric output should fit in a 64-bit integer. If there are multiple solutions, any solution is acceptable. Sample Input 2 hello world 7 i am lord voldemort tom marvolo riddle 0 Sample Output no yes i L 1 am L 1 lord L 1 voldemort L 1 tom R 1 marvolo R 1 riddle R 1
- crazy_kid_hnf的博客 在学习快速排序的过程中有幸看到了一位大神的博文，觉得对于快速排序的理解与讲解都非常不错，这里转载一下，希望那位大神不要介意： 快速排序萌萌哒详解 坐在马桶上看算法：快速排序 算法的精髓在于，跟它一...
- 3年前weixin_42122779的博客 学习C语言有几天了，第一个分享程序：实现冒泡排序算法原理的简单动画展示，动画原理是最简单的清屏重绘。环境是WIN764,EGE库，CODEBLOCK 13.12。欢迎大神指正，能接受轻拍，很基础的东西，也能接受伸手党。我的...
- 2年前Black_黑色的博客 十大经典排序算法（动图演示） </h1> <div class="clear"></div> <div class="postBody"> 0、算法概述 0.1 算法分类 十种常见排序算法可以分为两大类： 比较类排序：通过比较来决定...
- 嵌入式资讯精选的博客 文章来源于：单片机与嵌入式算法（Algorithm）：计算机解题的基本思想方法和步骤。算法的描述：是对要解决一个问题或要完成一项任务所采取的方法和步骤的描述，包括需要什么数据（输入什么数...
- 6年前lchad的博客 上一篇文章我们一起学习了直接...可能有很多同学说快速排序,堆排序,我都会,这些简单的插入排序我都不屑于用.确实,以上几种算法相对于之前的O(n^2)级别的算法真的是弱爆了,效率可能还会差上千万倍,但是我们不妨翻看一下
- 5月前司二水的博客 想让各位大神帮我看看，我这个排序算是一个什么排序。 #include <stdio.h> int main() { int a[10],n=10,t,i,b; for(i=0;i<n;i++) scanf("%d ",&a[i]); for(b=0;b<n;b++) { for(i=b+1;i<n;...
- Shim_ZoMoe的博客 最近由于长时间没写过基本的排序算法，结果导致只知道大概思想便不知怎么去编写这些算法的代码了，所以借着一下午的时间把基本的几个排序算法的代码写了一边，算是对它的复习吧！ 一 .「冒泡排序」： 冒泡...
- 1年前éng？的博客 几种C语言经典排序算法笔记 本人在复习也还在学习C语言，这里自己重新写了几个简单的经典排序算法，在这里做个小笔记以便查阅。网上关于相关算法的描述已经很多了，这里就简单描述一下，把代码实现放上。这是第一次...
- 7年前偷钻石的小子的博客 首先，你要对快速排序的思想有一定的了解，先看快速排序的代码。
- weixin_39834745的博客 展开全部总得思想还是冒泡排序，改良一下就可以了e69da5e887aa62616964757a686964616f31333335343961。#include#include#includeintmain(void){chara[300];chartemp;intlen;intloop,loop1;intflag;gets(a);len=(int)...