【BZOJ4843】[Neerc2016]Expect to Wait 排序
【BZOJ4843】[Neerc2016]Expect to Wait
Description
Input
Output
Sample Input
- 1 1
- 2 2
+ 4 1
- 6 1
+ 7 2
0 3 1 2
Sample Output
0
8
3
题解:将借书看成+x,换书看成-x,然后在时间轴上求前缀和,发现那些权值>0的线段的加权长度即时总等待时间。那么如果初始有y本书呢?则我们只需要计算所有权值>y的线段的加权长度。将所有线段排个序即可。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=100010;
int n,m,tot;
ll sum,sv,sl;
char str[10];
ll ans[maxn],t[maxn];
struct node
{
ll val,len;
}p[maxn];
struct query
{
int org;
ll val;
}q[maxn];
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
bool cmp1(node a,node b)
{
return a.val>b.val;
}
bool cmp2(query a,query b)
{
return a.val>b.val;
}
int main()
{
n=rd(),m=rd();
int i,j,a;
for(i=1;i<=n;i++)
{
scanf("%s",str),t[i]=rd(),a=rd();
if(sum>0) p[++tot].val=sum,p[tot].len=t[i]-t[i-1];
if(str[0]=='+') a=-a;
sum+=a;
}
for(i=1;i<=m;i++) q[i].val=rd(),q[i].org=i;
sort(p+1,p+tot+1,cmp1);
sort(q+1,q+m+1,cmp2);
for(i=j=1;i<=m;i++)
{
for(;p[j].val>q[i].val&&j<=tot;j++) sv+=p[j].val*p[j].len,sl+=p[j].len;
if(q[i].val<sum) ans[q[i].org]=-1;
else ans[q[i].org]=sv-sl*q[i].val;
}
for(i=1;i<=m;i++)
{
if(ans[i]==-1) printf("INFINITY\n");
else printf("%lld\n",ans[i]);
}
return 0;
}
【BZOJ4843】[Neerc2016]Expect to Wait 排序的更多相关文章
- bzoj4843 [Neerc2016]Expect to Wait
[Neerc2016]Expect to Wait Time Limit: 10 Sec Memory Limit: 128 MB Description ls最近开了一家图书馆,大家听说是ls开的, ...
- 「Neerc2016」Expect to Wait
题目描述 ls最近开了一家图书馆,大家听说是ls开的,纷纷过来借书,自然就会出现供不应求的情况, 并且借书的过程类 似一个队列,每次有人来借书就将它加至队尾,每次有人来还书就把书借给队头的若干个人,定 ...
- Perl小知识点之排序sort
脚本这种东西,就是要常用,否则一段时间不用就生疏了,因此决定时时记一些小知识点,一来回顾一下,二来需要的时候可以迅速获得提示. Sort by number You could now write a ...
- PHP 使用用户自定义的比较函数对数组中的值进行排序
原文:PHP 使用用户自定义的比较函数对数组中的值进行排序 usort (PHP 4, PHP 5) usort — 使用用户自定义的比较函数对数组中的值进行排序 说明 bool ...
- April Fools Contest 2017 题解&源码(A,数学 B,数学 C,数学 D,字符串 E,数字逻辑 F,排序,卡时间,G,数学)
A. Numbers Joke time limit per test:2 seconds memory limit per test:64 megabytes input:standard inpu ...
- [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- 使用Spring JPA中Page、Pageable接口和Sort类完成分页排序
显示时,有三个参数,前两个必填,第几页,一页多少个size,第三个参数默认可以不填. 但是发现这个方法已经过时了,通过查看它的源码发现,新方法为静态方法PageRequest of(page,size ...
- 剑指offer——面试题25:合并两个 排序的链表
自己答案: ListNode* MergeTwoSortedList(ListNode* pHead1,ListNode* pHead2) { if(pHead1==nullptr&& ...
- Expect使用小记
By francis_hao May 31,2017 本文翻译了部分Expect的man手册,只选取了个人常用的功能,因此并不完善. Expect是一个可以和交互式程序对话的程序 概述 ...
随机推荐
- 二叉树遍历 Morris
二叉树的遍历,先根遍历,不适用递归,存储空间为 O(1) 转自:http://chuansongme.com/n/100461 MorrisInOrder(): while 没有结束 如果当前节点没有 ...
- Hotspot JVM下,parallel与concurrent的区别
转载于知乎 作者:Ted Mosby链接:https://www.zhihu.com/question/21535747/answer/144884632来源:知乎著作权归作者所有.商业转载请联系作者 ...
- (10)centos搭建web服务器 (Nginx+ django)
安装 python3 sudo yum install python34 安装uWSGI pip install uwsgi 安装 Nginx http://nginx.org/en/download ...
- Codeforces 746G(构造)
G. ...
- VisualStudio 2013 Prieview体验
今天看到VisualStudio 2013的预览版发布了,便立即下载试用了一下. 主体界面和VS2012非常类似,不过色彩要稍微丰富点. 现在支持用MS账户登录了,登陆后可以同步设置,这个小功能还是比 ...
- smartsvn学习(三) Error validating server certificate for
Error validating server certificate for 'xxxxxxxxxxxx:443': - The certificate is not issued by a tr ...
- Failed to fetch URL http://dl-ssl.google.com/android/repository/repository.xml, reason:
Failed to fetch URL http://dl-ssl.google.com/android/repository/repository.xml, reason: Connection t ...
- MFC中 用Static控件做超链接(可以实现变手形、下划线、字体变色等功能)
1.新建一个基于对话框的工程MyLink,在对话框中拖一个Static控件,ID可改为IDC_SLink. 2.在头文件中添加成员变量: private: CRect m_Rect; CFont* m ...
- cocos2d-x 学习记录
不积跬步,无以至千里.不积小流,无以成江海. 開始学习cocos2d-x ,路漫漫其修远兮.加油!
- 工程管理,用网页就够了!——Wish3D Earth在线三维地球强势上线
大型工程涉及到众多的施工队.管理单位和相关部门,相互之间需要传递的数据.文件的数量是惊人的,必须建立起有效的信息管理方法,使管理者及时把握工程的信息,全面准确地控制工程施工情况. 现代化的建筑工程管理 ...