COCI 2011-2012 setnja

TM :setnja (1S256M)一个人要散步去会见他的 N 个朋友(按给定的顺序会见)。我们可以理解成他们都住在一个

很大的网格内,每个朋友住其中的一个单元格,所有人每一步都可以走到相邻的八个格子中。
每个朋友最多可以走 P 步与他相见,每个人的 P 值不一定相同。他可以决定起点和终点。
问他会见完所有朋友的最少步数。
输入:
第一行,一个正整数 N (2 N 200 000),表示朋友个数。
接下来,N 行,描述 N 个朋友,x, y, and P (0 x, y, P 200 000)x,y 表示朋友最初的坐
标。朋友给的顺序就是他要依次会见的顺序。
输出:
一个数,表示他要走的最少的步数。
Scoring
30%的数据,所有数字最多 20.
另有 30%的数据,所有朋友的 P 值小于 10.
样例:
Input
3
3 10 2
8 4 2
2 5 2
output
4
input
4
3 3 5
7 11 5
20 8 10
30 18 3
output
19
样例 1,从 (4, 8),出发会见第一个朋友。走两步在(66)会见第二个朋友,走两步到(45)会见第三个朋友。
//暂时没找到那个oj上有

代码 :

#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

const int inf = 1000000;
const int N = 2e5 + 10;
struct size_ {
    int x1, x2, y1, y2;
    size_ () {}
    size_ (int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) {}
}c[N];

size_ cmp(size_ a, size_ b) {
    size_ w(max(a.x1, b.x1), min(a.x2, b.x2), max(a.y1, b.y1), min(a.y2, b.y2));
    if( w.x1 > w.x2 || w.y1 > w.y2) return size_ (-inf, -inf, -inf, -inf);
    return w;
}

int n;
int main() {
    freopen("setnja.in", "r", stdin);
    freopen("setnja.out", "w", stdout);
    scanf("%d", &n);
    for( int i = 1, x, y, p; i <= n; i ++) 
        scanf("%d%d%d", &x, &y, &p), c[i] = size_(x-p, x+p, y-p, y+p);
    size_ w = c[1]; 
        long long ans = 0ll; // 开 long long ,不然会炸
    for( int i = 2; i <= n; i ++) {
        size_ t = cmp(w, c[i]);
        if( t.x1 != -inf) {    w = t; continue ;}
        int x = max(max(w.x1 - c[i].x2, c[i].x1 - w.x2), max(w.y1 - c[i].y2, c[i].y1 - w.y2));
//        printf("%d %d<< \n", x, i); // de bug
        w = cmp(size_(w.x1-x, w.x2+x, w.y1-x, w.y2+x), c[i]); ans += (long long)x;
    }
    printf("%lld", ans);
    return 0;
}   
View Code

时间较短,博主考试炸了要改题,为什么这么做后2天找时间补上

猜你喜欢

转载自www.cnblogs.com/miecoku/p/9047451.html