萌新不会动态规划求助
查看原帖
萌新不会动态规划求助
553625
scp020楼主2024/6/20 07:58

rt,拜谢同机房大佬讲的思路。

离散食物背包,连续食物贪心。

样例都可通过,WA 0pts

#include<bits/stdc++.h>
namespace fast_IO
{
	// 
};
using namespace fast_IO;
#define int long long
#define double long double
const int N=300,W=10010;
int n,w;
double ans;
struct Discrete // 离散
{
	int f[W];
	std::vector< std::pair<int,int> > v[W];
	std::vector<int> tmp;
	inline void add(const int w0,const int t0,const int delt)
	{
		v[w0].push_back(std::make_pair(t0,delt));
	}
	inline void solve()
	{
		memset(f,128,sizeof(f)),f[0]=0;
		for(int i=1;i<=w;i++)
			if(!v[i].empty())
			{
				tmp.clear();
				for(int j=0;j<v[i].size();j++)
					for(int k=1;k<=w/i;k++) tmp.push_back(v[i][j].first-(k-1)*v[i][j].second);
				std::nth_element(tmp.begin(),tmp.begin()+w/i,tmp.end(),std::greater<int>());
				for(int j=0;j<w/i;j++)
					for(int k=w;k>=i;k--) if(f[k-i]!=-9187201950435737457ll) f[k]=std::max(f[k],f[k-i]+tmp[j]);
			}
	}
};
struct Continuous // 连续
{
	double f[W],last;
	std::vector< std::pair<int,int> > v;
	std::vector< std::pair<double,double> > tmp;
	inline void add(const int t0,const int delt)
	{
		v.push_back(std::make_pair(t0,delt)),tmp.push_back(std::make_pair(0,0));
	}
	inline void solve()
	{
		memset(f,0,sizeof(f));
		std::sort(v.begin(),v.end(),std::greater< std::pair<int,int> >());
		for(int i=1;i<=w;i++)
		{
			last=i;
			for(int j=0;j<v.size();j++) tmp[j]=v[j];
			for(int j=0;j<v.size();j++)
				if(j+1<v.size())
				{
					if(last>(tmp[j].first-tmp[j+1].first)/tmp[j].second)
						f[i]+=((tmp[j].first-tmp[j+1].first)/tmp[j].second)*
						(tmp[j].first-0.5*tmp[j].second*((tmp[j].first-tmp[j+1].first)*1.0/tmp[j].second)),
						last-=(tmp[j].first-tmp[j+1].first)/tmp[j].second,
						tmp[j+1].second=tmp[j].second*tmp[j+1].second/(tmp[j].second+tmp[j+1].second);
					else
					{
						f[i]+=last*(tmp[j].first-0.5*tmp[j].second*last);
						break;
					}
				}else f[i]+=last*(tmp[j].first-0.5*tmp[j].second*last);
		}
	}
};
Discrete dis;
Continuous con;
char ch;
signed main()
{
	in>>n>>w;
	for(int i=1,w0,t0,delt;i<=n;i++)
	{
		in>>ch;
		if(ch=='C') in>>t0>>delt,con.add(t0,delt);
		else in>>w0>>t0>>delt,dis.add(w0,t0,delt);
	}
	con.solve(),dis.solve();
	if(con.v.empty())
	{
		if(dis.f[w]<0) std::cout<<"impossible";
		else std::cout<<std::fixed<<std::setprecision(7)<<dis.f[w];
		return 0;
	}
	for(int i=0;i<=w;i++) ans=std::max(ans,dis.f[i]+con.f[w-i]);
	std::cout<<std::fixed<<std::setprecision(7)<<ans;
	return 0;
}


2024/6/20 07:58
加载中...