RQNOJ 23 花生采摘 解题报告
嗯,我练习了下二叉堆,代码写得挺长的,其实题目比较简单,就是贪心就行……
嗯,上代码吧:
#include <math.h> #include <stdio.h> #include <stdlib.h> #define left(i) (((i) << 1) + 1) #define right(i) (((i) << 1) + 2) #define parent(i) (((i) - 1) >> 1) struct node{ int x, y, v; }heap[400]; int end; int n, m; void insert(int v, int x, int y) { int i = end; struct node t = {x, y, v}; while(i != 0){ if(heap[parent(i)].v < t.v){ heap[i] = heap[parent(i)]; }else{ break; } i = parent(i); } heap[i] = t; end++; } struct node delete(void) { struct node t = heap[0]; int i = 0, j; --end; while(left(i) < end){ j = left(i); if(right(i) < end && heap[right(i)].v > heap[j].v){ j = right(i); } if(heap[end].v < heap[j].v){ heap[i] = heap[j]; }else{ break; } i = j; } heap[i] = heap[end]; return t; } int getdis(struct node a, struct node b) { return abs(a.x - b.x) + abs(a.y - b.y) + 1; } int main(int argc, char **argv) { int i, j, t, s; struct node l, p; int k; scanf("%d%d%d", &n, &m, &k); for(i = 1; i <= n; i++){ for(j = 1; j <= m; j++){ scanf("%d", &t); if(t != 0){ insert(t, i, j); } } } t = s = 0; p = delete(); l.x = l.v = 0; l.y = p.y; while(t + getdis(l, p) + p.x <= k){ t += getdis(l, p); s += p.v; l = p; if(end == 0){ break; } p = delete(); } printf("%d\n", s); return 0; }