比赛 20110412 评测结果 AWWAWAWWWW
题目名称 山顶问题 最终得分 30
用户昵称 fanzeyi 运行时间 0.000 s
代码语言 C 内存使用 0.00 MiB
提交时间 2011-04-12 10:34:48
显示代码纯文本
/*
 * =====================================================================================
 *
 *       Filename:  peaks.c
 *
 *    Description:  20100412 Peaks 山顶问题
 *
 *        Version:  1.0
 *        Created:  2011年04月12日 01时45分49秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Zeray Rice , fanzeyi1994[at]gmail.com
 *        Company:  Fanhe.org
 *
 * =====================================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp(const void *a, const void *b)
{
	return *(long*)a - *(long*)b;
}

int main()
{
	FILE *fin = fopen("peaks.in", "r");
	FILE *fout = fopen("peaks.out", "w");
	long *mount;
	long *peaks;
	register long tmp = 0;
	long n;
	short least;
	int i, j;
	register unsigned int min = 0xFFFFFFFF;
	fscanf(fin, "%ld %hd", &n, &least);
	mount = (long*)calloc(n, sizeof(long));
	peaks = (long*)calloc(n, sizeof(long));
	for( i = 0 ; i < n ; i++ )
	{
		fscanf(fin, "%ld", &mount[i]);
		if( mount[i] < min )
			min = mount[i];
	}
	j = 0;
	for( i = 0 ; i < n ; i++ )
	{
		mount[i] -= min;
		if( mount[i] != 0 )
			tmp += mount[i];
		if( mount[i] == 0 && tmp == 0 )
			continue;
		if( mount[i] == 0 && tmp != 0)
		{
			peaks[j] = tmp;
			j++;
			tmp = 0;
		}
	}
	peaks[j] = tmp;
	qsort(peaks, j+1, sizeof(long), cmp);
	tmp = 0;
	for( i = 0 ; i <= least ; i++ )
		tmp += peaks[i];
	fprintf(fout, "%ld", tmp);
	return 0;
}