大家帮我看看这个用C怎么编 ORZ

来源:百度知道 编辑:UC知道 时间:2024/05/17 21:00:07
"To be or not to be, that is the question." Now FJ( Frank&John) faces a serious problem.
FJ is breeding mussels these days. The mussels all want to be put into a culturist with a grade no little than their own grades. FJ accept their requirements.
FJ provides culturists of different grades, all having a certain capacity. FJ first put mussels into culturists with the same grade until they are full. Then he may put some mussels into some potential culturists that still have capacity.
Now, FJ wants to know how many mussels can be put into the culturists.

Input

For each data set:
The first line contains two integers, n and m(0<n<=100000,0<m<=1000000), indicating the number of culturists and the number of mussels. The ith culturist has a grade i, and grade 1 is considered the highest.
The second line contains n integers indicating the culturists' capacity in order.
The third line contains m integer

我的感觉就是模拟一下

#include<iostream>
#include<cstdio>
using namespace std;

#define MaxN (1000100)

int N, M;
int cap[MaxN],a[MaxN],i,j, res,rem;

int run() {
for (i=0;i<N;++i) scanf("%d", cap+i);
for (i=0;i<M;++i) {scanf("%d", a+i); --a[i];}
sort(a,a+M);
for (i=j=res=rem=0;j<M;++j) {
for (;i<N&&i<=a[j];rem+=cap[i++]);
if (rem>0) ++res, --rem;
} printf("%d\n", res);
}

int main() {
while (scanf("%d %d", &N, &M)==2) run();
}