每日算法 - day 45

每日算法

those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.3.31


P1802 5倍经验日

#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>

using namespace std;
const int N = 1005;

int n , x;
struct node{
	int l, w, c;
	node(int l,int w,int c):l(l),w(w),c(c){}
	node(){}
};
node p[N];
int f[N][N];
int main()
{
	cin >> n >> x;
	for(int i = 1;i <= n ;i ++)
	{
		scanf("%d %d %d",&p[i].l,&p[i].w,&p[i].c);
	}
	
	for(int i = 1;i <= n;i ++)
	{
		for(int j = 0;j <= x ;j ++)
		{
			if(j < p[i].c)
			{
				f[i][j] = f[i-1][j] + p[i].l;
			}else{
				f[i][j] = max(f[i-1][j] + p[i].l,f[i-1][j-p[i].c] + p[i].w);
			}
		}	
	}
	cout << (long long)f[n][x] * 5 << endl;
	return 0;
} 

P1048 采药

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;
const int N = 1010;
int T , m;
int w[N] , v[N] ; //花费时间
int f[N][N];
int main()
{
	cin >> T >> m;
	for(int i = 1;i <= m ;i ++)
	{
		scanf("%d %d",&w[i],&v[i]);
	}
	for(int i = 1;i <= m ;i ++)
	{
		for(int j = 1;j <= T ;j ++)
		{
			if(j >= w[i])
			{
				f[i][j] = max(f[i-1][j],f[i-1][j - w[i]] + v[i]);
			}else f[i][j] = f[i-1][j];
		}
	}
	cout << f[m][T] << endl;
	return  0;
}

一维

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;
const int N = 1005;
int t , m;
int w[N], v[N], f[N];
int main()
{
	cin >> t >> m;
	for(int i = 1;i <= m; i++)
	{
		scanf("%d %d",&w[i],&v[i]);
	}

	for(int i = 1;i <= m ;i ++)
	{
		for(int j = t;j >= w[i] ;j --)
		{
			f[j] = max(f[j - w[i]] + v[i],f[j]);
		}

	}
	cout << f[t] << endl;
	return 0;
}

P1734 最大约数和

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>

using namespace std;
const int N = 1010;
int s;
int v[N];
void func(int x)
{
	int ans = 0;
	for(int i = 1;i < x;i ++)
	{
		if(x % i == 0)ans += i;
	}
	v[x] = ans;
}
int f[N][N];
int main()
{
	cin >> s;
	for(int i = 1;i <= s ;i ++)func(i);

	for(int i = 1 ;i <= s; i++) //当前物品
	{
		for(int j = 1;j <= s;j ++) //当前容量
		{
			if(j >= i) // i 同时有代表其本身得权值 w[i]
			{
				f[i][j] = max(f[i-1][j],f[i-1][j-i] + v[i]);
			}else f[i][j] = f[i-1][j];
		}
	}
	cout << f[s][s] << endl;	
	return 0;
}

一维优化

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>

using namespace std;
const int N = 1010;
int s;
int v[N];
void func(int x)
{
	int ans = 0;
	for(int i = 1;i < x;i ++)
	{
		if(x % i == 0)ans += i;
	}
	v[x] = ans;
}
int f[N];
int main()
{
	cin >> s;
	for(int i = 1;i <= s ;i ++)func(i);

	for(int i = 1 ;i <= s; i++) //当前物品
	{
		for(int j = s;j >= i;j --) //当前容量
		{        // i 同时有代表其本身得权值 w[i]
			f[j] = max(f[j],f[j-i] + v[i]);
		}
	}
	cout << f[s] << endl;	
	return 0;
}
posted @ 2020-03-31 23:47  _starsky  阅读(137)  评论(0编辑  收藏  举报