题目背景
思路解析:很经典的贪心问题,把物品按照从便宜到贵的顺序排好序,然后按照富贵程度排人,直接暴力会tle所以这里采用双指针.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;
int n, m, res;
int qian[100005], jia[100005];
int main()
{cin >> n >> m;for (int i = 0; i < n; i++)cin >> qian[i];for (int i = 0; i < m; i++)cin >> jia[i];sort(qian, qian + n);sort(jia, jia + m);//排序int j = 0, i = 0;while(1)//循环往后//i代表了钱数的坐标,j代表了价格的坐标{if (i > n || j > m) break;//表示越界直接breakif (qian[i]>=jia[j])//钱够直接买{res++;i++;j++;}else{i++;//让钱多的人来}}cout << res;return 0;
}