UVA 12325 Zombie's Treasure Chest 分类枚举算法

本文探讨了在特定数据规模下,如何通过优化枚举算法避免超时错误(TLE)。通过案例分析,展示了当数据范围在int类型内时,如何根据不同情况选择最优策略,包括物品数量尽可能多的情况和单一物品数量较大的情况,以实现算法效率的最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所给数据在int范围内 

单独使用其中一种枚举算法都会导致TLE

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;

int main(){
  int T;
  scanf("%d", &T);
  for(int kase = 1; kase <= T; kase++) {
    int n, s1, v1, s2, v2;
    scanf("%d%d%d%d%d", &n, &s1, &v1, &s2, &v2);
    if(s1 > s2){ // s2 is bigger
      swap(s1, s2);
      swap(v1, v2);
    }
    LL ans = 0;
    if(n / s2 >= 2<<16){ // both s1 and s2 are small
      for(LL i = 0; i <= s1; i++){ //item 2 as many as possible
        ans = max(ans, v2*i + (n-s2*i)/s1*v1);
      }
      for(LL i = 0; i <= s2; i++){ //item 1 as many as possible
        ans = max(ans, v1*i + (n-s1*i)/s2*v2);
      }
    }else{ // s2 is large
      for(LL i = 0; s2*i <= n; i++)
	// search the number of item 2 and then item 1 as many as possible
        ans = max(ans, v2*i + (n-s2*i)/s1*v1);
    }
    printf("Case #%d: %lld\n", kase, ans);
  }
  return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值