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

用优先队列的V*N的算法竟然要63ms,求优化!

Posted by zero6842 at 2015-01-16 23:25:08 on Problem 1276
#include <stdio.h>
#include <string.h>

#define MAX_CASH  100002
#define MAX       12

int d[MAX];
int count[MAX];
int used[MAX_CASH];
int cash[MAX_CASH];
int v;

// priority queue
typedef struct {
    int pos;
    int val;
} Entry;
Entry queue[MAX_CASH];
int start, end, window;

static inline
void insert_queue(int val, int pos)
{
    while ( start < end && queue[end-1].val <= val) { end--;}
    queue[end].pos = pos;
    queue[end++].val = val;
    while ((queue[start].pos + window) <= pos) start++;
}

static inline 
int get_max_from_queue()
{
    return queue[start].val;
}

int main()
{
    int n, i, j;

    while (scanf("%d %d", &v, &n) != EOF) {

        for (i = 1; i <= n; i++) {
            scanf("%d %d", &count[i], &d[i]);
        }

        for (i = 0; i <= v; i++) {
            cash[i] = 0;
        }

        for (i = 1; i <= n; i++) {
            // reinit the priority-queue
            int w = d[i];
            start = end = 0;
            window = count[i] + 1;

            // try put
            for (j = 0; j < w; j++) {
                int m, cur = j;
                for (m = 0; cur <= v; m++, cur += w) {
                    // insert cash[cur] - m*w
                    insert_queue(cash[cur] + j - cur, m);
                    // assign it with max + m*w
                    cash[cur] = get_max_from_queue() + cur - j;
                }
            }
        }
        printf("%d\n", cash[v]);
    }
}


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