BZOJ 1110: [POI2007]砝码Odw( 贪心 )

时间:2021-11-26 05:48:51

BZOJ 1110: [POI2007]砝码Odw( 贪心 )

ORZjcvb...

 #include<bits/stdc++.h>

 using namespace std;

 const int maxn = ;

 int N, M, item[maxn], V[maxn];
vector<int> o, cnt, Div; int main() { cin >> N >> M;
for(int i = ; i < N; i++)
scanf("%d", V + i);
for(int i = ; i < M; i++)
scanf("%d", item + i); sort(item, item + M);
o.push_back(item[]); cnt.push_back();
for(int i = ; i < M; i++)
if(item[i] == item[i - ]) cnt[cnt.size() - ]++;
else {
o.push_back(item[i]);
cnt.push_back();
} int S = o.size();
Div.resize(S);
for(int i = ; i < N; i++) {
for(int j = S - ; ~j; j--) {
int t = V[i] / o[j];
Div[j] += t;
V[i] -= t * o[j];
}
} int ans = ;
for(int i = ; i < S; i++) {
if(cnt[i] > Div[i]) {
for(int j = i + ; j < S && Div[i] < cnt[i]; j++) if(Div[j]) {
int c = ;
for(; o[j] / o[i] * c < cnt[i] - Div[i]; c++)
if(c == Div[j]) break;
Div[j] -= c; Div[i] += o[j] / o[i] * c;
}
ans += min(Div[i], cnt[i]);
if(Div[i] < cnt[i]) break;
} else
ans += cnt[i];
}
printf("%d\n", ans); return ;
}

1110: [POI2007]砝码Odw

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 366  Solved: 202
[Submit][Status][Discuss]

Description

在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作。公司有一些固定容量的容器可以装这些砝码。他们想装尽量多的砝码以便搬运,并且丢弃剩下的砝码。每个容器可以装的砝码数量有限制,但是他们能够装的总重量不能超过每个容器的限制。一个容器也可以不装任何东西。任何两个砝码都有一个特征,他们的中总有一个的重量是另外一个的整数倍,当然他们也可能相等。

Input

输入文件的第一行包含两个数n和m。表示容器的数量以及砝码的数量。(1<=n, m<=100000) 第二行包含n个整数wi,表示每个容器能够装的最大质量。(1<=wi<=1000000000) 第三行包含m个整数mj,表示每个砝码的质量。(1<=mj<=1000000000)

Output

输出文件要求仅包含一个数,为能够装进容器的最多的砝码数量。

Sample Input

2 4
13 9
4 12 2 4

Sample Output

3

HINT

 

Source