[HG] Market explanations

Title Description

In the town of bits, a total of \ (n \) stores, numbers followed by \ (1 \) to \ (n \) .
Each store will only sell an article, the first of which \ (i \) store items priced at \ (C_i \) , value of \ (v_i \) , and the store opened in time for the \ (t_i \) .
\ (Byteasar \) plan \ (m \) time shopping, the first of which \ (i \) time shopping time \ (T_i \) , budget \ (M_i \)
each purchase time, \ (Byteasar \ ) will buy at each store up to an item, of course, he can not choose what to buy
if shopping at stores opened earlier time, then obviously he can not be shopping in this store.
Now \ (Byteasar \) want to know, for each plan, he bought up to the total value of the number of items. Please write a program to help \ (Byteasar \) reasonable arrangements for shopping plan.
Note: Each spent amount shall not exceed the budget, the budget must be spent not finished, and the budget can not be left to other programs to use.
for\ (100 \% \) specific number据:
\ (n, m \ in [1,300]; c_i, M_i \ in [1, 10 ^ 9]; vi, t_i, Ti \ in [1,300] \)
Observations found do not open long long

answer

Obviously because \ (C_i, M_i \ Leq 10 ^ 9 \) , can not do 01 backpack.
So we asked the offline ordering process, and DP in chronological order of increasing.
For each time an item, we "do down" 01 backpack, that is a calculated value of the minimum cost required.

analysis

While swatting the examination room, a small open space, \ (20 \) PTS roll rough.
Later still baffling hyperactivity rules once, resulting in changes to \ (80 \) PTS.

Code

#include <cstdio>
#include <algorithm>

using namespace std;

struct Mark{
    int c, v, t;
    bool operator < (const Mark &rhs) const {
        return t < rhs.t;
    }
} market[305];

struct Qry{
    int t, m, id;
    bool operator < (const Qry &rhs) const {
        return t < rhs.t;
    }
} qry[100005];

int g[90005];
int qans[100005];

inline void flush(int u){
    for (int i = 90000; i >= market[u].c; --i){
        if (g[i] > g[i - market[u].c] + market[u].v)
            g[i] = g[i - market[u].c] + market[u].v;
    }
    for (int i = 90000; i >= 1; --i)
        if (g[i] > g[i + 1]) g[i] = g[i + 1];
}

int main(){
    int n, m; scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i)
        scanf("%d %d %d", &market[i].v, &market[i].c, &market[i].t);
    sort(market + 1, market + n + 1);
    for (int i = 1; i <= m; ++i){
        int t, m; scanf("%d %d", &t, &m);
        qry[i] = {t, m, i};
    }
    sort(qry + 1, qry + m + 1);
    for (int i = 1; i <= 90004; ++i) g[i] = 1000000007;
    for (int i = 1, pos = 1; i <= m; ++i){
        for ( ; market[pos].t <= qry[i].t && pos <= n; ++pos)
            flush(pos);
        qans[qry[i].id] = upper_bound(g, g + 90001, qry[i].m) - g - 1;
    }
    for (int i = 1; i <= m; ++i)
        printf("%d\n", qans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/linzhengmin/p/11771410.html