QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#103308#6130. Plants vs. Zombiesevirir#RE 2ms3376kbC++202.2kb2023-05-05 07:09:202023-05-05 07:09:21

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-05 07:09:21]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:3376kb
  • [2023-05-05 07:09:20]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define forn(i,a,b) for(int i = a; i < (b); i++)
#define fore(i,a,b) for(int i = a; i <= (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ull = unsigned long long;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;

void water_ij_until_i_near_target(ull target, const vector<ull> &a, vector<ull> &d, size_t i, size_t j, int &m) {
    if (d[i] >= target) return;
    ull waters = (target - d[i] - 1) / a[i];
    if (waters == 0) return;
    // cout << "watered " << i << " and " << j << ", took " << (waters * 2) << " waters" << endl;
    d[i] += waters * a[i];
    d[j] += waters * a[j];
    m -= 2 * waters;
}

bool canReach(ull target, const vector<ull> &a, int n, int m) {
    // cout << "target: " << target << endl;

    vector<ull> d = a;
    m -= n;

    forn(i, 0, n - 1) {
        water_ij_until_i_near_target(target, a, d, i, i+1, m);
        if (m < 0) return false;
    }

    water_ij_until_i_near_target(target, a, d, n - 1, n - 2, m);

    int cur_run = 0;
    forn(i, 0, n) {
        if (d[i] < target) {
            cur_run++;
        } else if (cur_run) {
            int waters = 0;
            if (i == n - 1) {
                waters = cur_run;
            } else {
                waters = (cur_run + 1) / 2 * 2;
            }
            m -= waters;
            // cout << "run from " << (i - cur_run) << " to " << (i - 1) << " took " << waters << endl;
            cur_run = 0;
        }
    }

    if (cur_run != 0) {
        m -= (cur_run + 1) / 2 * 2;
    }

    return m >= 0;
}

void solve()
{
    int n, m;
    cin >> n >> m;

    if (m < n) {
        cout << 0 << endl;
        return;
    }

    vector<ull> a(n, 0);
    forn(i,0,n) cin >> a[i];

    ull left = 0;
    ull right = (*max_element(all(a)) + n - 1) / n * m;
    while (left < right) {
        ull mid = left + (right - left + 1) / 2;
        if (canReach(mid, a, n, m)) {
            left = mid;
        } else {
            right = mid - 1;
        }
    }
    cout << right << endl;
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);

    int t; cin>>t;
    forn(i,0,t) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3376kb

input:

2
4 8
3 2 6 6
3 9
10 10 1

output:

6
4

result:

ok 2 number(s): "6 4"

Test #2:

score: -100
Dangerous Syscalls

input:

116
4 0
3 2 6 6
4 1
3 2 6 6
10 19
10 2 8 4 2 4 9 3 3 3
4 8
3 9 3 6
2 19
2 10
11 15
3 1 1 4 3 7 10 8 6 7 10
10 14
8 7 1 1 10 9 2 8 10 7
2 13
2 3
10 10
8 1 6 6 9 4 7 1 8 8
7 14
6 7 4 5 3 1 3
11 6
8 1 10 9 7 2 6 6 1 3 9
4 10
6 1 3 8
7 7
10 6 2 10 4 7 2
5 11
9 10 5 9 2
9 1
2 4 8 6 2 8 8 1 6
4 5
7 2 9 8
...

output:

0
0
1
4
6
20
3
2
14
1
4
0
0
0
0
1

result: