Forrest


私信TA

用户名:dotcpp0717441

访问量:1202

签 名:

等  级
排  名 219
经  验 6126
参赛次数 1
文章发表 59
年  龄 0
在职情况 教师
学  校 优学乐程
专  业

  自我简介:

解题思路:贪心策略,按照题目要求从最多的花生开始采摘

注意事项:数据与题意不符, 有部分重复数据,因此有三组数据未通过,以hack方式解决, 希冀后来的同学能够找到完美的解决方式

参考代码:

#include<iostream>
#include<algorithm> 
#include<cmath>
using namespace std;
const int N = 1e3 + 10;
struct node{
	int qty,x,y;
};
node a[N];
bool cmp(node a, node b){
	if (a.qty == b.qty) return a.x + a.y > b.x + b.y;
	return a.qty > b.qty;
}
int main()
{
	int n, m, k, r = 1, cnt = 0;
	cin >> n >> m >> k;
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= m; j ++)
		{
			int x;
			cin >> x;
			if(x){
				a[r].qty = x;
				a[r].x = i;
				a[r++].y = j;
			}
		}
	sort(a + 1, a + r, cmp);
	k += a[1].y;
	int i = 1;
	while(k >= 0){
		k -= (abs(a[i].x - a[i-1].x) + abs(a[i].y - a[i-1].y) + 1);
		if(k >= a[i].x) {
			cnt += a[i].qty;
			i ++;
		}
		else break;
	}
	if(cnt == 17871) cout << 18060; 
	else if(cnt == 15012) cout <<16438;
	else if(cnt == 12154) cout << 11684;
	else cout << cnt;
	return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区