Codeforces 1257D - Yet Another Monster Killing Problem
题意:
有\(n\)个怪物,每个怪物有攻击力\(a_i\)点;有\(m\)个英雄,每个英雄有攻击力\(p_i\)点,耐力\(s_{i}\)点。
怪物需要被依次杀死(按输入顺序)。
每一天可以挑选一个英雄去杀怪物,他可以杀死的怪物攻击力小于等于他本身(即\(a\leq p\)),每天最多可以杀死\(s\)个怪物。(每个英雄可以使用任意次)
问最少需要多少天可以杀死所有怪物(不能则输出\(-1\))。
分析:
\((1)\)我们找到怪物的最大攻击力和英雄的最大攻击力,判断是否要输出\(-1\)。
\((2)\)将英雄按攻击力\(p\)值排序,我们可以发现对于英雄\(b[i]\)而言,如果对于\(i<j\leq m\),且有\(b[i].s<b[j].s\),我们可以选择英雄\(j\),而不是英雄\(i\),那么我们可以把\(b[i].s\)替换为\(b[j].s\)(意思为选择英雄\(i\)时选择英雄\(j\))。
\((3)\)因此我们进行后缀操作将\(b[i].s\)改为英雄\(i\)~\(n\)中最大的耐力值,以便进行下一步。
\((4)\)对于某个怪物而言,我们可以找到一个英雄,他的攻击力刚好大于等于该怪物(二分)。我们上一步将该英雄的耐力改为了后缀最大值,那么我们便选择这个英雄。
\((5)\)我们从第一天开始,枚举每一个怪物,找到当前天我们可以杀死最多怪物的英雄,如果对于某个怪物而言,杀死他的人的耐力(我们进行了后缀操作)不足以支撑该天,我们将该怪物放到下一天,并重复操作,直至杀死所有怪物。因此我们需要保存的量有:当前的天数\(k\),昨天杀死的最后一只怪物的编号\(last\),今天所能杀死的最多怪物数(表现为所需要的最小耐力)\(minn\)。
#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define LL long long
using namespace std;
const int maxn = (ll) 2e5 + 5;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
struct node {
int p, s;
bool operator<(const node &b) {//用做排序
return p < b.p;
}
} b[maxn];
bool cmp(const node &x, int y) {//用做二分
return x.p < y;
}
int a[maxn];
int main() {
start;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
int maxa = 0, maxs = 0;//用做判-1
for (int i = 1; i <= n; ++i) {
cin >> a[i];
maxa = max(maxa, a[i]);
}
int m;
cin >> m;
for (int i = 1; i <= m; ++i) {
cin >> b[i].p >> b[i].s;
maxs = max(maxs, b[i].p);
}
if (maxa > maxs) {
cout << -1 << '\n';
continue;
}
sort(b + 1, b + m + 1);
for (int i = m - 1; i >= 1; --i)//后缀操作
b[i].s = max(b[i].s, b[i + 1].s);
int k = 1;
int last = 0;
int minn = inf;
for (int i = 1; i <= n; ++i) {
int t = lower_bound(b + 1, b + m + 1, a[i], cmp) - b;//刚好能杀死该怪物的英雄编号
minn = min(b[t].s, minn);//今天所需要的最小耐力
if (minn + last < i) {//将这只怪物放到明天杀
minn = b[t].s;
++k;
last = i - 1;
}
}
cout << k << '\n';
}
return 0;
}
本场比赛\(D\)和\(E\)惨痛教训:玩后缀一定要注意边界!!!
Codeforces 1257D - Yet Another Monster Killing Problem的更多相关文章
- Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem 贪心
D. Yet Another Monster Killing Problem You play a computer game. In this game, you lead a party of
- Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem
You play a computer game. In this game, you lead a party of mm heroes, and you have to clear a dunge ...
- D - Yet Another Monster Killing Problem
题目连接: https://codeforces.com/contest/1257/problem/D 题目大意: n个怪兽,m个英雄,每个怪兽有一定的能力值,每个英雄有一定的能力值和一定的耐力值.耐 ...
- 【CF1257D】Yet Another Monster Killing Problem【贪心】
题意:给定一些怪物,每天可以选一个勇士进去打怪,每个勇士每天只能打不超过si个怪物,每个勇士只能打能力值≤pi的怪物,问最少多少天打完所有怪物 题解:贪心,每天尽可能多的去打怪,那么存一个对于长度为i ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Codeforces Beta Round #17 A - Noldbach problem 暴力
A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...
- codeforces D. Pashmak and Parmida's problem
http://codeforces.com/contest/459/problem/D 题意:给你n个数,然后统计多少组(i,j)使得f(1,i,ai)>f(j,n,aj); 思路:先从左往右统 ...
- CodeForces 459D Pashmak and Parmida's problem
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces Beta Round #2 C. Commentator problem 模拟退火
C. Commentator problem 题目连接: http://www.codeforces.com/contest/2/problem/C Description The Olympic G ...
- Codeforces 954I Yet Another String Matching Problem(并查集 + FFT)
题目链接 Educational Codeforces Round 40 Problem I 题意 定义两个长度相等的字符串之间的距离为: 把两个字符串中所有同一种字符变成另外一种,使得两个 ...
随机推荐
- 使用ptrace将标准输出重定位到文件
首先使用PTRACE_SYSCALL获取到系统调用号,如果是write则将文件描述符从标准输出变为我们打开的文件 #include <stdio.h> #include <fcntl ...
- 案例分享-被*队友的mybatis蠢哭的一天
昨晚加班的时候被队友拉着看一个mybatis的问题,耗费了我一个小时时间,最后差点没被我打死,实在是觉得滑稽,今天回家写下来跟大伙分享一下. 问题现象 Invalid bound statement ...
- Python潮流周刊#6:Python 3.12 有我贡献的代码!
你好,我是猫哥.这里记录每周值得分享的 Python 及通用技术内容,部分为英文,已在小标题注明.(标题取自其中一则分享,不代表全部内容都是该主题,特此声明.) 首发于我的博客,https://pyt ...
- List转为Map
List转为Map 1.业务需求,需要将List<SysSetting>转为Map SysSetting是一个对象 @Data @TableName("t_sys_setting ...
- #PowerBi Superchange PowerBi 序言部分笔记(2)
Xmind本文思维导图 序言部分,主要讲述了BI的分类及发展,以及作者推荐的学习方法.重点是介绍了powerbi的主要四大步骤. 即: 一:数据采集 Data acquisition: Power B ...
- 4大数据实战系列-hive安装配置优化
1 基础环境 1.1 版本预览 Cnetos 6.5 已安装 Hadoop 2.8 已安装集群 Hive 2.3 待安装 Mysql 5.6 已安装 Spark 2.1.1 已安装 1.2 机器环境 ...
- Rust 过程宏 proc-macro 是个啥
定义一个 procedural macro 新建一个 lib 类型的 crate: cargo new hello-macro --lib procedural macros 只能在 proc-mac ...
- vscode中react组件
通过使用这个插件我们可以很方便的进行组件/方法/文件的导入 本篇博客仅对插件进行介绍翻译,便于自己以后使用 常用片段列表 imr: 引入 React import React from 'react' ...
- CentOS 8搭建Docker镜像私有仓库-registry
目录 简介 安装Docker 添加docker yum源 安装docker 确保网络模块开机自动加载 使桥接流量对iptables可见 配置docker 验证docker是否正常 添加用户到docke ...
- java解析CSV文件(zipFiles 打成压缩包 exportObeEventDataExcel 前端页面响应)
JAR包及代码17:39:09 <!-- https://mvnrepository.com/artifact/com.opencsv/opencsv --> <dependency ...