ACW734. 能量石

该博客介绍了如何运用贪心策略和动态规划解决能量石问题。首先,通过贪心算法对物品根据消耗时间和衰减值进行排序,然后使用01背包的动态规划方法,将时间视为体积,费用作为价值,求解在不超过总时间限制下能获取的最大价值。代码中展示了具体的实现过程。
摘要由CSDN通过智能技术生成

题目

贪心的微扰邻项目和dp的结合.

**题意:**给定n个物品的消耗时间s,初始价值e,每秒价值的衰减值l.
对于每个物品,当选择它的时候,获得的价值为当前的价值,而不用考虑再衰减。
能量最多衰减至0.

思路: 先贪心,后dp.
假设最优解的能量石排列长度为k(1<=k<=n)k(1<=k<=n) .

贪心
那么对于任意两个位置al和al+1 (1 <= l < k)
交换以后的价值之和大于等于交换之前的价值之和Ei−t∗Li+Ej−(t+Si)∗Lj>=Ej−t∗Lj+Ei−(t+Sj)∗Li。
即Li * Sj >= Si * Lj
所以可以按照该规律进行排序。
此时已用贪心完成预处理。

dp
01背包
将时间看作体积
将费用作为价值
f[i][j]表示从前i个物品中选,时间不超过j的最大价值
f[j]表示时间恰好用了j的最大价值
考虑物品价值的衰减,物品i被选择时的价值为max(ei - (t - si) * li,0),时间代价为si.

代码:

// Problem: 能量石
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/736/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i) 
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 103;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T; 
int cas = 1;
int f[N*N]; //表示容积用了恰好j的最大价值
struct node{
	int s,e,l;
	bool operator<(const node&rhs)
	{
		return s*rhs.l < rhs.s * l;
	}
}a[N];
void solve()
{
   printf("Case #%d: ",cas++);
   read(n);
   int sum = 0;
   for(int i=1;i<=n;++i)
   {
   	 read(a[i].s),read(a[i].e),read(a[i].l);
   	 sum += a[i].s;
   }
   sort(a+1,a+1+n);
   mem(f,-0x3f);
   f[0] = 0;
   for(int i=1;i<=n;++i)
   {
   	  for(int j=sum;j>=a[i].s;--j)
   	  {
   	  	 f[j] = max(f[j],f[j-a[i].s] + max(0,a[i].e - (j - a[i].s)*a[i].l) ); //消耗
   	  }
   }
   int ans = 0;
   fir(i,1,sum) ans = max(ans,f[i]);
   write(ans); puts("");
}
signed main(void)
{  
   // T = 1;
   // OldTomato; cin>>T;
   read(T);
   while(T--)
   {
   	 solve();
   }
   return 0;
}

别再摆烂了lgl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值