fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int maxCitiesWithFuel(int N, int M, vector<int>& cityDemand, vector<int>& stationSupply) {
  8. sort(cityDemand.begin(), cityDemand.end());
  9. sort(stationSupply.begin(), stationSupply.end());
  10.  
  11. int cityIdx = 0, stationIdx = 0, count = 0;
  12.  
  13. while (cityIdx < N && stationIdx < M) {
  14. if (stationSupply[stationIdx] >= cityDemand[cityIdx]) {
  15. // Station can fulfill this city's demand
  16. count++;
  17. cityIdx++;
  18. stationIdx++;
  19. } else {
  20. // Station cannot fulfill, try next station
  21. stationIdx++;
  22. }
  23. }
  24.  
  25. return count;
  26. }
  27.  
  28. int main() {
  29. int N, M;
  30. cin >> N >> M;
  31. vector<int> cityDemand(N);
  32. vector<int> stationSupply(M);
  33.  
  34. for (int i = 0; i < N; ++i) {
  35. cin >> cityDemand[i];
  36. }
  37.  
  38. for (int i = 0; i < M; ++i) {
  39. cin >> stationSupply[i];
  40. }
  41.  
  42. cout << maxCitiesWithFuel(N, M, cityDemand, stationSupply) << endl;
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5292KB
stdin
10
aba
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
0