HDU 1950 Bridging signals ( DP动态规划 O(n^2) )

#include <stdio.h>
#define MAX_POARTS 40000

int numOfTests;
int numOfPorts;
int arrayOfPorts[MAX_POARTS + 1];
//lenOfIS[index]表示遍历arrayOfPorts到索引index的时候,以arrayOfPorts[index]为"结尾"的递增子序列的长度
int lenOfIS[MAX_POARTS + 1];//length of increasing subsequence
int result;//length of increasing subsequence

int main(){

	scanf("%d", &numOfTests);
	int test;
	for (test = 1; test <= numOfTests; test++){
		scanf("%d", &numOfPorts);
		int indexOfPort;
		for (indexOfPort = 1; indexOfPort <= numOfPorts; indexOfPort++)
			scanf("%d", &arrayOfPorts[indexOfPort]);
		
		result = 0;
		for (indexOfPort = 1; indexOfPort <= numOfPorts; indexOfPort++){
			lenOfIS[indexOfPort] = 1;
			int indexOfBefore;
			for (indexOfBefore = 1; indexOfBefore < indexOfPort; indexOfBefore++)
				if (arrayOfPorts[indexOfBefore] < arrayOfPorts[indexOfPort] && lenOfIS[indexOfBefore] + 1 > lenOfIS[indexOfPort])
					lenOfIS[indexOfPort] = lenOfIS[indexOfBefore] + 1;
			if (lenOfIS[indexOfPort] > result)
				result = lenOfIS[indexOfPort];
		
		}

		printf("%d\n", result);
	}

	return 0;
}

你可能感兴趣的:(动态规划,HDU,LIS,Signals,Bridging,1950)