题解 CF16B 【Burglar and Matches】
这道题目是最简单的贪心问题啦!用STL的二元结构体pair或者用struct,class都可以解决的!
那么我用的是pair,比struct似乎是慢了一点,1.27s,自定义排序 + sort
#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;
#define p pair<int, int>//宏定义一下,不然写得多
int n, m, x, y;
bool cmp(p a, p b)//自定义排序
{
return a.second > b.second;//不管first,只排second
}
p a[25];
int main()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> x >> y;
a[i] = make_pair(x, y);//读入可以用make_pair,也可以用直接first和second,都行的哦!
}
sort(a + 1, a + m + 1, cmp);//排序
int ans = 0, s = 0;//s是现在的火柴盒数量,ans是答案
for(int i = 1; i <= m; i++)
{
s += a[i].first;//加上火柴盒的数量
if(s > n)//如果已经多于可以取的就加上现在还能取的
{
ans += (n - s + a[i].first) * a[i].second;
break;//循环停下来
}
ans += a[i].first * a[i].second;//直接乘
}
cout << ans << endl;
return 0;
}
End.
哦,还没end,求过
End.
哦,还要求赞
End.

浙公网安备 33010602011771号