CF702F T-Shirts
题目描述
The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all nn types of t-shirts go on sale. The t-shirt of the ii -th type has two integer parameters — c_{i}ci and q_{i}qi , where c_{i}ci — is the price of the ii -th type t-shirt, q_{i}qi — is the quality of the ii -th type t-shirt. It should be assumed that the unlimited number of t-shirts of each type goes on sale in the shop, but in general the quality is not concerned with the price.
As predicted, kk customers will come to the shop within the next month, the jj -th customer will get ready to spend up to b_{j}bj on buying t-shirts.
All customers have the same strategy. First of all, the customer wants to buy the maximum possible number of the highest quality t-shirts, then to buy the maximum possible number of the highest quality t-shirts from residuary t-shirts and so on. At the same time among several same quality t-shirts the customer will buy one that is cheaper. The customers don't like the same t-shirts, so each customer will not buy more than one t-shirt of one type.
Determine the number of t-shirts which each customer will buy, if they use the described strategy. All customers act independently from each other, and the purchase of one does not affect the purchase of another.
输入输出格式
输入格式:
The first line contains the positive integer nn ( 1<=n<=2·10^{5}1<=n<=2⋅105 ) — the number of t-shirt types.
Each of the following nn lines contains two integers c_{i}ci and q_{i}qi ( 1<=c_{i},q_{i}<=10^{9}1<=ci,qi<=109 ) — the price and the quality of the ii -th type t-shirt.
The next line contains the positive integer kk ( 1<=k<=2·10^{5}1<=k<=2⋅105 ) — the number of the customers.
The next line contains kk positive integers b_{1},b_{2},...,b_{k}b1,b2,...,bk ( 1<=b_{j}<=10^{9}1<=bj<=109 ), where the jj -th number is equal to the sum, which the jj -th customer gets ready to spend on t-shirts.
输出格式:
The first line of the input data should contain the sequence of kk integers, where the ii -th number should be equal to the number of t-shirts, which the ii -th customer will buy.
输入输出样例
3
7 5
3 5
4 3
2
13 14
2 3
2
100 500
50 499
4
50 200 150 100
1 2 2 1
说明
In the first example the first customer will buy the t-shirt of the second type, then the t-shirt of the first type. He will spend 10 and will not be able to buy the t-shirt of the third type because it costs 4, and the customer will owe only 3. The second customer will buy all three t-shirts (at first, the t-shirt of the second type, then the t-shirt of the first type, and then the t-shirt of the third type). He will spend all money on it.
Solution:
本题无旋treap,思路也是ZYYS。
题意就是$n$个物品,每个都有花费$ci$和价值$pi$,然后有$m$个人每个人有$ai$的钱,每件衣服一个人只能买一次,一个人每次会买他所承担的起且没买过的价值最高的衣服,问最后每个人买了几件衣服。
首先我们肯定得确定一个买物品的顺序,既然每次是买价值最高的,那么我们以价值对物品从大到小排(相同价值花费小的排前面),再对每个人的构建平衡树,就可以依次枚举每件物品,在平衡树中查它能被那些人买。
我们用无旋treap写,可以将树按物品的花费$split$成两棵树$x,y$,那么对于权值大于等于花费的$y$树,每个节点的贡献都+1、权值都-花费,我们可以直接懒惰标记。
那么问题是权值减少后,就不能$merge$分离出的两棵树,因为此时不一定满足右边的树权值严格大于左边的树了。
解决办法是对于$y$树,我们再按花费-1来$split$成两棵树$y,z$,新形成的$y$由于权值小于花费,所以得和$x$合并,由于可能存在懒惰标记,于是我们只能暴力中序遍历$y$的每个节点并插入到$x$树中,然后因为$z$树权值还是严格大于等于花费,所以直接$merge$新的$x$和$z$。
暴力重构怎么能AC?现在我们来证明每个节点最多被暴力合并$\log val$次,注意我们重构的标准是对于那些减去花费后权值小于花费的点暴力重构,设原权值为$x$、花费为$y$,那么就有$x-y<y$,即$x<2y$,即每次被重构的点的权值至少减少一半,那么最多就被重构$\log val$次咯。
于是时间复杂度$O(n\log n)$。
代码:
/*Code by 520 -- 9.27*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
int n,m,root,cnt,ch[N][],date[N],num[N],rnd[N],ppx[N],wmz[N];
struct node{
int c,q;
bool operator < (const node &a) const{return q==a.q?c<a.c:q>a.q;}
}a[N]; int gi(){
int a=;char x=getchar();
while(x<''||x>'') x=getchar();
while(x>=''&&x<='') a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void down(int rt){
if(ppx[rt]){
date[ch[rt][]]+=ppx[rt],date[ch[rt][]]+=ppx[rt];
ppx[ch[rt][]]+=ppx[rt],ppx[ch[rt][]]+=ppx[rt];
ppx[rt]=;
}
if(wmz[rt]){
num[ch[rt][]]+=wmz[rt],num[ch[rt][]]+=wmz[rt];
wmz[ch[rt][]]+=wmz[rt],wmz[ch[rt][]]+=wmz[rt];
wmz[rt]=;
}
} int merge(int x,int y){
if(!x||!y) return x+y;
if(rnd[x]<rnd[y]){
down(x);
ch[x][]=merge(ch[x][],y);
return x;
}
else{
down(y);
ch[y][]=merge(x,ch[y][]);
return y;
}
} void split(int rt,int k,int &x,int &y){
if(!rt) {x=y=;return;}
down(rt);
if(date[rt]<k) x=rt,split(ch[rt][],k,ch[rt][],y);
else y=rt,split(ch[rt][],k,x,ch[rt][]);
} il int ins(int a,int b){
int x=,y=;
split(a,date[b],x,y);
a=merge(x,merge(b,y));
return a;
} int dfs(int x,int y){
if(!x) return y;
down(x);
y=dfs(ch[x][],y);
y=dfs(ch[x][],y);
ch[x][]=ch[x][]=;
return ins(y,x);
} void getans(int x){
if(!x) return;
down(x);
getans(ch[x][]),getans(ch[x][]);
} int main(){
n=gi();
For(i,,n) a[i].c=gi(),a[i].q=gi();
sort(a+,a+n+);
m=gi();
For(i,,m) date[i]=gi(),rnd[i]=rand(),root=ins(root,i);
For(i,,n) {
int x=,y=,z=,o=;
split(root,a[i].c,x,y);
date[y]-=a[i].c,ppx[y]-=a[i].c;
num[y]++,wmz[y]++;
split(y,a[i].c-,z,o);
x=dfs(z,x);
root=merge(x,o);
}
getans(root);
For(i,,m) printf("%d ",num[i]);
return ;
}
CF702F T-Shirts的更多相关文章
- CF702F T-Shirts FHQ Treap
题意翻译 题目大意: 有n种T恤,每种有价格ci和品质qi.有m个人要买T恤,第i个人有vi元,每人每次都会买一件能买得起的qi最大的T恤.一个人只能买一种T恤一件,所有人之间都是独立的.问最后每个人 ...
- MySQL基础
数据库操作 ---终端使用数据库 mysql -u root -p 之后回车键 输入密码 ---显示所有数据库: show databases; ---默认数据库: mysql - 用户权限相关数据 ...
- Swift3.0P1 语法指南——构造器
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- [ASP.NET MVC 小牛之路]07 - URL Routing
我们知道在ASP.NET Web Forms中,一个URL请求往往对应一个aspx页面,一个aspx页面就是一个物理文件,它包含对请求的处理. 而在ASP.NET MVC中,一个URL请求是由对应的一 ...
- Mysql操作初级
Mysql操作初级 本节内容 数据库概述 数据库安装 数据库操作 数据表操作 表内容操作 1.数据库概述 数据库管理系统叫做DBMS 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建 ...
- MYSQL(一)
一,概述: 1,什么是数据库: 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库. 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serv ...
- MVC路由配置
目录 URL Routing 的定义方式 示例准备 给片段变量定义默认值 定义静态片段 自定义片段变量 自定义片段变量的定义和取值 将自定义片段变量作为Action方法的参数 指定自定义片段变量为可选 ...
- MySQL使用详解--根据个人学习总结
1.安装配置 2.启动mysql服务并配置 mysql> \s(status也行) 查看当前服务器状态 查看编码状态 Server characterset : utf8 Db characte ...
- Google140道面试题
FQ找来,可能历史比较悠久了,慢慢看. 原文连接:http://www.impactinterview.com/2009/10/140-google-interview-questions/ Goog ...
随机推荐
- wordpress4.4+版本自动生成一个768w像素缩略图的解决办法
4.4版本以后,wordpress增加了响应式图片的功能,目的是让图片能适应手机.平板等不同屏幕,但是我不想要这个功能,把缩略图大小全调成0,function.php里的相关函数全删除了, 上传图片还 ...
- MySQL日志系统:redo log与binlog
日志系统主要有redo log(重做日志)和binlog(归档日志).redo log是InnoDB存储引擎层的日志,binlog是MySQL Server层记录的日志, 两者都是记录了某些操作的日志 ...
- 推荐3个小程序开源组件库——Vant、iView、ColorUI
推荐3个小程序开源组件库 在进行小程序开发时,经常会遇到编写组件方面的阻碍,这让我们花费大量的时间在页面以及 CSS 样式编写上.因此可以使用开源组件库,有些复杂的组件可以直接拿来使用,节省开发时间, ...
- Centos7 zabbix 自动发现与注册
自动发现与自动注册 自动发现: zabbix Server主动发现所有客户端,然后将客户端登记自己的小本上,缺点zabbix server压力山大(网段大,客户端多),时间消耗多. 自动注册: zab ...
- 随手记录-linux-Linux目录结构
转:别人的 装完Linux,首先需要弄清Linux 标准目录结构 / root —?启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home —?存储普通用户的个人文件 ft ...
- centos 切换用户显示bash-4.2$,不显示用户名路径的问题
原文链接: http://blog.csdn.net/testcs_dn/article/details/70482468
- 3、昨天的BUG
基本功能实现了,但是有一些小问题,修改昨天余留的BUG
- Ubuntu16.04下 编译安装 Tensorflow
安装bazel sudo ./bazel***.sh 输入bazel version 检查是否安装. 编译tensorflow 1)./configure 除了选择支持cuda是y,其余的都选择n. ...
- MAX值-单元测试
#include<iostream> using namespace std; int Largest(int list[], int length); // list[]:求最大值的函数 ...
- 使用Axure RP设计Android界面原型
转至@徐州瑞步科技(http://www.cnblogs.com/brooks-dotnet/archive/2013/06/05/3119923.html) 资源地址:http://pan.baid ...