【背包】CF 837D,1032E,922E,1203F2

题解尽请期待...

  • T1:CF837D Round Subset
    • title
    • code
  • T2:CF1032E The Unbearable Lightness of Weights
    • title
    • code
  • T3:CF922E Birds
    • title
    • code
  • T4:CF1203F2 Complete the Projects (hard version)
    • title
    • code

【背包】CF 837D,1032E,922E,1203F2_第1张图片

T1:CF837D Round Subset

title

code

#include 
#include 
#include 
using namespace std;
#define ll long long
int n, k;
int cnt2[205], cnt5[205];
ll dp[205][6005];

int main() {
	memset( dp, -1, sizeof( dp ) );
	dp[0][0] = 0;
	scanf( "%d %d", &n, &k );
	for( int i = 1;i <= n;i ++ ) {
		ll t;
		scanf( "%lld", &t );
		while( t % 2 == 0 ) t >>= 1, cnt2[i] ++;
		while( t % 5 == 0 ) t /= 5, cnt5[i] ++;
	}
	for( int i = 1;i <= n;i ++ )
		for( int j = k;j;j -- )
			for( int p = 6000;p >= cnt5[i];p -- )
				if( dp[j - 1][p - cnt5[i]] != -1 )
					dp[j][p] = max( dp[j][p], dp[j - 1][p - cnt5[i]] + cnt2[i] );
	ll ans = 0;
	for( int i = 1;i <= 6000;i ++ )
		ans = max( ans, min( 1ll * i, dp[k][i] ) );
	printf( "%lld", ans );
	return 0;
}

T2:CF1032E The Unbearable Lightness of Weights

title

code

#include 
#include 
#include 
using namespace std;
#define Pair pair < int, int >
map < Pair, int > dp, pre;
int n, tot, maxx;
int a[105], cnt[105], C[105][105];

int main() {
	scanf( "%d", &n );
	for( int i = 1;i <= n;i ++ ) {
		scanf( "%d", &a[i] );
		maxx = max( maxx, a[i] );
		if( ( ++ cnt[a[i]] ) == 1 ) ++ tot;
	}
	if( tot <= 2 ) return ! printf( "%d", n );
	for( int i = 0;i <= n;i ++ ) {
		C[i][0] = C[i][i] = 1;
		for( int j = 1;j < i;j ++ )
			C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
	}
	dp[make_pair( 0, 0 )] = pre[make_pair( 0, 0 )] = 1;
	for( int i = 1;i <= n;i ++ ) {
		for( map < Pair, int > :: iterator it = pre.begin();it != pre.end(); it ++ ) {
			Pair temp = make_pair( it -> first.first + a[i], it -> first.second + 1 );
			dp[temp] += it -> second;
		}
		pre = dp;
	}
	int ans = 1;
	for( int w = 1;w <= maxx;w ++ )
		for( int t = 2;t <= cnt[w];t ++ )
			if( dp[make_pair( w * t, t )] == C[cnt[w]][t] )
				ans = max( ans, t );
	printf( "%d", ans );
	return 0;
}

T3:CF922E Birds

title

code

#include 
#include 
#include 
using namespace std;
#define ll long long
ll n, W, B, X, ans;
ll c[1005], cost[1005];
ll dp[2][10005];

int main() {
	scanf( "%lld %lld %lld %lld", &n, &W, &B, &X );
	for( int i = 1;i <= n;i ++ )
		scanf( "%lld", &c[i] ), ans += c[i];
	for( int i = 1;i <= n;i ++ )
		scanf( "%lld", &cost[i] );
	memset( dp, -1, sizeof( dp ) );
	for( int i = 0;i <= c[1];i ++ )
		dp[1][i] = W - cost[1] * i;
	for( int i = 2;i <= n;i ++ ) {
		memset( dp[i & 1], -1, sizeof( dp[i & 1] ) );
		for( ll j = 0;j <= ans;j ++ )
			for( ll k = 0;k <= min( j, c[i] );k ++ ) {
				if( dp[( i - 1 ) & 1][j - k] < 0 ) continue;
				ll temp = min( dp[( i - 1 ) & 1][j - k] + X, W + ( j - k ) * B ) - k * cost[i];
				dp[i & 1][j] = max( dp[i & 1][j], temp );
			}
	}
	while( ans && dp[n & 1][ans] < 0 ) ans --;
	printf( "%lld", ans );
	return 0;
}

T4:CF1203F2 Complete the Projects (hard version)

title

code

#include 
#include 
using namespace std;
struct node {
	int a, b;
	node(){}
	node( int A, int B ) {
		a = A, b = B;
	}
}p1[105], p2[105];
int n, rating, cnt1, cnt2, ans, result;
int a[105], b[105];
int dp[105][60005];
//dp[i][j] 前i个任务 能力值为j 最多能完成多少个任务 
bool cmp1( node x, node y ) {
	return x.a < y.a;
}

bool cmp2( node x, node y ) {
	return x.a + x.b > y.a + y.b;
}

int Fabs( int x ) {
	return ( x > 0 ) ? x : -x;
}

int main() {
	scanf( "%d %d", &n, &rating );
	for( int i = 1;i <= n;i ++ ) {
		scanf( "%d %d", &a[i], &b[i] );
		if( b[i] >= 0 ) p1[++ cnt1] = node( a[i], b[i] );
		else p2[++ cnt2] = node( a[i], b[i] );
	}
	sort( p1 + 1, p1 + cnt1 + 1, cmp1 );
	for( int i = 1;i <= cnt1;i ++ )
		if( rating >= p1[i].a )
			rating += p1[i].b, ans ++;
		else break;
	sort( p2 + 1, p2 + cnt2 + 1, cmp2 );
	for( int i = 1;i <= cnt2;i ++ )
		if( p2[i].a <= rating && Fabs( p2[i].b ) <= rating ) {
			result = max( result, 1 );
			dp[i][rating + p2[i].b] = 1;
			for( int j = i - 1;j;j -- )
				for( int k = max( Fabs( p2[i].b ), p2[i].a );k <= 60000;k ++ )
					if( dp[j][k] ) {
						dp[i][k + p2[i].b] = max( dp[i][k + p2[i].b], dp[j][k] + 1 );
						result = max( result, dp[i][k + p2[i].b] );
					}
		}
	printf( "%d", ans + result );
	return 0;
}

你可能感兴趣的:(#,普通DP,#,矩阵加速)