传送门

最关键的想法就是每个位置一定用的是当前能用的最便宜的水,因为到后面可能有更便宜的

然后其他还没用上的水我们也留着,假装此时已经买了,但是如果发现后面有更优的再反悔也不迟

每相邻两个朋友之间我们把最便宜的一些水消耗了

然后考虑有朋友来送水了

设这个朋友的水的最大体积为 $mx$,价格为 $cst$,如果系统完全装得下 $mx$ 的水那么 $\text{我全都要}$ 即可

如果装不下那么看看系统里最贵的那个单位水 $x$,如果价格大于 $cst$ ,那么我们就不要这个 $x$ 了,直接反悔,问就是根本没买过

(有点像网络流里面的反向边...)

那么价格为 $cst$ 的水就可以多一单位了,然后不断重复直到水的价格都小于等于 $cst$ 或者这 $mx$ 单位的水全部加入到系统里面

实际上代码实现的时候并不需要一单位一单位考虑

到了最后可能系统里还剩下一些水,当然也是假装根本没买过就行了(实际上的确没买过 $2333$)

怎么维护的问题自己开心就好了,这里学的官方题解用 $map$ ($map$ 竟然还能这么用)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=5e5+;
int Q,n,m,Cap,C0;//Cap是容量
struct dat {
int t,mx,cst;//每个朋友到达时间,最大水量,单位价值
dat (int _t=,int _mx=,int _cst=) { t=_t,mx=_mx,cst=_cst; }
inline bool operator < (const dat &tmp) const {
return t<tmp.t;
}
}A[N];
#define fir first
#define sec second
ll solve()
{
ll ans=;
map <int,int> mp;
int now=C0; mp[]=C0;
// now 是当前系统里的水量
for(int i=;i<=n;i++)
{
int dis=A[i].t-A[i-].t;//时间差
// 注意当前处理的时间区间是 [ A[i-1].t , A[i].t ), 左闭右开
while(dis && !mp.empty())//用当前最便宜的水
{
int mx=min( mp.begin()->sec , dis );
mp.begin()->sec -= mx;
dis-=mx; now-=mx;
ans+=1ll*mp.begin()->fir * mx;//用了才计算价钱
if(!mp.begin()->sec)
mp.erase(mp.begin());
}
if(dis) return -;//没水了 int New=min( Cap-now , A[i].mx );//多出的水
now+=New;//加满
while( New<A[i].mx && !mp.empty() && mp.rbegin()->fir > A[i].cst )//考虑替换原本系统里比较贵的水
{
int mx=min( mp.rbegin()->sec , A[i].mx-New );
mp.rbegin()->sec -= mx;
New+=mx;
if(!mp.rbegin()->sec)
mp.erase( --mp.end() );
}
mp[A[i].cst]+=New;
}
return ans;
}
int main()
{
Q=read();
while(Q--)
{
n=read(),m=read(),Cap=read(),C0=read();
for(int i=;i<=n;i++)
A[i].t=read(),A[i].mx=read(),A[i].cst=read();
A[++n]=dat(m,,);//注意细节
sort(A+,A+n+);
printf("%lld\n",solve());
}
return ;
}

Codeforces 1238G. Adilbek and the Watering System的更多相关文章

  1. Codeforces Gym 100286F Problem F. Fibonacci System 数位DP

    Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  2. CodeForces round 967 div2 题解(A~E)

    本来准备比完赛就写题解的, 但是一拖拖了一星期, 唉 最后一题没搞懂怎么做,恳请大神指教 欢迎大家在评论区提问. A Mind the Gap 稳定版题面 https://cn.vjudge.net/ ...

  3. UESTC_Frozen Rose-Heads CDOJ 791

    The winter is coming and all the experts are warning that it will be the coldest one in the last hun ...

  4. Codeforces Round #313 (Div. 2) A. Currency System in Geraldion

    A. Currency System in Geraldion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...

  5. CodeForces 527B Error Correct System

    Error Correct System Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  6. Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力

    C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...

  7. Codeforces Beta Round #4 (Div. 2 Only) C. Registration system hash

    C. Registration system Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  8. Codeforces Round #313 (Div. 2) A. Currency System in Geraldion 水题

    A. Currency System in Geraldion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...

  9. Codeforces Round #298 (Div. 2) E. Berland Local Positioning System 构造

    E. Berland Local Positioning System Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.c ...

随机推荐

  1. java按某个字段对数据分组

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  2. Leetcode题目94.二叉树的中序遍历(中等)

    题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...

  3. rabbitmq权限细分二

    禁止用户远程登录 主要有以下几步 1.进入容器 docker exec -it ID /bin/bash 2.打开配置文件 vi /etc/rabbitmq/rabbitmq.conf 3.加入配置, ...

  4. LeetCode347——优先队列解决查询前k高频率数字问题

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 例如, 给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2]. 注意: 你可以假设给定的 k 总是合理的,1 ≤ k ...

  5. Docker搭建ELK分析tomat日志

    最近公司的项目中用到了ELK,正好有时间自己搭建一个学习一下.在实体机或虚拟机中搭建还需要安装软件,使用docker镜像安装是省时省力的,如下是步骤. 1. 下载elasticsearch镜像: #d ...

  6. leetcode94 不同的二叉搜索树

    solution 1:**动态规划 class Solution { public: int numTrees(int n) { vector<int> g={1,1,2}; for(in ...

  7. 怎么彻底关闭卸载删除Cortana小娜进程,最简单

    原文地址:https://jingyan.baidu.com/article/90bc8fc8be67bcf653640cfa.html Win10中的Cortana是微软开发的一款个人AI助理,集聊 ...

  8. 开机自动挂载ISO文件

    开机自动挂载ISO文件 Table of Contents 1. 概述 1.1. 通过fstab 1.2. 通过rc.local 1 概述 开机自动挂载ISO 文件有两种途径 .一种是通过配置fsta ...

  9. Ubuntu下使用AMD APP编写OpenCL程序

    对于Ubuntu或其近亲(Lubuntu.Kubuntu.Mint等)编写OpenCL程序也不会太难.由于本例用的是AMD APP SDK,因此需要AMD的GPU以及相关驱动.首先,去AMD官网下载G ...

  10. RabbitMQ学习之:(十)AMQP和RabbitMQ介绍 (转贴+我的评论)

    From: http://www.infoq.com/cn/articles/AMQP-RabbitMQ 准备开始 高级消息队列协议(AMQP1)是一个异步消息传递所使用的应用层协议规范.作为线路层协 ...