TYVJ 1094 矩形分割 解题报告

  贪心,转题解:

很好的一道贪心。

首先一个需要明确的问题就是因为要分成1*1的格子,所以每一条边肯定都要切,只不过是切的次数的多少有所不同。对于每一条纵边来说,它所需要切得次数等于在这条纵向边切之前的已经切的横边的次数。对于横边就看纵边。这样,一个很显然的想法就是让代价大的先切,这样的话满足代价大的切得次数少。把横边和纵边排个序,然后不断的维护ans即可。

  代码:

#include <stdio.h>
#include <stdlib.h>
int heng[2000], shu[2000];
int ans;

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

int main(int argc, char **argv)
{
	int i, j;
	int m, n;
	scanf("%d%d", &n, &m);
	n--, m--;
	for(i = 0; i < n; i++){
		scanf("%d", &heng[i]);
	}
	for(i = 0; i < m; i++){
		scanf("%d", &shu[i]);
	}
	qsort(heng, n, sizeof(int), com);
	qsort(shu, m, sizeof(int), com);
	i = 0, j = 0;
	while(i < n || j < m){
		if(heng[i] > shu[j]){
			ans += (j + 1) * heng[i++];
		}else{
			ans += (i + 1) * shu[j++];
		}
	}
	printf("%d\n", ans);
	return 0;
}
posted @ 2011-07-21 11:54  zqynux  阅读(311)  评论(0编辑  收藏  举报