51nod 1163 最高的奖励

本文探讨了一种解决任务调度问题的算法,旨在最大化总奖励。通过两种方法实现:优先队列和并查集,分别展示了如何在有限时间内选择最优任务组合以获取最高奖励。

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

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163&judgeId=598136

1163 最高的奖励 

基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题

 收藏

 关注

有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突,这需要你来取舍。求能够获得的最高奖励。

Input

第1行:一个数N,表示任务的数量(2 <= N <= 50000)
第2 - N + 1行,每行2个数,中间用空格分隔,表示任务的最晚结束时间E[i]以及对应的奖励W[i]。(1 <= E[i] <= 10^9,1 <= W[i] <= 10^9)

Output

输出能够获得的最高奖励。

Input示例

7
4 20
2 60
4 70
3 40
1 30
4 50
6 10

Output示例

230

看a出来的人挺多的,就过来a这道题。。。。没想到卡着了,容器不熟悉,果然要吃大亏的.....一直在想怎么能让时间确定的情况下选最大的的和。。。怎么排序都不行因为它不是一个动态的,在网上一看...优先队列woc..........原来是这样

#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn  50005
#define ll long long
using namespace std;
struct node
{
    int time;
    int p;
}ac[maxn];
bool  cmp(node a,node b)
{
    if(a.time==b.time)
        return a.p>b.p;
    return a.time<b.time;
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ac[i].time>>ac[i].p;
    }
   sort(ac,ac+n,cmp);
   ll ans=0;
   priority_queue<int,vector <int> ,greater <int> >que;
   for(int i=0;i<n;i++)
   {
       int q=ac[i].p;
       if(ac[i].time>que.size())
       {
            ans+=q;
            que.push(q);
       }
       else
       {
           ans+=q;
           que.push(q);
           ans-=que.top();
           que.pop();
       }
   }
   cout<<ans<<endl;
    return 0;
}

优先队列写出来了,在网上看别人的代码终于找到一个不用优先队列的,emmmm她竟然用了并查集,而且是我想用的之前一直没想通的先排质量,然后不断的更新,,真的厉害了。

#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn  50005
#define ll long long
using namespace std;
struct node
{
    int time;
    int p;
}ac[maxn];
int par[maxn];     //最晚i秒的事情可在j秒完成
bool  cmp(node a,node b)
{
    return a.p>b.p;
}
int find(int x)
{
    if(x<=0) return -1;
    else if(x==par[x])
    {
        par[x]=x-1;
    }
    else  return par[x]=find(par[x]);//路径压缩
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ac[i].time>>ac[i].p;
        par[i]=i;
    }
   sort(ac,ac+n,cmp);
   ll ans=0;
   for(int i=0;i<n;i++)
   {
        int t=ac[i].time;
        if(find(t)>=0)
        {
            ans+=ac[i].p;
        }
   }
   cout<<ans<<endl;
    return 0;
}

 

题目 51nod 3478 涉及一个矩阵问题,要通过最少的操作次数,使得矩阵中至少有 `RowCount` 行和 `ColumnCount` 列是回文的。解决这个问题的关键在于如何高效地枚举所有可能的行和列组合,并计算每种组合所需的操作次数。 ### 解法思路 1. **预处理每一行和每一列变为回文所需的最少操作次数**: - 对于每一行,计算将其变为回文所需的最少操作次数。这可以通过比较每对对称位置的值是否相同来完成。 - 对于每一列,计算将其变为回文所需的最少操作次数,方法同上。 2. **枚举所有可能的行和列组合**: - 由于 `N` 和 `M` 的最大值为 8,因此可以枚举所有可能的行组合和列组合。 - 对于每一种组合,计算其所需的最少操作次数,并取最小值。 3. **计算操作次数**: - 对于每一种组合,需要计算哪些行和列需要修改,并且注意行和列的交叉可能会重复计算,因此需要去重。 ### 代码实现 以下是一个可能的实现方式,使用了枚举和位运算来处理组合问题: ```python def min_operations_to_palindrome(matrix, row_count, col_count): import itertools N = len(matrix) M = len(matrix[0]) # Precompute the cost to make each row a palindrome row_cost = [] for i in range(N): cost = 0 for j in range(M // 2): if matrix[i][j] != matrix[i][M - 1 - j]: cost += 1 row_cost.append(cost) # Precompute the cost to make each column a palindrome col_cost = [] for j in range(M): cost = 0 for i in range(N // 2): if matrix[i][j] != matrix[N - 1 - i][j]: cost += 1 col_cost.append(cost) min_total_cost = float('inf') # Enumerate all combinations of rows and columns rows = list(range(N)) cols = list(range(M)) from itertools import combinations for row_comb in combinations(rows, row_count): for col_comb in combinations(cols, col_count): # Calculate the cost for this combination cost = 0 # Add row costs for r in row_comb: cost += row_cost[r] # Add column costs for c in col_comb: cost += col_cost[c] # Subtract the overlapping cells for r in row_comb: for c in col_comb: # Check if this cell is part of the palindrome calculation if r < N // 2 and c < M // 2: if matrix[r][c] != matrix[r][M - 1 - c] and matrix[N - 1 - r][c] != matrix[N - 1 - r][M - 1 - c]: cost -= 1 min_total_cost = min(min_total_cost, cost) return min_total_cost # Example usage matrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ] row_count = 2 col_count = 2 result = min_operations_to_palindrome(matrix, row_count, col_count) print(result) ``` ### 代码说明 - **预处理成本**:首先计算每一行和每一列变为回文所需的最少操作次数。 - **枚举组合**:使用 `itertools.combinations` 枚举所有可能的行和列组合。 - **计算成本**:对于每一种组合,计算其成本,并考虑行和列交叉的重复计算问题。 ### 复杂度分析 - **时间复杂度**:由于 `N` 和 `M` 的最大值为 8,因此枚举所有组合的时间复杂度为 $ O(N^{RowCount} \times M^{ColCount}) $,这在实际中是可接受的。 - **空间复杂度**:主要是存储预处理的成本,空间复杂度为 $ O(N + M) $。 ### 相关问题 1. 如何优化矩阵中行和列的枚举组合以减少计算时间? 2. 在计算行和列的交叉时,如何更高效地处理重复计算的问题? 3. 如果矩阵的大小增加到更大的范围,如何调整算法以保持效率? 4. 如何处理矩阵中行和列的回文条件不同时的情况? 5. 如何扩展算法以支持更多的操作类型,例如翻转某个区域的值?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值