HDU 2439 the mussels

Source: Internet
Author: User
The musselstime limit: 1000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 2439
64-bit integer Io format: % i64d Java class name: Main "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.
Inputfor 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 integers all in the range 1 ~ N, indicating the mussels 'grade in order.

Proceed to the end of file.
Outputa single integer, which is the number of mussels that can be put into the culturists.
Sample Input
4 4100 4 4 41 2 3 44 31 1 1 14 2 2
Sample output
43
Source2008 Asia Harbin Regional Contest online problem solving: greedy + simulation, just follow the meaning of the question to do it...
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 int cul[100010],tmp,n,m;18 void myscanf(int &x){19     char ch;20     while((ch = getchar()) < ‘0‘ || ch > ‘9‘);21     x = 0;22     x = x*10 + ch - ‘0‘;23     while((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) x = x*10 + ch - ‘0‘;24 }25 int main() {26     while(~scanf("%d %d",&n,&m)){27         for(int i = 1; i <= n; i++)28             myscanf(cul[i]);29         int ans = 0;30         for(int i = 1; i <= m; i++){31             myscanf(tmp);32             if(cul[tmp]){33                 --cul[tmp];34                 ++ans;35             }else{36                 for(int j = tmp; j; --j){37                     if(cul[j]){38                         --cul[j];39                         ++ans;40                         break;41                     }42                 }43             }44         }45         printf("%d\n",ans);46     }47     return 0;48 }
View code

 

HDU 2439 the mussels

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.