2024.12.24 周四

2024.12.24 周四


Q1. 1100

You are given an array a a a of n n n positive integers and a score. If your score is greater than or equal to a i a_i ai, then you can increase your score by a i a_i ai and remove a i a_i ai from the array.

For each index i i i, output the maximum number of additional array elements that you can remove if you remove a i a_i ai and then set your score to a i a_i ai. Note that the removal of a i a_i ai should not be counted in the answer.


Q2. 1100

Monocarp is a student at Berland State University. Due to recent changes in the Berland education system, Monocarp has to study only one subject — programming.

The academic term consists of n n n days, and in order not to get expelled, Monocarp has to earn at least P P P points during those n n n days. There are two ways to earn points — completing practical tasks and attending lessons. For each practical task Monocarp fulfills, he earns t t t points, and for each lesson he attends, he earns l l l points.

Practical tasks are unlocked “each week” as the term goes on: the first task is unlocked on day 1 1 1 (and can be completed on any day from 1 1 1 to n n n), the second task is unlocked on day 8 8 8 (and can be completed on any day from 8 8 8 to n n n), the third task is unlocked on day 15 15 15, and so on.

Every day from 1 1 1 to n n n, there is a lesson which can be attended by Monocarp. And every day, Monocarp chooses whether to study or to rest the whole day. When Monocarp decides to study, he attends a lesson and can complete no more than 2 2 2 tasks, which are already unlocked and not completed yet. If Monocarp rests the whole day, he skips a lesson and ignores tasks.

Monocarp wants to have as many days off as possible, i. e. he wants to maximize the number of days he rests. Help him calculate the maximum number of days he can rest!


------------------------独自思考分割线------------------------

  • 用时:35 28。Q1有点思维。Q2是3状态贪心。

A1.

  1. 排序之后发现前面的一定可以选取,后面到某一个临界值。假思路:二分,没有仔细证明答案二段性。
  2. 从当前向后一个个找复杂度较高,考虑快速查找,em… 倒序考虑: 若 p r e [ i ] > = a [ i + 1 ] , f [ i ] = f [ i + 1 ] ; e l s e f [ i ] = i − 1 。 若pre[i]>=a[i+1],f[i]=f[i+1];else f[i]=i-1。 pre[i]>=a[i+1]f[i]=f[i+1]elsef[i]=i1
  3. 倒叙可行性的本质就是:利用后面的答案,先计算后面的答案可以让前面快速找到答案。遍历思想,数据维护,转移答案,有dp的味道。

A2.

  1. 那么一大堆就是:读好题意,贪心计算3个阶段。

------------------------代码分割线------------------------

A1.

#include 
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    struct Node
    {
        /* data */
        int x, i, f;
    };
    vector a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].x;
        a[i].i = i;
    }
    sort(a.begin() + 1, a.end(), [](Node &a, Node &b)
         { return a.x > b.x; });
    vector pre(n + 1);
    for (int i = 1; i <= n; i++)
        pre[i] = pre[i - 1] + a[i].x;

    for (int i = 1; i <= n; i++)
        if (i == 1)
            a[i].f = n - 1;
        else
        {
            if (pre[n] - pre[i - 1] >= a[i - 1].x)
                a[i].f = a[i - 1].f;
            else
                a[i].f = n - i;
        }

    sort(a.begin(), a.end(), [](Node &a, Node &b)
         { return a.i < b.i; });
    for (int i = 1; i <= n; i++)
        cout << a[i].f << " ";
    cout << endl;
}
// void _()
// {
//     int n;
//     cin >> n;
//     struct Node
//     {
//         /* data */
//         int x, i, res;
//     };
//     vector a(n + 1);
//     for (int i = 1; i <= n; i++)
//     {
//         cin >> a[i].x;
//         a[i].i = i;
//     }
//     sort(a.begin(), a.end(), [](Node &a, Node &b)
//          { return a.x < b.x; });
//     vector pre(n + 1);
//     for (int i = 1; i <= n; i++)
//         pre[i] = pre[i - 1] + a[i].x;
//     auto ok = [&](int x)
//     {
//         return pre[x - 1] >= a[x].x;
//     };
//     for (int i = 1; i <= n; i++)
//     {
//         int l = i, r = n + 1;
//         while (r - l - 1)
//         {
//             int mid = l + r >> 1;
//             if (ok(mid))
//                 l = mid;
//             else
//                 r = mid;
//         }
//         bug2(i, l);
//         a[i].res = l - 1;
//     }
//     sort(a.begin(), a.end(), [](Node &a, Node &b)
//          { return a.i < b.i; });
//     for (int i = 1; i <= n; i++)
//         cout << a[i].res << " ";
//     cout << endl;
// }

A2.

#include 
#define int long long //
#define endl '\n'     // 交互/调试 关
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(6);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, p, t, l;
    cin >> n >> p >> l >> t;
    auto cal = [](int a, int b)
    {
        return (a + b - 1) / b;
    };
    int has = cal(n, 7);
    int ned = cal(p, t * 2 + l);
    int res = min(has / 2, ned);
    p -= res * (t * 2 + l);
    // bug(p);
    if (p > 0)
    {
        if (has & 1)
            res++, p -= t + l;
    }
    if (p > 0)
        res += cal(p, l);
    res = min(res, n);
    cout
        << n - res << endl;
}
// void _()
// {
//     int n, p, t, l;
//     cin >> n >> p >> l >> t;
//     auto cal = [](int a, int b)
//     {
//         return (a + b - 1) / b;
//     };
//     int has = cal(n, 7);
//     int ned = cal(p, t * 2 + l);
//     int res = min(has, ned);
//     if (ned > has)
//     {
//         p -= has * (t * 2 + l);
//         res += cal(p, l);
//         res = min(n, res);
//     }
//     cout << n - res << endl;
// }

你可能感兴趣的:(日常训练,c++,算法)