讨论 / 排序做简单高效
沧海一声喵 2018-02-27 11:17:49
点我顶贴 收藏 删除
#include <cstdio>

#include <algorithm>

using namespace std;

typedef struct node{

int i,j,num;

}node;

int cmp(const node &s,const node &t){

return s.num>t.num;}

int main(){

int i,j,m,n,K,s,x,i0,j0;node v[401];

int d,num,k;

scanf("%d%d%d",&m,&n,&K);

for(s=0,i=1;i<=m;i++)

for(j=1;j<=n;j++){

scanf("%d",&x);

if(x){

s++;v[s].i=i;v[s].j=j;v[s].num=x;}}

sort(v+1,v+s+1,cmp);

num=0;k=0;i0=0;j0=v[1].j;

for(i=1;i<=s;i++){

d=abs(v[i].i-i0)+abs(v[i].j-j0)+v[i].i+1;

if(d+k<=K){

num+=v[i].num;

k=k+(d-v[i].i);

i0=v[i].i;j0=v[i].j;}

else break;}

printf("%d",num);

return 0;}

查看更多回复
提交回复