Codeforces Educational Codeforces Round 15 F. T-Shirts

 

F. T-Shirts
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all n types of t-shirts go on sale. The t-shirt of the i-th type has two integer parameters — ci and qi, where ci — is the price of the i-th type t-shirt, qi — is the quality of the i-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, k customers will come to the shop within the next month, the j-th customer will get ready to spend up to 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.

Input

The first line contains the positive integer n (1 ≤ n ≤ 2·105) — the number of t-shirt types.

Each of the following n lines contains two integers ci and qi (1 ≤ ci, qi ≤ 109) — the price and the quality of the i-th type t-shirt.

The next line contains the positive integer k (1 ≤ k ≤ 2·105) — the number of the customers.

The next line contains k positive integers b1, b2, ..., bk (1 ≤ bj ≤ 109), where the j-th number is equal to the sum, which the j-th customer gets ready to spend on t-shirts.

Output

The first line of the input data should contain the sequence of k integers, where the i-th number should be equal to the number of t-shirts, which the i-th customer will buy.

Examples
input
3
7 5
3 5
4 3
2
13 14
output
2 3 
input
2
100 500
50 499
4
50 200 150 100
output
1 2 2 1 
Note

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.

 

题目链接:http://codeforces.com/contest/702/problem/F

 

混合背包问题,推荐背包九讲:http://blog.sina.com.cn/s/blog_8cf6e8d90100zldn.html

 

 

  1 //#pragma comment(linker, "/STACK:102400000,102400000")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 #define vi vector<int>
  5 #define pii pair<int,int>
  6 #define pb push_back
  7 #define mp make_pair
  8 #define all(x) x.begin(),x.end()
  9 #define SZ(x) (int)(x.size())
 10 #define rep(i,a,b) for(int i=a;i<b;i++)
 11 #define per(i,a,b) for(int i=b-1;i>=a;i--)
 12 #define inf 1000000007
 13 #define mod 1000000007
 14 #define x first
 15 #define y second
 16 #define pi acos(-1.0)
 17 #define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
 18 //#define dprintf(...) 
 19 #define hash _hash
 20 #define rank _rank
 21 //#define dprintf(...) fprintf(outFile,__VA_ARGS__)
 22  
 23 #define FOREACH(it,x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
 24 #define ull unsigned long long
 25 #define ll long long
 26 #define N 200010
 27  
 28 template <class T,class U>inline void Max(T &a,U b){if(a<b)a=b;}
 29 template <class T,class U>inline void Min(T &a,U b){if(a>b)a=b;}
 30  
 31 //FILE* outFile;
 32  
 33 inline void add(int &a,int b){a+=b;while(a>=mod)a-=mod;}
 34  
 35 int pow(int a,int b){
 36     int ans=1;
 37     while(b){
 38         if(b&1)ans=ans*(ll)a%mod;
 39         a=(ll)a*a%mod;b>>=1;
 40     }
 41     return ans;
 42 }
 43 
 44 pii a[N];
 45 int ans[N];
 46 struct node{
 47     int pri,v,c,t,id,sum;
 48     node *l,*r;
 49     node(int w=0,int i=0){v=w;id=i;pri=rand();sum=c=t=0;l=r=0;}
 50     void down(){
 51         if(c){
 52             if(l)l->c+=c,l->v+=c;
 53             if(r)r->c+=c,r->v+=c;
 54             c=0;
 55         }
 56         if(t){
 57             if(l)l->t+=t,l->sum+=t;
 58             if(r)r->t+=t,r->sum+=t;
 59             t=0;
 60         }
 61     }
 62 }t[N];
 63 void  split(node *rt,node *(&a),node *(&b),int val){
 64     if(!rt){a=b=0;return;}
 65     rt->down();
 66     if(rt->v>=val){b=rt;split(rt->l,a,b->l,val);return;}
 67     a=rt;split(rt->r,a->r,b,val);
 68 }
 69 node *merge(node *a,node *b){
 70     if(!a)return b;
 71     if(!b)return a;
 72     if(a->pri>=b->pri){
 73         a->down();
 74         a->r=merge(a->r,b);
 75         return a;
 76     }
 77     b->down();
 78     b->l=merge(a,b->l);
 79     return b;
 80 }
 81 node *insert(node *a,node *b){
 82     if(!a)return b;
 83     node *l,*r;
 84     split(a,l,r,b->v);
 85     return merge(l,merge(b,r));
 86 }
 87 node *get_left(node *rt){
 88     while(rt->l){
 89         rt->down();
 90         rt=rt->l;
 91     }
 92     return rt;
 93 }
 94 node *get_right(node *rt){
 95     while(rt->r){
 96         rt->down();
 97         rt=rt->r;
 98     }
 99     return rt;
100 }
101 void upd(node *rt,int v,int w){
102     rt->c+=v;rt->t+=w;rt->v+=v;rt->sum+=w;
103 }
104 void query(node *rt){
105     if(!rt)return;
106     if(rt->l||rt->r){
107         rt->down();
108         ans[rt->id]=rt->sum;
109         query(rt->l);
110         query(rt->r);
111     }
112     else ans[rt->id]=rt->sum;
113 }
114 void print(node *rt){
115     if(!rt)return;
116     if(rt->l)print(rt->l);
117     DBG(rt->v)
118     if(rt->r)print(rt->r);
119 }
120 int main(){
121     int T,i,j,k,n,m,K;
122     scanf("%d",&n);
123     rep(i,0,n)scanf("%d%d",&a[i].y,&a[i].x),a[i].x*=-1;
124     sort(a,a+n);
125     scanf("%d",&m);
126     node *rt;rt=NULL;
127     rep(i,0,m){
128         scanf("%d",&k);
129         t[i]=node(k,i);
130         node *x=&t[i];
131         rt=insert(rt,x);
132     }
133     //print(rt);
134     node *l,*r,*lx,*rx,*ly,*ry;
135     rep(i,0,n){
136         split(rt,l,r,a[i].y);
137         if(!r){rt=l;continue;}
138         upd(r,-a[i].y,1);
139         if(!l){rt=r;continue;}
140         lx=get_left(r),rx=get_right(l);
141         while(lx->v<rx->v){
142             split(r,ly,ry,lx->v+1);
143             l=insert(l,ly);r=ry;
144             if(!l||!r)break;
145             lx=get_left(r),rx=get_right(l);
146         }
147         rt=merge(l,r);
148     }
149     query(rt);
150     rep(i,0,m)printf("%d ",ans[i]);puts("");
151     //cerr<<1.*clock()/CLOCKS_PER_SEC<<"s\n";
152     return 0;
153 }
查看代码

 

转载于:https://www.cnblogs.com/Mino521/p/5722982.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值