传送门

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

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

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

然后考虑有朋友来送水了

设这个朋友的水的最大体积为 $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. Failed to configure a DataSource 'url' attribute问题解决

    才写了一行代码又报错了.. *************************** APPLICATION FAILED TO START *************************** De ...

  2. C# Read/Write another Process' Memory

    https://codingvision.net/security/c-read-write-another-process-memory Today’s tutorial is about…proc ...

  3. 【log4j】log4j.properties 文件示例

    # 下面的文件内容是写程序长期要用的,放在这里留个底#Output information(higher than INFO) to stdout and file.info/debug/error ...

  4. backbone之collection

    最近要用到backbone.js,网上也找了些资料,但是就看到一个开头还可以,往下看基本就看不下去了,幸好有这本书[LeanpubRead] Backbone.Marionette.js A Gent ...

  5. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-3.Vidoe相关接口完善和规范协议

    笔记 3 .Vidoe相关接口完善和规范协议     简介:完善相关接口,协议规范讲解 1.save接口保存对象             1)@RequestParam(value = "p ...

  6. kafka入门学习---1 启动kakfa

    1.查看kafka生产者产生的数据 kafka-console-consumer.sh --zookeeper hadoop-:,hadoop-:,hadoop-: -topic kafkademo ...

  7. Fiddler抓取https设置详解(图文)

    本文主要说明了自己在设置fiddler抓取https过程中所遇到的问题及解决步骤,特别是fiddler在设置证书的环节遇到的各种奇葩问题,特此分享! 声明:本文为原创文章,转载请注明来源:https: ...

  8. 搭建Kubernetes容器集群管理系统

    1.Kubernetes 概述 Kubernetes 是 Google 开源的容器集群管理系统,基于 Docker 构建一个容器的调度服务,提供资源调度.均衡容灾.服务注册.劢态扩缩容等功能套件. 基 ...

  9. LVS系列二、LVS集群-DR模式

    一. LVS-DR和LVS-IP TUN集群概述 1.  Direct Routing(直接路由) Director分配请求到不同的real server.real server处理请求后直接回应给用 ...

  10. 启动nfs清除端口占用过程

    centos7起nfs服务. 按教程执行: vim /etc/exportsyum install -y nfs-utils systemctl enable rpcbind.service syst ...