[JSOI2007]建筑抢修(回)(堆)

优先修截至时间短的建筑!!!

大佬ac代码

设置截止时间为first,消耗时间为second,比我的巧妙

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;
const int N = 150100;

pair<ll, ll> arr[N];
priority_queue<ll> q;
ll sum = 0, ans = 0, n;

int main()
{
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld%lld", &arr[i].second, &arr[i].first);
	}
	sort(arr + 1, arr + 1 + n);  //按截至时间排序
	for (int i = 1; i <= n; i++)
	{
		sum += arr[i].second;      //先假设能修
		if (sum <= arr[i].first)
		{
			q.push(arr[i].second);   //确实能修
			ans++;
		}
		else
		{
			if (q.top() > arr[i].second)//修不了但可替换(堆顶的修理时间比当前的修理时间长)
			{
				sum -= q.top();   
				q.pop();                 //不修堆顶的建筑
				q.push(arr[i].second);    //修当前的建筑

			}
			else sum -= arr[i].second;    //确实不能修
		}
	}
	printf("%lld\n", ans);
	return 0;
}

菜鸡的 ac代码

按题目依次读入,一开始没考虑pair使用sort的优先级,就wa了,下面是改正代码

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;
const int N = 150100;

pair<ll,ll> arr[N];
priority_queue<ll> q;
ll sum = 0,ans=0,n;

bool cmp(pair<ll, ll> x, pair<ll, ll> y)
{
    if(x.second!=y.second)
	    return x.second < y.second;
    eles
	    return x.first < y.first;
}
int main()
{
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld%lld", &arr[i].first, &arr[i].second);
	}
	sort(arr + 1, arr + 1 + n,cmp);
	for (int i = 1; i <= n; i++)
	{
		sum += arr[i].first;
		if (sum <= arr[i].second)
		{
			q.push(arr[i].first);
			ans++;
		}
		else
		{
			if (q.top() > arr[i].first)
			{
				sum -= q.top();
				q.pop();
				q.push(arr[i].first);
			
			}
			else sum -= arr[i].first;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

 关键:以截至时间排序

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据引用[1],dp[u][j]表示在u子树中选取恰好j个人时能获得的最大价值。而根据引用,该问题的时间复杂度为O(log2​104×nm)。 对于洛谷P2143 [JSOI2010] 巨额奖金问题,我们可以使用动态规划来解决。具体步骤如下: 1. 首先,我们需要构建一棵树来表示员工之间的关系。树的根节点表示公司的总经理,其他节点表示员工。每个节点都有一个权值,表示该员工的奖金金额。 2. 接下来,我们可以使用动态规划来计算每个节点的dp值。对于每个节点u,我们可以考虑两种情况: - 如果选择节点u,则dp[u][j] = dp[v][j-1] + value[u],其中v是u的子节点,value[u]表示节点u的奖金金额。 - 如果不选择节点u,则dp[u][j] = max(dp[v][j]),其中v是u的子节点。 3. 最后,我们可以通过遍历树的所有节点,计算出dp[u][j]的最大值,即为所求的巨额奖金。 下面是一个示例代码,演示了如何使用动态规划来解决洛谷P2143 [JSOI2010] 巨额奖金问题: ```python # 构建树的数据结构 class Node: def __init__(self, value): self.value = value self.children = [] # 动态规划求解最大奖金 def max_bonus(root, j): dp = [[0] * (j+1) for _ in range(len(root)+1)] def dfs(node): if not node: return for child in node.children: dfs(child) for k in range(j, 0, -1): dp[node.value][k] = max(dp[node.value][k], dp[node.value][k-1] + node.value) for child in node.children: for k in range(j, 0, -1): for l in range(k-1, -1, -1): dp[node.value][k] = max(dp[node.value][k], dp[node.value][k-l-1] + dp[child.value][l]) dfs(root) return dp[root.value][j] # 构建树 root = Node(1) root.children.append(Node(2)) root.children.append(Node(3)) root.children[0].children.append(Node(4)) root.children[0].children.append(Node(5)) root.children[1].children.append(Node(6)) # 求解最大奖金 j = 3 max_bonus_value = max_bonus(root, j) print("最大奖金为:", max_bonus_value) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值