题解:P17297 [ICPC 2026 Xi'an I] Qenerals
super_peach · · 题解
题目分析
有一个非常明显的性质:每个堡垒每秒都只能提供
下面是具体做法。
- 先给
a 数组从小到大排序。 - 从头到尾看一下
a 数组,如果现在的兵力不够,就得等兵力够了再占领。 ::::info[得等多久呢] 假设现有兵力是x ,占领堡垒数(每秒兵力增长数)是y ,准备占领第i 个堡垒,占领第i 个堡垒的兵力消耗是a_i ,要等待的秒数即为\lceil \dfrac{a_i - x}{y} \rceil 。 :::: - 兵力够了之后看一看这个堡垒值不值得占领。每个堡垒在第
t 秒占领完成之后会提供m - t 的兵力,而占领堡垒要花费a_i 的兵力,判断收入和支出的大小决定是否占领。 - 由于
a 数组已经从小到大排过序了,所以a_i 是越来越大的,而总时间m 是不变的,随着时间流逝t 是越来越大的,所以m - t 是越来越小的,当看到第一个a_i > m - t 的时候就可以知道后面的所有堡垒都不值得占领,直接跳出循环计算总兵力即可。#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; }::::info[声明] 本题解为作者独立创作。 ::::