3
31
2015
0

Tyvj 1005 采药

 

#include <iostream>
using namespace std;
int t,m,f[1001],w[101],v[101];
int main()
{
	cin>>t>>m;
	for (int i=1;i<=m;++i)
		cin>>w[i]>>v[i];
	for (int i=1;i<=m;++i)
		for (int j=t;j>=w[i];--j)
			if (f[j]<f[j-w[i]]+v[i])
				f[j]=f[j-w[i]]+v[i];
	cout<<f[t];
	return 0;
}
Category: 题解 | Tags: Tyvj 动规
3
30
2015
0

Tyvj 1004 滑雪

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int s[100][100],r,c,a[100][100],ans;

int f(int x,int y) { //从x,y开始最长走几步
	if (s[x][y]!=-1) return s[x][y]; //如果算过就直接返回
	s[x][y]=1;
	if (x>0 && a[x][y]>a[x-1][y])
		s[x][y]=max(s[x][y],f(x-1,y)+1);
	if (y>0 && a[x][y]>a[x][y-1])
		s[x][y]=max(s[x][y],f(x,y-1)+1);
	if (x<r-1 && a[x][y]>a[x+1][y])
		s[x][y]=max(s[x][y],f(x+1,y)+1);
	if (y<c-1 && a[x][y]>a[x][y+1])
		s[x][y]=max(s[x][y],f(x,y+1)+1);
	return s[x][y];
}

int main() {
	cin>>r>>c; memset(s,-1,sizeof(s));
	for (int i=0;i<r;++i)
		for (int j=0;j<c;++j)
			cin>>a[i][j];
	for (int i=0;i<r;++i)
		for (int j=0;j<c;++j)
			ans=max(ans,f(i,j)); //求最大值
	cout<<ans;
	return 0;
}
Category: 题解 | Tags: Tyvj 记忆化搜索
3
30
2015
0

Tyvj 1003 越野跑

#include <cstdio>
#include <iostream>
using namespace std;
long long m,t;
int u,f,d,n;
char s;
int main()
{
	cin>>m>>n>>u>>f>>d;
	u+=d; f*=2; //去加回的距离一起算
	for (int i=1;i<=n;++i) {
		cin>>s;
		if (s=='u' || s=='d') t+=u; //若为坡,直接加
		else t+=f;
		if (t>m) {
			cout<<i-1; //直到超过秒数
			return 0;
		}
	}
	if (t<=m) cout<<n;
	return 0;
}
Category: 题解 | Tags: Tyvj 模拟

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com