动态规划

Feature


DP is an algorithm that is very flexible.

OJ Practise


POJ 2392


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxk= 4e2+3;
const int maxh= 4e4+3;

struct blk
{
	int h, a, c;
	blk(){};
	blk(int hh, int aa, int cc) : h(hh), a(aa), c(cc) {}
	bool operator < (const blk &cmp) const
	{
		return a< cmp.a;
	}
}elv[maxk];
int use[maxh];
int ht[maxh];

int main()
{
	int k, ans= -1;
	scanf("%d", &k);
	memset(ht, 0, sizeof(ht));
	for (int i= 0; i< k; ++i){
		int h, a, c;
		scanf("%d %d %d", &h, &a, &c);
		elv[i]= blk(h, a, c);
	}
	sort(elv, elv+k);
	ht[0]= 1;
	for (int i= 0; i< k; ++i){
		memset(use, 0, sizeof(use));
		for (int j= elv[i].h; j< elv[i].a+1; ++j){
			if (!ht[j] && ht[j-elv[i].h] && use[j-elv[i].h]< elv[i].c){
				use[j]= use[j-elv[i].h]+1;
				ht[j]= 1;
				ans= max(ans, j);
			}
		}
	}
	printf("%d", ans);

	return 0;
}

POJ 3093

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ull unsigned long long 
using namespace std;

const int maxn= 1e3+3;
const int maxv= 33;
const int maxd= 1e3+3;

int prc[maxv];
int mm[maxd];
int sm[maxv];

int main()
{
	int N, V, D;
	scanf("%d", &N);

	for (int i= 1; i<= N; ++i){
		ull ans= 0;
		memset(prc, 0, sizeof(prc));
		memset(sm, 0, sizeof(sm));
		memset(mm, 0, sizeof(mm));

		scanf("%d %d", &V, &D);
		for (int j= 1; j< V+1; ++j){
			scanf("%d", prc+j);
		}
		sort(prc+1, prc+1+V);
		if (prc[1]> D){
			printf("%d 0\n", i);
			continue;
		}

		for (int j= 1; j< V+1; ++j){
			sm[j]= sm[j-1]+prc[j];
		}
		mm[0]= 1;

		for (int j= V; j> 0; --j){
			for (int k= max(0, D-sm[j]+1); k<= D-sm[j-1]; ++k){
				ans+= mm[k];
			}
			for (int k= D; k>= prc[j]; --k){
				mm[k]+= mm[k-prc[j]];
			}
		}

		printf("%d %llu\n", i, ans);
	}

	return 0;
}
posted @ 2019-12-09 17:09  IdiotNe  阅读(79)  评论(0)    收藏  举报