5 November

Topological sorting

for (int i=1; i<=n; ++i) if (!ind[i]) q.push(i);
while (!q.empty()) {
    int now=q.top(); q.pop(); printf("%d ", now);
    for (int k=head[now]; k; k=nex[k]) 
        if (--ind[to[k]]==0) q.push(to[k]);
}

Setsutsumi DP of Kamiki

Existing \ (N \) th items and volume \ (M \) backpack. Bulky items were \ (V_1, V_2, ..., V_n \) . Demand does not pick up the article \ (I \) , the the number of programs loaded backpack.

Set \ (f (n, V) \) forward enumeration backpack, \ (G (n-, V) \) reverse enumeration backpack, the program number \ (\ displaystyle \ sum_ {i = }} ^ {n-. 1 \ sum_ {J} = 0 {m} ^ F (. 1-I, J) \ CDOT G (I +. 1, MJ) \) .

The error that is not thoroughly understand 1D and codes similarities and differences between the two-dimensional backpack: 1 Knapsack been without direct assignment, 2 Knapsack or need \ (f (i, j) = f (i-1, j) \) of .

#include <cstdio>

int n, m, w[1003], f[1003][10003], g[1003][10003], ans;

int main() {
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i) scanf("%d", &w[i]);
    f[0][0]=1;
    for (int i=1; i<=n; ++i) {
        for (int j=m; j>=w[i]; --j) f[i][j]=(f[i-1][j]+f[i-1][j-w[i]])%1014;
        for (int j=w[i]-1; ~j; --j) f[i][j]=f[i-1][j]; // (*)
    }
    g[n+1][0]=1;
    for (int i=n; i; --i) {
        for (int j=m; j>=w[i]; --j) g[i][j]=(g[i+1][j]+g[i+1][j-w[i]])%1014;
        for (int j=w[i]-1; ~j; --j) g[i][j]=g[i+1][j]; // (*)
    }
    for (int i=1; i<=n; ++i) {
        ans=0;
        for (int j=0; j<=m; ++j) ans=(ans+f[i-1][j]*g[i+1][m-j])%1014;
        printf("%d ", ans);
    }
    return 0;
}

Sequence

It gives the number of columns \ (\ {A_n \} \ ) , modified minimal elements, such that the number of columns \ (\ {A_n \} \ ) be a tolerance of 1 arithmetic sequence.

Pretreatment list of numbers \ (\ {A_n-n \ } \) to greatly simplify the present problem solution.

Guess you like

Origin www.cnblogs.com/greyqz/p/11807418.html