CCF-CSP认证考试 202303-2 垦田计划 100分题解

更多 CSP 认证考试题目题解可以前往:CSP-CCF 认证考试真题题解


原题链接: 202303-2 垦田计划

时间限制: 1.0s
内存限制: 512.0MB

问题描述

顿顿总共选中了 n n n 块区域准备开垦田地,由于各块区域大小不一,开垦所需时间也不尽相同。据估算,其中第 i i i 块( 1 ≤ i ≤ n 1 \le i \le n 1in)区域的开垦耗时为 t i t_i ti 天。这 n n n 块区域可以同时开垦,所以总耗时 t T o t a l t_{Total} tTotal 取决于耗时最长的区域,即: t T o t a l = max ⁡ { t 1 , t 2 , ⋯   , t n } t_{Total} = \max \{ t_1, t_2, \cdots, t_n \} tTotal=max{t1,t2,,tn}

为了加快开垦进度,顿顿准备在部分区域投入额外资源来缩短开垦时间。具体来说:

  • 在第 i i i 块区域每投入 c i c_i ci 单位资源,便可将其开垦耗时缩短 1 1 1 天;
  • 耗时缩短天数以整数记,即第 i i i 块区域投入资源数量必须是 c i c_i ci 的整数倍;
  • 在第 i i i 块区域最多可投入 c i × ( t i − k ) c_i \times (t_i - k) ci×(tik) 单位资源,将其开垦耗时缩短为 k k k 天;
  • 这里的 k k k 表示开垦一块区域的最少天数,满足 0 < k ≤ min ⁡ { t 1 , t 2 , ⋯   , t n } 0 < k \le \min \{ t_1, t_2, \cdots, t_n \} 0<kmin{t1,t2,,tn};换言之,如果无限制地投入资源,所有区域都可以用 k k k 天完成开垦。

现在顿顿手中共有 m m m 单位资源可供使用,试计算开垦 n n n 块区域最少需要多少天?

输入格式

从标准输入读入数据。

输入共 n + 1 n+1 n+1 行。

输入的第一行包含空格分隔的三个正整数 n n n m m m k k k,分别表示待开垦的区域总数、顿顿手上的资源数量和每块区域的最少开垦天数。

接下来 n n n 行,每行包含空格分隔的两个正整数 t i t_i ti c i c_i ci,分别表示第 i i i 块区域开垦耗时和将耗时缩短 1 1 1 天所需资源数量。

输出格式

输出到标准输出。

输出一个整数,表示开垦 n n n 块区域的最少耗时。

样例输入1

4 9 2
6 1
5 1
6 2
7 1

样例输出1

5

样例解释

如下表所示,投入 5 5 5 单位资源即可将总耗时缩短至 5 5 5 天。此时顿顿手中还剩余 4 4 4 单位资源,但无论如何安排,也无法使总耗时进一步缩短。

i i i 基础耗时 t i t_i ti 缩减 1 1 1 天所需资源 c i c_i ci 投入资源数量 实际耗时
1 6 1 1 5
2 5 1 0 5
3 6 2 2 5
4 7 1 2 5

样例输入2

4 30 2
6 1
5 1
6 2
7 1

样例输出2

2

样例解释

投入 20 20 20 单位资源,恰好可将所有区域开垦耗时均缩短为 k = 2 k = 2 k=2 天;受限于 k k k,剩余的 10 10 10 单位资源无法使耗时进一步缩短。

子任务

70 % 70\% 70% 的测试数据满足: 0 < n , t i , c i ≤ 100 0 < n, t_i, c_i \le 100 0<n,ti,ci100 0 < m ≤ 1 0 6 0 < m \le 10^6 0<m106

全部的测试数据满足: 0 < n , t i , c i ≤ 1 0 5 0 < n, t_i, c_i \le 10^5 0<n,ti,ci105 0 < m ≤ 1 0 9 0 < m \le 10^9 0<m109


题解

对于一个天数 x x x,如果能在 x x x 天完成任务(缩短后的),必定能在 x + 1 , x + 2 , ⋯ x+1,x+2,\cdots x+1,x+2, 天内完成;反之,如果不能在 x x x 天完成任务,必定不能在 x − 1 , x − 2 , ⋯ x-1,x-2,\cdots x1,x2, 天内完成。

