你想为蛇建造一座神庙。这座庙宇将建在一座山脉上,这座山可以被认为是n块,在那里,第一街区的高度是由hi确定的。庙宇将在地块的一个连续段上建造,其高度应从1开始,每次增加1,直到一定高度,然后每一次精确降低1至1,即1、2、3、.x-1,x,x-1,x-2,.,1可以对应于寺庙.此外,除了庙宇以外的所有街区的高度都应该是零高度,这样从左边或右边看它的人就可以看到它。你想建造一座寺庙。为此,您可以降低一些块的高度。在一次操作中,可以将块的高度降低1单位。找出建造寺庙所需的最少操作次数。输入输入的第一行包含表示测试用例数量的整数T。对T测试用例的描述如下。每个测试用例的第一行包含一个整数n,下一行包含n个整数,其中第一个整数表示每个测试用例的hi输出,输出一个与测试用例答案相对应的整数。约束条件1≤T≤10 2≤N≤10 5 1≤Hi≤10^9输入3 3 1 2 1 4 1 1 1 5 1 2 6 2 1输出0 1 1 3
#include <stdio.h>
void quickSort(int arr[], int low, int high);
int partition (int arr[], int low, int high);
void swap(int* a, int* b);
int main(void) {
// your code goes here
int t=0, i=0, n=0, move=0, j=0;
scanf("%d\n", &t);
for(i=1;i<=t;++i)
{
scanf("%d\n", &n);
int arr[n];
int i5;
for(i5=0;i5<n;++i5)
scanf("%d ", &arr[i5]);
quickSort(arr, 0, n-1);
int max= (arr[n-1]!=arr[n-2])?arr[n-2]+1:arr[n-1];
move = arr[n-1]-max;
--max;
int i3=0;
for(i3=n-2;i3>0;--max)
{
move+=((arr[i3]-max)+(arr[i3-1]-max));
i3-=2;
if(max==1)
{
break;
}
}
for(j=0;j<=i3;++j)
{
move+=arr[j];
}
printf("%d\n", move);
}
return 0;
}
// A utility function to swap two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
int j;
for (j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}发布于 2017-05-28 09:22:34
您的代码在某些情况下不起作用。例如,输入1,1,2,2,4,答案应该是6,但代码返回1。
发布于 2017-05-28 14:27:29
可读性
如果赋值语句和条件中的运算符和操作数之间存在空格,则代码的可读性将大大提高,increment (++)和decrement (--)是例外情况:
int main(void) {
// your code goes here
int t = 0;
int n = 0;
int move = 0;
scanf("%d\n", &t);
for(int i = 1; i <= t; ++i)
{
scanf("%d\n", &n);
int arr[n];
for(int i5 = 0; i5 < n; ++i5)
{
scanf("%d ", &arr[i5]);
}
quickSort(arr, 0, n-1);
int max = (arr[n-1] != arr[n-2]) ? arr[n-2] +1 : arr[n-1];
move = arr[n-1] - max;
--max;
int i3;
for(i3 = n-2; i3 > 0; --max)
{
move += ((arr[i3] - max) + (arr[i3-1] - max));
i3 -= 2;
if(max == 1)
{
break;
}
}
for(int j = 0; j <= i3; ++j)
{
move += arr[j];
}
printf("%d\n", move);
}
return 0;
}i5 scanf()没有正确缩进。由于代码方括号了所有的for loops和if statements,所以i5循环也应该如上面所示放在括号中。
根据需要声明变量
for loop控制变量可以在for循环的开头声明,如i、i5和j所示。
i3变量在for loop之外正确声明,因为它在for loop后面的语句中使用。
当初始化在不同的行上时,更容易阅读它们,如上面所示。
发布于 2017-05-28 05:51:13
您可以使用mergesort代替快速排序,因为这可能会遇到超过时间限制。
https://codereview.stackexchange.com/questions/164358
复制相似问题