Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求助:我用DP求出了,最大值,最小值,但是在判断每个数是取还是不取时没思路,非常感谢!!

Posted by yjy at 2008-04-04 10:09:41 on Problem 3399

#include <string.h>
#include <algorithm>
using namespace std;
const int SIZE=105;
double dp[SIZE][SIZE][5];
int dat[SIZE];
bool ans[SIZE][SIZE][5];
int main()
{
	int i,j;
	while (scanf ("%d %d",&n,&k)!=EOF)
	{
		for (i=1;i<=n;i++)
			scanf ("%d",&dat[i]);
		for (i=1;i<=n;i++)
			dp[i][0][1]=dp[i][0][2]=1;
		for (j=1;j<=k;j++)
			dp[0][j][1]=dp[0][j][2]=1;
		dp[0][0][1]=dp[0][0][2]=1;
		for (i=1;i<=n;i++)
			for (j=1;j<=k;j++)
			{
				ans[i][j][1]=ans[i][j][2]=true;
				if (dat[i]>0)
				{
					dp[i][j][1]=dp[i-1][j-1][1]*dat[i];
					dp[i][j][2]=dp[i-1][j-1][2]*dat[i];
				}
				else
				{
					dp[i][j][1]=dp[i-1][j-1][2]*dat[i];
					dp[i][j][2]=dp[i-1][j-1][1]*dat[i];
				}
				if (i>j)
				{if (dp[i][j][1]>dp[i-1][j][1])
				 {
					ans[i][j][1]=false;
					dp[i][j][1]=dp[i-1][j][1];
				 }
				 if (dp[i][j][2]<dp[i-1][j][2])
				 {
					ans[i][j][2]=false;
					dp[i][j][2]=dp[i-1][j][2];
				 }
				}
			}
		printf("~~~%lf~~~\n",dp[n][k][2]);

	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator