题目链接:https://www.luogu.org/problemnew/show/P1086
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 1000;
int M, N, TIME;
int groundnut[MAX_N][MAX_N];
struct node {
int num;
int x, y;
}book[MAX_N];
int cmp (node x, node y) {
return x.num > y.num;
}
int dance (node x, node y) {
return ((x.x - y.x) > 0 ? (x.x - y.x) : (y.x - x.x)) + ((x.y - y.y) > 0 ? (x.y - y.y) : (y.y - x.y));
}
int main () {
cin >> M >> N >> TIME;
int ans = 0;
for (int i = 1; i <= M; i++)
for (int j = 1; j <= N; j++) {
cin >> groundnut[i][j];
book[ans].num = groundnut[i][j];
book[ans].x = j; book[ans++].y = i;
}
sort (book, book + ans, cmp);
int sum = 0; // 总的花生的数量
int i = 0;
// while (true) {
// if (TIME >= 2 * book[i].y + 1) {
// sum += book[i].num;
cout << TIME << " " << book[i].y << endl;
// TIME -= book[i].y + 1;
cout << TIME << endl;
// break;
// } else if (i == ans - 1) break;
// else i++;
// }
//这里是自己理解错题意了(手动尴尬!=_=)
bool flag = true;
if (TIME >= 2 * book[i].y + 1) {
sum += book[i].num;
TIME -= book[i].y + 1;
flag = false;
}
while (TIME > 0) {
if (i == ans - 1) break;
int far = dance (book[i], book[i + 1]);
if (TIME < far + 1 + book[i + 1].y) break;
// cout << TIME << " " << far + 1 + book[i + 1].y << endl;
sum += book[i + 1].num;
TIME -= far + 1;
i ++;
}
if (flag) sum = 0;
cout << sum << endl;
return 0;
}