Educational Codeforces Round 85 (Div. 2) Part of the solution

A. Level Statistics

Title

A level has the number of player attempts and the number of customs clearance, and gives the data of a player's $ n $ time in chronological order to determine whether these data are reasonable.

Ideas

  • No more customs clearances than attempts
  • The number of attempts at the next moment will not be less than the previous moment
  • The number of customs clearance at the next moment will not be less than the previous moment
  • Each increase in the number of customs clearance will not exceed the increase in the number of attempts

Code

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

void solve() {
    int n; cin >> n;
    int p[n], c[n];
    for (int i = 0; i < n; i++) {
        cin >> p[i] >> c[i];
    }
    for (int i = 0; i < n; i++) {
        if (c[i] > p[i]) {
            cout << "NO" << "\n";
            return;
        }
    }
    for (int i = 1; i < n; i++) {
        if (p[i] < p[i - 1] || c[i] < c[i - 1] || c[i] - c[i - 1] > p[i] - p[i - 1]) {
            cout << "NO" << "\n";
            return;
        }
    }
    cout << "YES" << "\n";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

B. Middle Class

Title

Knowing the personal wealth value of $ n $, you can select some people to collect their wealth values ​​and distribute them equally, and ask how many people have a wealth value of not less than $ x $ at the end.

Ideas

Sort in descending order and distribute the excess wealth value. The number of people before $ x $ cannot be collected for the first time is the answer.

Code

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

void solve() {
    int n, x; cin >> n >> x;
    int a[n]; for (int & i : a) cin >> i;
    sort(a, a + n, greater<int>());
    ll extra = 0, ans = 0;
    for (int i : a) {
        if (i >= x) {
            extra += i - x;
            ++ans;
        } else if (extra >= x - i) {
            extra -= x - i;
            ++ans;
        }
    }
    cout << ans << "\n";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

C. Circle of Monsters

Title

There is a circle of monsters, each monster has $ a_i $ health, every monster shot $ 1 $ times will lose $ 1 $ health, when the monster dies, it will cause $ b_i $ damage to the next monster, if you want to destroy all monsters How many shots are needed at least.

Ideas

First find out how many times you need to shoot, that is $ \ sum_ {i = 0} ^ {n-1} a [i]-b [i-1] $, and then enumerate which monster is the starting point and take the sum The minimum value is sufficient.

Code

#include <bits/stdc++.h>
#define pre(i) ((i - 1 + n) % n)
using ll = long long;
using namespace std;

void solve() {
    int n; cin >> n;
    ll a[n], b[n];
    for (int i =0 ; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    ll sum = 0;
    for (int i = 0; i < n; i++) {
        sum += max(0LL, a[i] - b[pre(i)]);
    }
    ll ans = 1e18;
    for (int i = 0; i < n; i++) {
        if (a[i] > b[i]) {
            ans = min(ans, sum + b[i]);
        } else {
            ans = min(ans, sum + a[i]);
        }
    }
    cout << ans << "\n";    
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t; cin >> t;
    while (t--) solve();
}

 

Guess you like

Origin www.cnblogs.com/Kanoon/p/12677459.html