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

优先队列真心快。。优化之前是1000MS+,优化之后是60MS+, DP也是400MS+。。。。再附两组数据和DP代码

Posted by 058179 at 2013-05-05 12:54:47 on Problem 1456 and last updated at 2013-05-05 15:48:08
5
50 3
30 2
40 2
20 2
50 1
6
50 6
20 5
10 2
20 2
30 2
40 2
两组结果是都140
DP:
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 10005
struct Products
{
	int p, d;
}products[MAX];

int n, MaxProfit[MAX];
bool cmp(Products A, Products B) 
{
	if(A.d == B.d) return A.p < B.p;
	return A.d < B.d;
}
int main()
{
	int i, j;
	while(scanf("%d", &n) != EOF)
	{
		for(i = 0; i < n; i ++) scanf("%d%d", &products[i].p , &products[i].d );
		memset(MaxProfit, 0, sizeof(MaxProfit));
		sort(products, products + n, cmp);
		for(i = 0; i < n; i ++)
		{
			for(j = products[i].d - 1; j >= 0  ; j --)
				if(MaxProfit[j + 1] < MaxProfit[j] + products[i].p )
					MaxProfit[j + 1] = MaxProfit[j] + products[i].p;
		}
		for(i = 1; i < n; i ++) MaxProfit[0] = MaxProfit[0] > MaxProfit[i] ? MaxProfit[0] : MaxProfit[i];
		printf("%d\n", MaxProfit[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