【补题】Educational Codeforces Round 85 (Rated for Div. 2)

Question C is directly greedy, question D Euler circuit, in theory should be able to do ...
Then the relationship should be slower, questions A + B were done for 20 minutes each ... or it is ideal to cut two in 15 minutes After all, a ladder is hung, and the speed of opening the CF is not slow.


C. Circle of Monsters

\ (n \) monsters stand in a circle, the \ (i \) monster has \ (a [i] \) health and \ (b [i] \) explosion damage. When the \ (i \) monster dies, it explodes, and it only deals \ (b [i] \) damage to the \ (i + 1 \) monster . The explosion can be passed. Each shot will cause \ (1 \) damage to one of the monsters , and seek the minimum number of shots that can kill all monsters.

The data range of this question is a bit emmm, I thought to find a way to write \ (O (1) \) , actually it is \ (O (n) \) .

Treat each monster as the first kill target, calculate the number of times to make up the gun (that is, the remaining blood of other monsters after deducting explosion damage), just traverse n, and use set to maintain the minimum value (feeling what new usage get ... ).

Note the special judgment when \ (n == 1 \) .

#include<iostream>
#include<map>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<set>
using namespace std;

const int N = 1e5 + 5;

int main()
{
    int q;
    cin >> q;
    while (q--) {
        int n;
        cin >> n;
        long long int arr[N], pow[N], shrey[N];
        set<long long int> s;
        cin >> arr[0] >> pow[0];
        if (n == 1) {//只有一只怪物,特判
            cout << arr[0] << endl;
            continue;
        }
        long long int sum = 0;//不会被直接炸死的怪物的剩余血量
        for (int i = 1;i < n;i++) {
            cin >> arr[i] >> pow[i];
            shrey[i] = arr[i] - pow[i - 1];//shrey[i]是第i只怪物被炸后的剩余血量
            if (shrey[i] > 0) {//不会被直接炸死
                sum += shrey[i];//剩余血量
            }
        }
        shrey[0] = arr[0] - pow[n - 1];
        if (shrey[0] > 0) sum += shrey[0];
        for (int i = 0;i < n;i++) {
            if (shrey[i] > 0) {
                s.insert(sum - shrey[i] + arr[i]);
                //总残余血量-这只怪物残余血量+这只怪物生命值
                //即将它视为首个目标时需要的子弹数量
            }
            else {
                s.insert(sum + arr[i]);//总残余血量-(0)+生命值
            }
        }
        cout << *s.begin() << endl;//输出最小值
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/streamazure/p/12677497.html