[luoguP1086] 花生采摘(模拟)

传送门

 

模拟。。。


代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#define abs(x) ((x) < 0 ? -(x) : (x))

int n, m, k, cnt, ans;

struct node
{
	int v, x, y;
}p[1001];

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
	return x * f;
}

inline bool cmp(node x, node y)
{
	return x.v > y.v;
}

inline int query(int i, int j)
{
	return abs(p[i].x - p[j].x) + abs(p[i].y - p[j].y);
}

int main()
{
	int i, j, x;
	n = read();
	m = read();
	k = read();
	for(i = 1; i <= n; i++)
		for(j = 1; j <= m; j++)
			if(x = read())
				p[++cnt].v = x, p[cnt].x = i, p[cnt].y = j;
	std::sort(p + 1, p + cnt + 1, cmp);
	if(p[1].x * 2 + 1 <= k) ans += p[1].v, k -= p[1].x + 1;
	else
	{
		puts("0");
		return 0;
	}
	for(i = 2; i <= cnt; i++)
	{
		if(query(i - 1, i) + p[i].x + 1 > k)
		{
			printf("%d\n", ans);
			return 0;
		}
		ans += p[i].v;
		k -= query(i - 1, i) + 1;
	}
	printf("%d\n", ans);
	return 0;
}

  

posted @ 2017-06-26 11:07  zht467  阅读(157)  评论(0编辑  收藏  举报