TYVJ 1209 拦截导弹 解题报告

  刚把这题昨晚,顺便就用最大匹配把这题做了,不给思路了,直接上代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int num[20];
int f[20];
int ans = 1;
int from[20], used[20];
int map[20][20];
int n;

int find(int k)
{
	int i;
	for(i = 0; i < n; i++){
		if(map[k][i] && !used[i]){
			used[i] = 1;
			if(from[i] == -1 || find(from[i])){
				from[i] = k;
				return 1;
			}
		}
	}
	return 0;
}

int main(int argc, char **argv)
{
	int i = 1, j;
	scanf("%d", &num[0]);
	while(scanf(",%d", &num[i]) == 1){
		i++;
	}
	n = i;
	f[0] = 1;
	for(i = 1; i < n; i++){
		f[i] = 1;
		for(j = 0; j < i; j++){
			if(num[j] >= num[i] && f[i] < f[j] + 1){
				f[i] = f[j] + 1;
			}
		}
		if(ans < f[i]){
			ans = f[i];
		}
	}
	printf("%d,", ans);
	for(i = 0; i < n; i++){
		//因为只能按顺序打, 所以要从i + 1开始循环
		for(j = i + 1; j < n; j++){
			if(num[i] >= num[j]){
				map[i][j] = 1;
			}
		}
	}
	for(i = 0; i < n; i++){
		from[i] = -1;
	}
	for(i = ans = 0; i < n; i++){
		memset(used, 0, sizeof(used));
		if(find(i)){
			ans++;
		}
	}
	ans = n - ans;
	printf("%d\n", ans - 1);
	return 0;
}
posted @ 2011-07-15 15:28  zqynux  阅读(286)  评论(0编辑  收藏  举报