UVA 11012 - Cosmic Cabbages(枚举技巧)
2014-01-09 18:47
369 查看
Problem A
Cosmic Cabbages
Input: Standard Input
Output: Standard Output
Ambrose Bierce
Scientists from the planet Zeelich have figured out a way to grow cabbages in space. They have constructed a huge 3-dimensional steel grid upon which they plant said cabbages. Each cabbage is attached to a corner in
the grid, where 6 steel cables meet and is assigned Cartesian coordinates. A cosmic ant wants to crawl from cabbage X to cabbage Y along the cables that make the grid. The cosmic ant always chooses the shortest possible path along the grid lines while going
from cabbage X to cabbage Y. This distance is called the cosmic distance between two cabbages. Given a collection of cabbages what is the maximum distance between any two of the cabbages?
Input
The first line of input gives the number of cases, N (0<N<21). N test cases follow. Each one starts with a line containing n (2<=n<=105). The next n lines will
each give the 3-dimensional coordinates of a cosmic cabbage (integers in the range [-108, 108]).
[align=center][/align]
题意:给定n个三维坐标点,求两两最大曼哈顿距离。
思路:直接枚举O(n^2)不可行,那么换个方法:列出公式 d = |x1 - x2| + |y1 - y2| + |z1 - z2|。对应8种情况这里不一一列举,就举其中x1 - x2 + y1 - y2 + z1 - z2.。转换为(x1 + y1 + z1) - (x2 + y2 + z2)其他同理。发现只要前面尽可能大,后面尽可能小,出来的曼哈顿距离必然最大,那么对应8种情况,每种对于每个点都枚举过去,保存下所有点中x +|- y +|- z 最大和最小的值,最大的作为减数,最小最为被减数,然后对应8种情况中求出最大的即可。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x3f3f3f3f
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
const int N = 100005;
int t, n;
struct Point {
int v[3];
}p
;
void init() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d%d", &p[i].v[0], &p[i].v[1], &p[i].v[2]);
}
int solve() {
int ans = 0;
for (int i = 0; i < (1<<3); i++) {
int Min = INF, Max = -INF;
for (int j = 0; j < n; j ++) {
int sum = 0;
for (int k = 2; k >= 0; k--) {
if (i&(1<<k)) sum += p[j].v[2 - k];
else sum -= p[j].v[2 - k];
}
Max = max(Max, sum); Min = min(Min, sum);
}
ans = max(ans, Max - Min);
}
return ans;
}
int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
init();
printf("Case #%d: %d\n", ++cas, solve());
}
return 0;
}
Cosmic Cabbages
Input: Standard Input
Output: Standard Output
CABBAGE, n. A familiar kitchen-garden vegetable about as large and wise as a man's head. |
Scientists from the planet Zeelich have figured out a way to grow cabbages in space. They have constructed a huge 3-dimensional steel grid upon which they plant said cabbages. Each cabbage is attached to a corner in
the grid, where 6 steel cables meet and is assigned Cartesian coordinates. A cosmic ant wants to crawl from cabbage X to cabbage Y along the cables that make the grid. The cosmic ant always chooses the shortest possible path along the grid lines while going
from cabbage X to cabbage Y. This distance is called the cosmic distance between two cabbages. Given a collection of cabbages what is the maximum distance between any two of the cabbages?
Input
The first line of input gives the number of cases, N (0<N<21). N test cases follow. Each one starts with a line containing n (2<=n<=105). The next n lines will
each give the 3-dimensional coordinates of a cosmic cabbage (integers in the range [-108, 108]).
Output
For each test case, output one line containing "Case #x:" followed by the largest cosmic distance between cabbages X and Y, out of all possible choices of X and Y.Sample Input Output for Sample Input
4 2 1 1 1 2 2 2 3 0 0 0 0 0 1 1 1 0 4 0 1 2 3 4 5 6 7 8 9 10 11 6 0 0 0 1 1 1 2 2 2 0 0 1 1 0 0 0 1 0 | Case #1: 3 Case #2: 3 Case #3: 27 Case #4: 6 |
题意:给定n个三维坐标点,求两两最大曼哈顿距离。
思路:直接枚举O(n^2)不可行,那么换个方法:列出公式 d = |x1 - x2| + |y1 - y2| + |z1 - z2|。对应8种情况这里不一一列举,就举其中x1 - x2 + y1 - y2 + z1 - z2.。转换为(x1 + y1 + z1) - (x2 + y2 + z2)其他同理。发现只要前面尽可能大,后面尽可能小,出来的曼哈顿距离必然最大,那么对应8种情况,每种对于每个点都枚举过去,保存下所有点中x +|- y +|- z 最大和最小的值,最大的作为减数,最小最为被减数,然后对应8种情况中求出最大的即可。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x3f3f3f3f
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
const int N = 100005;
int t, n;
struct Point {
int v[3];
}p
;
void init() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d%d", &p[i].v[0], &p[i].v[1], &p[i].v[2]);
}
int solve() {
int ans = 0;
for (int i = 0; i < (1<<3); i++) {
int Min = INF, Max = -INF;
for (int j = 0; j < n; j ++) {
int sum = 0;
for (int k = 2; k >= 0; k--) {
if (i&(1<<k)) sum += p[j].v[2 - k];
else sum -= p[j].v[2 - k];
}
Max = max(Max, sum); Min = min(Min, sum);
}
ans = max(ans, Max - Min);
}
return ans;
}
int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
init();
printf("Case #%d: %d\n", ++cas, solve());
}
return 0;
}
相关文章推荐
- UVa 11012 - Cosmic Cabbages (机智地枚举)
- uva 11012 - Cosmic Cabbages
- uva 11012——Cosmic Cabbages
- UVa:11012 Cosmic Cabbages
- uva 11752 - The Super Powers(数论+枚举技巧)
- UVA 10730 - Antiarithmetic?(技巧枚举)
- UVA 10483 - The Sum Equals the Product (枚举技巧)
- UVA - 11754 Code Feat CRT (big技巧枚举 small暴力)
- uva 1382 Distant Galaxy(离散化+枚举技巧)
- uva 1618(枚举技巧)
- uva 471 - Magic Numbers(枚举技巧)
- UVA-1312 Cricket Field (技巧枚举)
- UVA 1481 - Genome Evolution(枚举技巧)
- UVA 12174 - Shuffle(技巧枚举+预处理)
- UVa229 - Scanner(枚举技巧)
- UVA 1432 - Fire-Control System(技巧枚举+线性扫描)
- (UVA - 10976)Fractions Again?!(技巧,暴力枚举)
- uva 229 - Scanner(枚举技巧)
- UVA 1326 - Jurassic Remains(技巧枚举+位运算)
- UVA 1508 Equipment(技巧枚举)