0
点赞
收藏
分享

微信扫一扫

CF702F T-Shirts FHQ Treap

题意翻译

题目大意:

有n种T恤,每种有价格ci和品质qi。有m个人要买T恤,第i个人有vi元,每人每次都会买一件能买得起的qi最大的T恤。一个人只能买一种T恤一件,所有人之间都是独立的。问最后每个人买了多少件T恤?

题目描述

The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all CF702F T-Shirts FHQ Treap_#include

As predicted, CF702F T-Shirts FHQ Treap_#define_02

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 CF702F T-Shirts FHQ Treap_i++_03

Each of the following CF702F T-Shirts FHQ Treap_#define_04

The next line contains the positive integer CF702F T-Shirts FHQ Treap_#include_05

The next line contains CF702F T-Shirts FHQ Treap_#define_06

输出格式:

The first line of the input data should contain the sequence of CF702F T-Shirts FHQ Treap_#define_07

输入输出样例

输入样例#1:

复制

3
7 5
3 5
4 3
2
13 14

输出样例#1: 复制

2 3

输入样例#2: 复制

2
100 500
50 499
4
50 200 150 100

输出样例#2: 复制

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.

朴素算法很好想,但TLE;

考虑用平衡树维护;

我们先将衬衫按quality排序;

然后对于每一件衬衫,我们在用人的钱构成的树中操作;

对于>=C[i] 的进行标记,然后下传;

但是-C[i]后,不一定满足merge的条件;

我们可以暴力对于重复的部分进行merge;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair pii;

inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}


ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/

int n, m, root;
int lson[maxn], rson[maxn], val[maxn], rnd[maxn];
int cnt[maxn], add[maxn], ans[maxn];

void pushdown(int v) {
if (add[v]) {
add[lson[v]] += add[v]; add[rson[v]] += add[v];
val[lson[v]] += add[v]; val[rson[v]] += add[v];
add[v] = 0;
}
if (ans[v]) {
ans[lson[v]] += ans[v]; ans[rson[v]] += ans[v];
cnt[lson[v]] += ans[v]; cnt[rson[v]] += ans[v];
ans[v] = 0;
}
}

void split(int k, int &x, int &y, int v) {
if (!k) {
x = y = 0; return;
}
pushdown(k);
if (val[k] < v) {
x = k; split(rson[k], rson[x], y, v);
}
else {
y = k; split(lson[k], x, lson[y], v);
}
}

int merge(int x, int y) {
if (!x || !y)return x + y;
if (rnd[x] < rnd[y]) {
pushdown(x); rson[x] = merge(rson[x], y);
return x;
}
else {
pushdown(y); lson[y] = merge(x, lson[y]);
return y;
}
}

int ins(int x, int y) {
int r1 = 0, r2 = 0;
split(x, r1, r2, val[y]);
x = merge(merge(r1, y), r2);
return x;
}

int build(int v, int y) {
if (!v)return y;
pushdown(v);
y = build(lson[v], y); y = build(rson[v], y);
lson[v] = rson[v] = 0;
return ins(y, v);
}

void dfs(int v) {
if (!v)return;
pushdown(v);
dfs(lson[v]); dfs(rson[v]);
}
pii a[maxn];


int main()
{
//ios::sync_with_stdio(0);
n = rd(); int c, q;
for (int i = 1; i <= n; i++) {
c = rd(); q = rd();
a[i] = make_pair(-q, c);
}
sort(a + 1, a + 1 + n);
m = rd();
for (int i = 1; i <= m; i++) {
val[i] = rd();
rnd[i] = rand();
root = ins(root, i);
}
for (int i = 1; i <= n; i++) {
int c = a[i].second;
int r1 = 0, r2 = 0, r3 = 0, r4 = 0;
split(root, r1, r2, c);
val[r2] -= c; add[r2] -= c;
cnt[r2]++; ans[r2]++;
split(r2, r3, r4, c - 1);
r1 = build(r3, r1);
root = merge(r1, r4);
}
dfs(root);
for (int i = 1; i <= m; i++)printf("%d ", cnt[i]);
return 0;
}

 

EPFL - Fighting

举报

相关推荐

0 条评论