链接:

https://codeforces.com/contest/1271/problem/D

题意:

You play a strategic video game (yeah, we ran out of good problem legends). In this game you control a large army, and your goal is to conquer n castles of your opponent.

Let's describe the game process in detail. Initially you control an army of k warriors. Your enemy controls n castles; to conquer the i-th castle, you need at least ai warriors (you are so good at this game that you don't lose any warriors while taking over a castle, so your army stays the same after the fight). After you take control over a castle, you recruit new warriors into your army — formally, after you capture the i-th castle, bi warriors join your army. Furthermore, after capturing a castle (or later) you can defend it: if you leave at least one warrior in a castle, this castle is considered defended. Each castle has an importance parameter ci, and your total score is the sum of importance values over all defended castles. There are two ways to defend a castle:

if you are currently in the castle i, you may leave one warrior to defend castle i;

there are m one-way portals connecting the castles. Each portal is characterised by two numbers of castles u and v (for each portal holds u>v). A portal can be used as follows: if you are currently in the castle u, you may send one warrior to defend castle v.

Obviously, when you order your warrior to defend some castle, he leaves your army.

You capture the castles in fixed order: you have to capture the first one, then the second one, and so on. After you capture the castle i (but only before capturing castle i+1) you may recruit new warriors from castle i, leave a warrior to defend castle i, and use any number of portals leading from castle i to other castles having smaller numbers. As soon as you capture the next castle, these actions for castle i won't be available to you.

If, during some moment in the game, you don't have enough warriors to capture the next castle, you lose. Your goal is to maximize the sum of importance values over all defended castles (note that you may hire new warriors in the last castle, defend it and use portals leading from it even after you capture it — your score will be calculated afterwards).

Can you determine an optimal strategy of capturing and defending the castles?

思路:

计算到每个城堡最少多少人,计算出每个位置可以派出去多少人。

记录每个点最右边的坐标,因为左边的人可以带到右边去用,所以往右边放是较优的。

这样就可以用一个堆来维护。

代码:

#include<bits/stdc++.h>
using namespace std; const int MAXN = 5e3+10; struct Node
{
int v, i;
bool operator < (const Node & rhs) const
{
return this->v < rhs.v;
}
}; priority_queue<Node> que;
int a[MAXN], b[MAXN], c[MAXN];
int need[MAXN], las[MAXN], use[MAXN];
int n, m, k; int main()
{
cin >> n >> m >> k;
for (int i = 1;i <= n;i++)
cin >> a[i] >> b[i] >> c[i], las[i] = i;
int u, v;
for (int i = 1;i <= m;i++)
{
cin >> u >> v;
las[v] = max(las[v], u);
}
need[n] = a[n];
for (int i = n-1;i >= 1;i--)
need[i] = max(a[i], need[i+1]-b[i]);
int sum = k;
for (int i = 1;i <= n;i++)
{
if (sum < a[i])
{
puts("-1");
return 0;
}
sum += b[i];
}
sum = k;
for (int i = 1;i <= n;i++)
{
use[i] = sum+b[i]-need[i+1];
sum = need[i+1];
que.push(Node{c[i], i});
}
int ans = 0;
while(!que.empty())
{
int val = que.top().v;
int x = que.top().i;
que.pop();
int y = las[x];
while(use[y] == 0 && y > 0)
y--;
if (y == 0)
continue;
use[y]--;
ans += val;
}
cout << ans << endl; return 0;
}

Codeforces Round #608 (Div. 2) D. Portals的更多相关文章

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

    题意:你起初有一支军队,有$k$个士兵,现在有$n$座城堡,你若想占领第$i$座城堡,至少得有$a[i]$个士兵才能占领$($占领后士兵不会减少$)$,占领了第$i$座城堡后,你将得到$b[i]$个士 ...

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

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

  3. Codeforces Round #608 (Div. 2)

    传送门 A. Suits 签到. Code /* * Author: heyuhhh * Created Time: 2019/12/15 17:16:33 */ #include <iostr ...

  4. 【cf补题记录】Codeforces Round #608 (Div. 2)

    比赛传送门 再次改下写博客的格式,以锻炼自己码字能力 A. Suits 题意:有四种材料,第一套西装需要 \(a\).\(d\) 各一件,卖 \(e\) 块:第二套西装需要 \(b\).\(c\).\ ...

  5. Codeforces Round #608 (Div. 2) E. Common Number

    链接: https://codeforces.com/contest/1271/problem/E 题意: At first, let's define function f(x) as follow ...

  6. Codeforces Round #608 (Div. 2) E - Common Number (二分 思维 树结构)

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

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

  8. Codeforces Round #608 (Div. 2) E. Common Number (二分,构造)

    题意:对于一个数\(x\),有函数\(f(x)\),如果它是偶数,则\(x/=2\),否则\(x-=1\),不断重复这个过程,直到\(x-1\),我们记\(x\)到\(1\)的这个过程为\(path( ...

  9. Codeforces Round #564 (Div. 1)

    Codeforces Round #564 (Div. 1) A Nauuo and Cards 首先如果牌库中最后的牌是\(1,2,\cdots, k\),那么就模拟一下能不能每次打出第\(k+i\ ...

随机推荐

  1. Angular中引入外部js的使用方式

    在Angular中我们或许会用到部分外部插件的时候,像Bootstrap,Jquery这些当然我们可以通过Npm安装包的形式引入,但是还有一些其它的js库需要引入的话,我们又应该怎样操作呢? 在这里做 ...

  2. 基于硬件的消息队列中间件 Solace 简介之二

    前言...... 前面简单介绍了Solace来自于哪家公司, 主要能做哪些事情. 本篇主要进一步介绍Solace作为消息传递的中间件如何工作的. 传统意义上来讲, 每当我们谈到消息中间件时, 首先想到 ...

  3. nginx集群+mysql数据同步

    mysql集群配置在网站负载均衡中是必不可少的: 首先说下我个人准备的负载均衡方式: 1.通过nginx方向代理来将服务器压力分散到各个服务器上: 2.每个服务器中代码逻辑一样: 3.通过使用redi ...

  4. hadoop3.0.0部署

    配置前先查下JAVA_HOME的位置vim /etc/profile#set java environment JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1. ...

  5. [转帖]DotNetCore跨平台~System.DrawingCore部署Linux需要注意的

    DotNetCore跨平台~System.DrawingCore部署Linux需要注意的   https://www.bbsmax.com/A/QV5ZemYVJy/?tdsourcetag=s_pc ...

  6. 查找担保圈-step4-提取s1中担保圈路径中的成员

    USE [test] GO /****** Object: StoredProcedure [dbo].[p02_get_group_member] Script Date: 2019/7/8 14: ...

  7. SQL之事务

    ●事务的ACID(acid)属性 ➢1.原子性(Atomicity ) 原子性是指事务是-一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. ➢2. 一致性(Consistency) 事务 ...

  8. liunx 安装rsync

    新建一个rsync.s文件,把下面的代码写入文件里: #!/usr/bin/env bash mkdir -p /data/app/rsync/etc/ mkdir -p /data/logs/rsy ...

  9. python进程之间的通信——Queue

    我们知道进程之间的数据是互不影响的,但有时我们需要在进程之间通信,那怎么办呢? 认识Queue 可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Queue本身是一个消息 ...

  10. 通过Playbook部署LAMP

    Ansible的PlayBook文件格式为YAML语言,所以希望你在编写PlayBook前对YAML语法有一定的了解,否则在运行PlayBook的时候经常碰到语法错误提示,这里我们通过介绍批量部署LA ...