BZOJ1736 [Usaco2005 jan]The Wedding Juicer 婚宴的榨汁机

从外面一点一点往里面拓展(floodfill),每次找出最小的一个点,计算它对答案的贡献就好了。。。

找最小的点的话,直接pq就行

 1 /**************************************************************
 2     Problem: 1736
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:196 ms
 7     Memory:2116 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <queue>
12
13 using namespace std;
14 typedef long long ll;
15 const int N = 305;
16 const int dx[] = {0, 0, 1, -1};
17 const int dy[] = {1, -1, 0, 0};
18
19 inline int read();
20
21 struct data {
22     int x, y, h;
23     data(int _x = 0, int _y = 0, int _h = 0) : x(_x), y(_y), h(_h) {}
24
25     inline bool operator < (const data &d) const {
26         return h > d.h;
27     }
28 };
29
30 int n, m;
31 int mp[N][N], v[N][N];
32 priority_queue <data> h;
33
34 inline ll work(){
35     ll res = 0;
36     int x, y, k;
37     data now;
38     while (!h.empty()) {
39         now = h.top(), h.pop();
40         for (k = 0; k < 4; ++k) {
41             x = now.x + dx[k], y = now.y + dy[k];
42             if (x <= 0 || y <= 0 || x > n || y > m || v[x][y]) continue;
43             v[x][y] = 1;
44             if (mp[x][y] < now.h)
45                 res += now.h - mp[x][y], mp[x][y] = now.h;
46             h.push(data(x, y, mp[x][y]));
47         }
48     }
49     return res;
50 }
51
52 int main() {
53     int i, j;
54     m = read(), n = read();
55     for (i = 1; i <= n; ++i)
56         for (j = 1; j <= m; ++j) mp[i][j] = read();
57     for (i = 1; i <= n; ++i)
58         for (j = 1; j <= m; ++j)
59             if (i == 1 || j == 1 || i == n || j == m)
60                 h.push(data(i, j, mp[i][j])), v[i][j] = 1;
61     printf("%lld\n", work());
62     return 0;
63 }
64
65 inline int read() {
66     static int x;
67     static char ch;
68     x = 0, ch = getchar();
69     while (ch < ‘0‘ || ‘9‘ < ch)
70         ch = getchar();
71     while (‘0‘ <= ch && ch <= ‘9‘) {
72         x = x * 10 + ch - ‘0‘;
73         ch = getchar();
74     }
75     return x;
76 }

时间: 2024-09-30 18:54:38

BZOJ1736 [Usaco2005 jan]The Wedding Juicer 婚宴的榨汁机的相关文章

1677: [Usaco2005 Jan]Sumsets 求和

1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 626  Solved: 348[Submit][Status] Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers th

[BZOJ1677][Usaco2005 Jan]Sumsets 求和

1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1031  Solved: 603 [Submit][Status][Discuss] Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use onl

[BZOJ1679][Usaco2005 Jan]Moo Volume 牛的呼声

1679: [Usaco2005 Jan]Moo Volume 牛的呼声 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1097  Solved: 571 [Submit][Status][Discuss] Description Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too

[poj] The Wedding Juicer | [lintcode] Trapping Rain Water II

问题描述 给定一个二维矩阵,每个元素都有一个正整数值,表示高度.这样构成了一个二维的.有高度的物体.请问该矩阵可以盛放多少水? 相关题目:POJ The Wedding Juicer Description Farmer John's cows have taken a side job designing interesting punch-bowl designs. The designs are created as follows: * A flat board of size W cm

BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )

完全背包.. --------------------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define

BZOJ 1679 [Usaco2005 Jan]Moo Volume 牛的呼声

解法1: N^2的暴力程序,卡卡常数就过了. #include <cstdio> #include <algorithm> #include <cmath> int n; int a[10005]; long long tot; inline bool Read(int &ret){ char c; int flag = 1; if(c=getchar(),c==EOF) return 0; while(c!='-' && (c<'0'||

bzoj:1677: [Usaco2005 Jan]Sumsets 求和

Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 1) 1+1+1+1+1+1+1 2) 1+1+1+1+

【bzoj1737】[Usaco2005 jan]Naptime 午睡时间

题目描述 Goneril is a very sleep-deprived cow. Her day is partitioned into N (3 <= N <= 3,830) equal time periods but she can spend only B (2 <= B < N) not necessarily contiguous periods in bed. Due to her bovine hormone levels, each period has it

BZOJ 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场

Description 大雨侵袭了奶牛们的牧场.牧场是一个R * C的矩形,其中1≤R,C≤50.大雨将没有长草的土地弄得泥泞不堪,可是小心的奶牛们不想在吃草的时候弄脏她们的蹄子.  为了防止她们的蹄子被弄脏,约翰决定在泥泞的牧场里放置一些木板.每一块木板的宽度为1个单位,长度任意.每一个板必须放置在平行于牧场的泥地里.    约翰想使用最少的木板覆盖所有的泥地.一个木板可以重叠在另一个木板上,但是不能放在草地上. Input 第1行:两个整数R和C. 第2到R+1行:每行C个字符,其中"*'代