Educational Codeforces Round 76 (Rated for Div. 2) - D. Yet Another Monster Killing Problem(贪心)

题意:有$n$个怪物,每个怪物有一个能力值$a[i]$,你现在有$m$个英雄,每个英雄有两个属性:$p[i]$表示这个英雄的能力值,$s[i]$表示这个英雄的耐力值,即一天内最多能消灭$s[i]$个怪物,每一天你可以选择一个英雄去消灭怪物,并且你只能一个一个的消灭,不能改变顺序,当一个英雄的能力值大于等于怪物的能力值并且他这一天内消灭的怪物数小于$s[i]$时,他就会继续去消灭下一个怪物,直到消灭的怪物数量等于$s[i]$或者这个英雄的能力值小于当前怪物的能力值,结束这一天,进入第二天,一个英雄可以使用多次,问消灭所有怪物最少的天数。

思路:当英雄最大的能力值小于怪物最大的能力值时,不可能把所有怪物消灭,直接输出$-1$,考虑有解时,显然当两个英雄的耐力值$s[i]$相等时,我们会选择能力值大的那个英雄去消灭怪物,所以我们定义$b[i]$表示耐力值至少为$i$(至少能消灭$i$只怪)的英雄中最大的能力值,显然$b[i]$从$n$到$1$是非递减的。设在某一天内已经消灭了$k$只怪物,因为一天只能选择一个英雄,所以在消灭第$k+1$只怪物时,如果这一天内怪物能力的最大值$now$大于$b[k+1]$,则应该结束这一天,第二天又从最大值$b[1]$开始消灭怪物。

注意:$b[i]$数组的求法,从后先前,每次$b[i]=max(b[i],b[i+1])$

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 200010;

int t, n, m, b[N], a[N];

int main()
{
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) b[i] = 0;
        int mmo = -1, mp = -1;
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            mmo = max(mmo, a[i]);
        }
        scanf("%d", &m);
        for (int i = 1; i <= m; i++) {
            int p, s;
            scanf("%d%d", &p, &s);
            mp = max(mp, p);
            b[s] = max(b[s], p);
        }
        for (int i = n - 1; i >= 1; i--) b[i] = max(b[i + 1], b[i]);
        if (mp < mmo) {
            printf("-1\n");
            continue;
        }
        int now = -1, k = 0, ans = 0;
        for (int i = 1; i <= n; i++) {
            now = max(now, a[i]), k++;
            if (b[k] < now) {
                ans++, k = 1, now = a[i];
            }
        }
        printf("%d\n", ans + 1);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zzzzzzy/p/11886348.html

时间: 2024-08-30 12:56:46

Educational Codeforces Round 76 (Rated for Div. 2) - D. Yet Another Monster Killing Problem(贪心)的相关文章

Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest

Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树) 题目链接 题意: 给定3个人互不相同的多个数字,可以把数字移动给别人,问最少移动几次后可以使第一个人的数字为1~m1,第二个人m1~m2,第三个人m2~n(可以没有数字) 题解: 设c[1][i]为第一个人m1为i时需要移动的次数,c[3][i]为m2为i是第三个人需要操作的次数,当其他两个人数字合法时,第二个人的数字也会合法.枚举第一个人的每个i,

Educational Codeforces Round 76 (Rated for Div. 2)

A - Two Rival Students 题意:共n个人排一排,两个人,位于a,b,相邻位置交换至多x次,最大化abs(a-b)的值. 题解:每次交换至多+1,不能超过n-1. #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { #ifdef KisekiPurin freopen("KisekiPurin.in", "r", stdin);

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition (实现,贪心,排序)

C. Multi-Subject Competition time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A multi-subject competition is coming! The competition has m different subjects participants can choose from. That'

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm