传送门

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

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

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

然后考虑有朋友来送水了

设这个朋友的水的最大体积为 $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. 0ctf-Wallbreaker Easy复现

    补坑+1. 有预留的后门,并且给了phpinfo,因此可以从phpinfo中先搜集一波信息: 这里禁用了很多命令执行的函数,所以应该要bypass_disablefunction,先读一下flag在哪 ...

  2. 整合spring boot时操作数据库时报错Caused by: java.lang.InstantiationException: tk.mybatis.mapper.provider.base.B

    原文:https://blog.csdn.net/u__f_o/article/details/82756701 一般出现这种情况,应该是没有扫描到对应的mapper包,即在启动类下配置MapperS ...

  3. Tomcat安装应用部署及配置文件解读

    Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. Tomcat和Nginx,APa ...

  4. [Java复习] 多线程 Multithreading

    Q1多线程基础 进程和线程? 进程: 1. 一段程序执行过程,动态的,相对而言程序是静态的.(与类和对象的关系类似) 2. CPU资源分配最小单元,包括CPU调度和资源管理. 3. 一个进程可以有多个 ...

  5. Centos7下使用Ceph-deploy快速部署Ceph分布式存储-操作记录(转)

    之前已详细介绍了Ceph分布式存储基础知识,下面简单记录下Centos7使用Ceph-deploy快速部署Ceph环境:1)基本环境 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  6. xshell一些简单的使用---1

    1.下载本地window中 2.在sftp模式下输入:put会打开xshell下载到window的位置目录

  7. oracle数据ORA-03113:通信通道的文件到达结尾的简单处理方式

    <ORA-03113:通信通道的文件结尾>错误处理: 出现的主要原因是由于归档日志空间不够了. 解决办法: --以sysdba方式登录 sqlplus / as sysdba --关闭数据 ...

  8. es6 实现单链表

    第一种/** * 链表节点类 */ class Node { constructor(ele) { this.ele = ele; this.next = null; } } /** * 链表类 */ ...

  9. 【转】MAC 配置ssh免密登录

    配了挺多次免密登录,老是忘了,备份一下 转载自知乎,如侵删.https://zhuanlan.zhihu.com/p/32279976 -------------------------------- ...

  10. Map构造器模式 map builder pattern

    maven引入依赖 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava ...