ZROI # 1005

ZROI # 1005

A very confusing topic ...
First, we found that \ (M \) and no eggs available.
So we direct it not a bird.
Then we found that we needed to find a minimum set of syrup \ (S \ ) .
so that the following holds:
\ [\ sum_ {I \ K_i in {S}} = N * V_I \]
wherein \ (K_i \) represents the \ (I \) species selected from the several syrup, \ (V_I \ ) represents the concentration of syrup \ (\ cfrac V_I} {M} {\) .
However, this demand is not good. we consider the conversion.
If we \ (V_I \) becomes \ (V_I-N \) , then we becomes the target so that the following equation holds minimum \ (S: \)
\ [\ sum_ {I \ * K_i in {S} (V_I-N)} = 0 \]
so like a lot to do.
we found that in fact, the equivalent of a full backpack \ (: \)
requires a full capacity just \ (0 \) backpack, each item has a minimum number of positive and negative article preferably unlimited.
consider the direct \ (the DP \) .
so that \ (f_i \)It represents a full capacity just \ (I \) backpack, each item has a minimum number of positive and negative article preferably unlimited.
Metastasis:
\ (F_i = min (F_i, F_ {} + I-V_I 1) \) (forward, rolling array-based operations)

Note that, if \ (v_i \) is less than \ (0 \) , it needs to come from a large transfer otherwise required from small transfer over.

\(Code:\)

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define one first
#define two second
#define rint read<int>
#define int long long
#define pb push_back

using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
       while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
   return f * x ;
}

const int base = 25e4 ;
const int N = 1e7 + 100 ;

int n , m , k , v[N] , f[N] ;

signed main (int argc , char * argv[]) {
    k = rint () ; n = rint () ; m = rint () ; MEM ( f , 0x7f ) ;
    rep ( i , 1 , k ) { v[i] = rint () ; v[i] -= n ; f[v[i]+base] = 1 ; }
    sort ( v + 1 , v + k + 1 ) ; k = unique ( v + 1 , v + k + 1 ) - v - 1 ;
    rep ( i , 1 , k ) {
        rep ( j , - base , base )
            if ( j + base - v[i] < 0 ) continue ;
            else f[j+base] = min ( f[j+base] , f[j+base-v[i]] + 1 ) ;
        per ( j , base , - base )
            if ( j + base - v[i] < 0 ) continue ;
            else f[j+base] = min ( f[j+base] , f[j+base-v[i]] + 1 ) ;
    }
    if ( f[base] >= 0x7f7f7f7f ) puts ("-1") ; 
    else printf ("%lld\n" , f[base] ) ; 
    return 0 ;
}

Guess you like

Origin www.cnblogs.com/Equinox-Flower/p/11571543.html