跳到正文

题解

题解:P17297 [ICPC 2026 Xi'an I] Qenerals

super_peach约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年8月28日
收录
2026年10月1日
更新
2026年11月9日
篇幅
约 3 分钟
题目
P17297

版本

  1. v1当前

题目分析

有一个非常明显的性质:每个堡垒每秒都只能提供 11 个士兵,所以要选择花费兵力少的堡垒去占领。

下面是具体做法。

  • 先给 aa 数组从小到大排序。
  • 从头到尾看一下 aa 数组,如果现在的兵力不够,就得等兵力够了再占领。
得等多久呢

假设现有兵力是 xx,占领堡垒数(每秒兵力增长数)是 yy,准备占领第 ii 个堡垒,占领第 ii 个堡垒的兵力消耗是 aia_i,要等待的秒数即为 ⌈ai−xy⌉\lceil \dfrac{a_i - x}{y} \rceil。

  • 兵力够了之后看一看这个堡垒值不值得占领。每个堡垒在第 tt 秒占领完成之后会提供 m−tm - t 的兵力,而占领堡垒要花费 aia_i 的兵力,判断收入和支出的大小决定是否占领。
  • 由于 aa 数组已经从小到大排过序了,所以 aia_i 是越来越大的,而总时间 mm 是不变的,随着时间流逝 tt 是越来越大的,所以 m−tm - t 是越来越小的,当看到第一个 ai>m−ta_i > m - t 的时候就可以知道后面的所有堡垒都不值得占领,直接跳出循环计算总兵力即可。
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int a[N];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    while (t--){
        int n,m;
        cin >> n >> m;
        for (int i = 1;i <= n;i++) cin >> a[i];
        sort(a + 1,a + n + 1);
        int x = 0,y = 1,t = 0;
        for (int i = 1;i <= n;i++){
            if (x < a[i]){
                int time = (a[i] - x + y - 1) / y;
                if (t + time > m) break;
                x += time * y;
                t += time;
            }
            if (a[i] < m - t){
                y++;
                x -= a[i];
            }
            else break;
        }
        x += (m - t) * y;
        cout << x << '\n';
    }
    return 0;
}
声明

本题解为作者独立创作。

原文