传送门

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

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

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

然后考虑有朋友来送水了

设这个朋友的水的最大体积为 $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. 2018全国大学信息安全竞赛wp(2misc+2crypto+1web)

    WEB: 0x00 easyweb admin用户是没有密码的,但是当密码为空时,网页会自动填写.用hackbar构造空密码post,服务器返回cookies,浏览器保存,刷新获得flag CRYPT ...

  2. 用PHP自带函数对二维数组进行排序

    经常会面临这样的需求,虽然有时候我们可以在数据库查询的时候,直接对数据进行排序,但还是无法满足日益复杂的业务需求. 这里边会用到两个函数 一个是array_column()函数,这个函数接受三个参数. ...

  3. 如何配置git send-email相关的邮箱信息?

    关键是配置smtpserver,请参考此处

  4. jQuery显示隐藏div的几种方法

    1.$("#demo").attr("style","display:none;");//隐藏div $("#demo" ...

  5. C#可以直接调用的Win32API

    以前整理的Win32 API,可以直接在C#中直接调用,在做WinForm时还是很有帮助的.以前用在一个多窗口界面中,当轮询窗口时,调用API会提高很多效率. 源码下载 http://files.cn ...

  6. 九十四:CMS系统之cms后台登录限制

    装饰器,验证当前session中是否存在定义的user_id,没有就重定向到登录页 from flask import session, redirect, url_forfrom functools ...

  7. [CDH] Acquire data: Flume and Kafka

    Flume 基本概念 一.是什么 Ref: http://flume.apache.org/ 数据源获取:Flume.Google Refine.Needlebase.ScraperWiki.Bloo ...

  8. Linux安装配置JDK1.8

    JDK1.8 链接:http://pan.baidu.com/s/1nvGBzdR 密码:ziqb 1  在/usr/local   文件夹下新建一个文件夹software ,将JDK放到此文件夹中 ...

  9. mysql RPM 包比较全的网站

    https://pkgs.org/download/mysql-devel https://pkgs.org/download/mysql-server https://pkgs.org/downlo ...

  10. Linux0.11学习

    Linux 0.11虽然不是什么“珠穆朗玛峰”,但它肯定还是“华山”或“泰山”.虽然有路但你还是需要最基本的努力和花费一定的代价才能“攀登”上去.1. PC兼容机硬件工作原理(比如8259A,8253 ...