思路:这道题很简单,直接1层循环枚举圈,另外一层循环枚举羊腿,如果 能套上,则判断是否为最大价值,最后输出即可。
时间复杂度:O(n*m)
因为题目保证了,所以不会超时。
代码:
#include <bits/stdc++.h>
using namespace std;
long long t,n,m,rc[1000001],rb[1000001],v[1000001],ans;
int main()
{
cin>>n>>m;
for(int i = 0; i < n; i++) cin>>rc[i];
for(int i = 0; i < m; i++) cin>>rb[i]>>v[i];
for(int i = 0; i < n; i++)
{
t = 0;
for(int j = 0; j < m; j++)
if(rc[i] > rb[j] && t < v[j])
t = v[j];
ans += t;
}
cout<<ans;
return 0;
}
优化:注意,题目中 ,所以我们可以用类似桶排序的思想来优化。用一个桶数组,下标为大小,价值为值,to[i]也就是大小为i,价值的最大值。
时间复杂度:O(100 * n)
代码:
#include <bits/stdc++.h>
using namespace std;
long long t,n,m,rc[1000001],rb,v,ans,ton[10000001],ts;
int main()
{
cin>>n>>m;
for(int i = 0; i < n; i++) cin>>rc[i];
for(int i = 0; i < m; i++)
{
cin>>rb>>v;
if(v > ton[rb]) ton[rb] = v;
}
for(int i = 0; i < n; i++)
{
t = 0;
for(int j = 0; j < 101; j++)
if(rc[i] > j && t < ton[j])
t = ton[j];
ans += t;
}
cout<<ans;
return 0;
}