因此答案具有二分性,通过二分答案,找到最小的 x x x,使得能在 x x x 天完成任务,而不能再 x − 1 x-1 x1 天完成任务。

二分的 check 函数直接遍历每个开垦区域,计算每个区域缩短到 mid 天内完成需要消耗的总资源,与有的资源 m m m 去比较来判断是否满足条件。

时间复杂度: O ( n log ⁡ t ) \mathcal{O}(n\log t) O(nlogt)

参考代码(46ms,3.527MB)

/*
    Created by Pujx on 2024/3/19.
*/
#pragma GCC optimize(2, 3, "Ofast", "inline")
#include 
using namespace std;
#define endl '\n'
//#define int long long
//#define double long double
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
#define inf (int)0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define yn(x) cout << (x ? "yes" : "no") << endl
#define Yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
#define mem(x, i) memset(x, i, sizeof(x))
#define cinarr(a, n) for (int i = 1; i <= n; i++) cin >> a[i]
#define cinstl(a) for (auto& x : a) cin >> x;
#define coutarr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n]
#define coutstl(a) for (const auto& x : a) cout << x << ' '; cout << endl
#define all(x) (x).begin(), (x).end()
#define md(x) (((x) % mod + mod) % mod)
#define ls (s << 1)
#define rs (s << 1 | 1)
#define ft first
#define se second
#define pii pair<int, int>
#ifdef DEBUG
    #include "debug.h"
#else
    #define dbg(...) void(0)
#endif

const int N = 2e5 + 5;
//const int M = 1e5 + 5;
const int mod = 998244353;
//const int mod = 1e9 + 7;
//template  T ksm(T a, i64 b) { T ans = 1; for (; b; a = 1ll * a * a, b >>= 1) if (b & 1) ans = 1ll * ans * a; return ans; }
//template  T ksm(T a, i64 b, T m = mod) { T ans = 1; for (; b; a = 1ll * a * a % m, b >>= 1) if (b & 1) ans = 1ll * ans * a % m; return ans; }

int t[N], c[N];
int n, m, k, q;

void work() {
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) cin >> t[i] >> c[i];
    int l = k, r = 1e5, ans = r;
    while (l <= r) {
        auto check = [&] (int x) -> bool {
            int sum = m;
            for (int i = 1; i <= n; i++) {
                if (t[i] <= x) continue;
                if (sum < 1ll * (t[i] - x) * c[i]) return false;
                sum -= (t[i] - x) * c[i];
            }
            return true;
        };
        int mid = l + r >> 1;
        if (check(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    cout << ans << endl;
}

signed main() {
#ifdef LOCAL
    freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.in", "r", stdin);
    freopen("C:\\Users\\admin\\CLionProjects\\Practice\\data.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int Case = 1;
    //cin >> Case;
    while (Case--) work();
    return 0;
}
/*
     _____   _   _       _  __    __
    |  _  \ | | | |     | | \ \  / /
    | |_| | | | | |     | |  \ \/ /
    |  ___/ | | | |  _  | |   }  {
    | |     | |_| | | |_| |  / /\ \
    |_|     \_____/ \_____/ /_/  \_\
*/

关于代码的亿点点说明:

  1. 代码的主体部分位于 void work() 函数中,另外会有部分变量申明、结构体定义、函数定义在上方。
  2. #pragma ... 是用来开启 O2、O3 等优化加快代码速度。
  3. 中间一大堆 #define ... 是我习惯上的一些宏定义,用来加快代码编写的速度。
  4. "debug.h" 头文件是我用于调试输出的代码,没有这个头文件也可以正常运行(前提是没定义 DEBUG 宏),在程序中如果看到 dbg(...) 是我中途调试的输出的语句,可能没删干净,但是没有提交上去没有任何影响。
  5. ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 这三句话是用于解除流同步,加快输入 cin 输出 cout 速度(这个输入输出流的速度很慢)。在小数据量无所谓,但是在比较大的读入时建议加这句话,避免读入输出超时。如果记不下来可以换用 scanfprintf,但使用了这句话后,cinscanfcoutprintf 不能混用。
  6. main 函数和 work 函数分开写纯属个人习惯,主要是为了多组数据。

你可能感兴趣的:(c++)