Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
优先队列真心快。。优化之前是1000MS+,优化之后是60MS+, DP也是400MS+。。。。再附两组数据和DP代码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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator