1 条题解

  • 0

    题意分析

    1. 问题转化:选择kk根绳索时,最大承重w=k×min(t1,...,tk)w = k \times \min(t_1,...,t_k)
    2. 关键性质:最优解必定选择撕裂重量最大的kk根绳索
    3. 数学表达wmax=max1kn(k×tk)w_{max} = \max_{1 \leq k \leq n} (k \times t_k'),其中tkt_k'为排序后第kk大的撕裂重量

    解题思路

    1. 排序处理:将绳索按撕裂重量降序排列
    2. 极值计算
      • 对排序后的数组,计算所有kk对应的wk=k×tkw_k = k \times t_k
      • 取所有wkw_k的最大值
    3. 边界情况:所有绳索单独使用时的最大值max(ti)\max(t_i)需参与比较

    实现步骤

    1. 输入处理:读取测试数据,存储撕裂重量数组
    2. 排序操作:对每个测试用例的数组进行降序排序
    3. 遍历计算
      • 初始化max_weight=0max\_weight = 0
      • kk11nn,计算current_weight=k×t[k1]current\_weight = k \times t[k-1]
      • 更新max_weight=max(max_weight,current_weight)max\_weight = \max(max\_weight, current\_weight)
    4. 输出结果:返回max_weightmax\_weight

    代码实现

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            vector<int> ropes(n);
            for (int i = 0; i < n; ++i) {
                cin >> ropes[i];
            }
            
            sort(ropes.begin(), ropes.end());
            
            int max_weight = 0;
            for (int i = 0; i < n; ++i) {
                int current = ropes[i] * (n - i);
                if (current > max_weight) {
                    max_weight = current;
                }
            }
            
            cout << max_weight << endl;
        }
        return 0;
    }
    • 1

    信息

    ID
    1293
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    4
    已通过
    1
    上传者