【洛谷】CF140D New Year Contest 题解

题目

CF140D New Year Contest 题解

按时长排序,在 \(0\) 时之前做的都在 \(0\) 时提交,\(0\) 时之后的都直接提交,然后贪心做。

理解其正确性:

考虑对 \(0\) 时后罚时的理解:如果有没做出来的题,那么每一分钟都会对罚时有贡献,所以我们要使题的数量尽可能小,所以贪心是正确的。

代码如下 :

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 105;
int a[maxn];
int n, i, ans = 0, sum = 0;
signed main()
{
    ios::sync_with_stdio;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a + 1, a + 1 + n); // 排序
    for (i = 1; i <= n; i++)
    {
        if (sum + a[i] > 710)
        {
            break;
        }
        else if (sum + a[i] <= 350)
        {
            sum += a[i];
        }
        else
        {
            sum += a[i];
            ans += sum - 350;
        }
    }
    cout << i - 1 << " " << ans << endl;
    return 0;
}
posted @ 2024-01-22 19:11  空驶过的是  阅读(6)  评论(0)    收藏  举报