Files
python/C++专题课程/NOIP算法总结/模拟/花生采摘/P1086.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

46 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
struct hs //指花生:)
{
int x, y, v;
bool operator < (const hs & a) const
{
return v < a.v;
}//优先队列默认从大到小排序,但是需要用到小于号,所以这里重载运算符 < (只重载<就可以了)
};
priority_queue <hs> q;
int main()
{
int m, n, k;
int sum = 0;
cin >> m >> n >> k;
int a;
hs t;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
{
cin >> a;
t.v = a;
t.x = j;
t.y = i;
if(a != 0) //如果有数值,就推进队列
q.push(t);
}
k -= q.top().y; //走到最大的花生植株
while(!q.empty() && k > q.top().y) //第一个是检验队列不为空,如果空了说明全摘完了,第二个是检验剩余的时间可以走回路边
{
k--; //只要进入一次循环,说明到了一棵植株,采摘耗费1时间
sum += q.top().v;
int lx = q.top().x, ly = q.top().y;
q.pop();//换下一棵树
k -= abs(lx - q.top().x) + abs(ly - q.top().y);//剩余的时间减去从上一棵植株到这棵的时间
}
cout << sum << endl;
return 0;
}