Codeforces Round #608 (Div. 2) - D. Portals(贪心)
题意:你起初有一支军队,有$k$个士兵,现在有$n$座城堡,你若想占领第$i$座城堡,至少得有$a[i]$个士兵才能占领$($占领后士兵不会减少$)$,占领了第$i$座城堡后,你将得到$b[i]$个士兵,然后你有两种方式防御你占领的城堡:
- 在你占领第$i$个城堡后留下一个士兵防御第$i$个城堡
- 有$m$个传送门,能从$u_{i}$传送到$v_{i}(u_{i}>v_{i})$,你可以在占领完第$u_{i}$座城堡后再派一个士兵去防御第$v_{i}$个城堡
如果你防御了第$i$座城堡,你能得到$c[i]$的成就值,现在问你,如果你能占领全部的城堡,你能获得的最大的成就值是多少,若不能占领全部的城堡, 输出$-1$。
思路:对于每一座城堡,如果它能在多个地方被防御,那么肯定选择最远能够被防御的地方,所以预处理出$to[i][j]$表示第$to[i][j]$个城堡最远能被防御的地方是第$i$个城堡,根据贪心的思想,我们对于城堡$i$,应该将城堡$i$能够到达的城堡$to[i][j]$全部进行防御,防御时按照成就值从高到低依次防御,当没有士兵进行防御时,如果当前需要防御的城堡的成就值大于你已经防御的城堡里面成就值的最小值,则应该放弃那个成就值最小的城堡,来防御当前的城堡,当发现自己的士兵不够占领某个城堡时,贪心放弃那些成就低的城堡,利用优先队列,每次放弃城堡时取出队头即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector> using namespace std; const int N = ; struct node {
int a, b, c;
}; int n, m, k, pos[N];
vector<int> to[N];
node p[N];
priority_queue< int, vector<int>, greater<int> > q; void init()
{
for (int i = ; i <= n; i++) pos[i] = i;
} bool cmp(int a, int b)
{
return p[a].c > p[b].c;
} int main()
{
scanf("%d%d%d", &n, &m, &k);
init();
for (int i = ; i <= n; i++)
scanf("%d%d%d", &p[i].a, &p[i].b, &p[i].c);
for (int i = ; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
pos[v] = max(pos[v], u);
}
for (int i = ; i <= n; i++) to[pos[i]].push_back(i);
for (int i = ; i <= n; i++) sort(to[i].begin(), to[i].end(), cmp);
int res = , flag = ;
for (int i = ; i <= n; i++) {
if (p[i].a > k) {
int dis = p[i].a - k;
if (dis > q.size()) {
flag = ; break;
}
while (dis--) {
int tp = q.top(); q.pop();
res -= tp, k++;
}
}
k += p[i].b;
for (int j = ; j < (int)to[i].size(); j++) {
int v = to[i][j];
if ( == p[v].c) continue;
if ( == k && !q.empty() && p[v].c > q.top()) {
int tp = q.top(); q.pop();
res -= tp, k++;
}
if (k > ) res += p[v].c, q.push(p[v].c), k--;
}
}
if ( == flag) printf("-1\n");
else printf("%d\n", res);
return ;
}
Codeforces Round #608 (Div. 2) - D. Portals(贪心)的更多相关文章
- Codeforces Round #608 (Div. 2) D. Portals
链接: https://codeforces.com/contest/1271/problem/D 题意: You play a strategic video game (yeah, we ran ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #608 (Div. 2)D(贪心)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],b[],c[]; int u,v; ...
- Codeforces Round #202 (Div. 1) A. Mafia 贪心
A. Mafia Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
- Codeforces Round #192 (Div. 1) A. Purification 贪心
A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...
- Codeforces Round #274 (Div. 1) A. Exams 贪心
A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...
随机推荐
- fdssd
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #in ...
- Func<T,TResult>代理
.NET平台已经发生了很多变化,最近决定好好的系统的学习一下了,开发做了这么多年,老实说很多时候都是在吃老本,这样下去不行的... 今天学习的是Func<T,TResult>,它是新的委托 ...
- 题解 P5594 【【XR-4】模拟赛】
P5594 [[XR-4]模拟赛] 洛谷10月月赛 II & X Round 4 Div.2前两道签到题还是很简单的,基本上是半小时内一遍过两题 看看题解,这题STL做法有用set输出size ...
- webpack 打包增加版本信息
What do we need? 笔者目的是在vue项目打包后的 dist/index.html 文件中写入本次打包git用户.最后一次git提交信息,这样做的目的是便于线上项目的管理和防止同事之间的 ...
- Kubernetes 升级记录:从 1.16.3 升级至 1.17.0
参考官方文档 Upgrading kubeadm clusters 在 ubuntu 18.04 上完成了升级,记录一下升级步骤. 一.升级第一个 master 节点 apt-get 安装 kubea ...
- 1.0 Jmeter 安装运行
1.百度搜索JDK进行下载JDK安装与环境变量配置http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html ---- 在JDK安装完 ...
- Django框架-路由层
Django框架-路由层 一.URL配置(URLconf) 1.基本格式 from django.conf.urls import url urlpatterns = [ url(正则表达式, vie ...
- 概率DP hdu 3366 .
题意:一个人被困在一个城堡里,面前有n条路,他自己有m百万元,选择每一条路都有p概率通过,q概率遇到士兵,1-p-q概率道路不通:遇到士兵的话需要上交1百万,如果不够钱,则被杀死,问的是最优情况下多少 ...
- 用eclipse发布springboot项目
使用eclipse打包springboot项目时一直报错 [ERROR] No compiler is provided in this environment. Perhaps you are ru ...
- Chinese Window Lattice And CSS
谁向云端着此亭,檐前树木映窗棂.-- 释绍嵩<陪赵知府登桃岭山亭> (image from 中国窗棂) The traditional Chinese window lattice has ...