题意:你起初有一支军队,有$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(贪心)的更多相关文章

  1. Codeforces Round #608 (Div. 2) D. Portals

    链接: https://codeforces.com/contest/1271/problem/D 题意: You play a strategic video game (yeah, we ran ...

  2. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  3. Codeforces Round #608 (Div. 2)D(贪心)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],b[],c[]; int u,v; ...

  4. 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 ...

  5. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  6. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  7. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  8. Codeforces Round #192 (Div. 1) A. Purification 贪心

    A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...

  9. 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 ...

随机推荐

  1. python+selenium:浏览器webdriver操作(0)

    1.浏览器最大化 启动的浏览器不是全屏的,这样不会影响脚本的执行,但是有时候会影响我们“观看”脚本的执行. #coding=utf-8 from selenium import webdriver i ...

  2. STA之RC Corner再论

    Q:RC-Corner跟PVT怎么组合? A:通常的组合:   Q:通常说的ttcorner指的是啥? A:@孟时光 ttcorner是指管子在tt+RCtyp吧. Typesof corners W ...

  3. C++四种初始化方式

    1. 直接初始化直接调用与实参匹配的构造函数,形式如“T t(u)”.2. 拷贝初始化:复制初始化首先使用指定构造函数创建一个临时对象,然后用复制构造函数将那个临时对象复制到正在创建的对象”,形式如“ ...

  4. split()函数+merge()函数

    在图像处理时,我们接触到的彩色以RGB居多,为了分析图像在某一通道上的特性,需要将图像的颜色通道进行分离,或者是在对某一颜色通道处理后重新进行融合.opencv提供了split()函数来进行颜色通道的 ...

  5. 7_4 素数环(UVa524)<回溯法和生成-测试法的比较>

    有一个环(ring)是由n个圈圈所组成的(在这里n一定是个偶数),我们想要把1到n的自然数各放到一个圈圈中,使得相邻2个圈圈中的数的和一定是素数.下图为n=6的情形.请注意:第1个圈圈中的数一定是1. ...

  6. expdp定时备份

    1.创建用户所需的永久表空间和临时表空间 create tablespace tbs_hankey_dat datafile '/opt/oracle/oradata/tbs_hankey.dbf' ...

  7. oracle11g UNDO使用率高,且unexpire过高

    1.查看使用率: col USED_PER for a60 SELECT A.TABLESPACE_NAME AS TABLESPACE_NAME, ,) AS TOTAL_GB, ,) AS FRE ...

  8. Linux netstat命令详解(检验本机各端口的网络连接情况)

    netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP ...

  9. Java - Test - TestNG: testng.xml 元素 class

    Java - Test - TestNG: testng.xml 元素 class 1. 概述 class 相关的元素 classes class methods exclude include 2. ...

  10. matplotlib动态图subplots()和subplot()不同及参数

    一.fig,ax = subplots(nrows,ncols,sharex,sharey,squeeze,subplot_kw,gridspec_kw,**fig_kw)  创建画布和子图 nrow ...