1595 - Symmetry (UVA)

题目链接如下:

Online Judge

开始的代码如下(能AC,但是太繁琐了,处理得很丑陋。其实就是把在中线上的点都删了,然后再看左右两边的点对不对称):

#include 
#include 
#include 
// #define debug

struct pt{
    int x, y;
};
int T, n, minx, maxx;
std::vector vec;

bool cmp1(const pt &a, const pt &b){
    return a.x != b.x ? a.x < b.x : a.y < b.y;
}

bool cmp2(const pt &a, const pt &b){
    return a.x != b.x ? a.x < b.x : a.y > b.y;
}

bool isSymmetric(std::vector &v){
    int sz = v.size();
    int xx = minx + maxx;
    for (int i = 0; i < v.size(); ++i){
        if(xx == 2 * vec[i].x){
            vec[i].x = 10001;
            sz--;
        }
    }
    sort(v.begin(), v.end(), cmp1);
    v.resize(sz);
    sort(v.begin() + sz / 2, v.end(), cmp2);
    for (int i = 0; i < sz / 2; ++i){
        if(v[i].y != v[sz - 1 - i].y || v[i].x + v[sz - 1 - i].x != xx){
            return false;
        }
    }
    return true;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d", &T);
    while (T--){
        scanf("%d", &n);
        vec.resize(n);
        minx = 10001;
        maxx = -10001;
        for (int i = 0; i < n; ++i){
            scanf("%d %d", &vec[i].x, &vec[i].y);
            minx = std::min(minx, vec[i].x);
            maxx = std::max(maxx, vec[i].x);
        }
        printf("%s\n", isSymmetric(vec) ? "YES" : "NO");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

修改后的代码如下,感觉稍微好一丢丢:

#include 
#include 
#include 
// #define debug

struct pt{
    int x, y;
};
int T, n;
std::vector vec;

bool cmp1(const pt &a, const pt &b){
    return a.x != b.x ? a.x < b.x : a.y < b.y;
}

bool cmp2(const pt &a, const pt &b){
    return a.x != b.x ? a.x < b.x : a.y > b.y;
}

bool isSymmetric(std::vector &v){
    int sz = v.size();
    sort(v.begin(), v.end(), cmp1);
    int xx = v.front().x + v.back().x;
    sort(v.begin() + sz / 2, v.end(), cmp2);
    for (int i = 0; i < sz / 2 + 1; ++i){
        if(2 * v[i].x == xx && 2 * v[sz - 1 - i].x == xx){
            continue;
        }
        if(v[i].y != v[sz - 1 - i].y || v[i].x + v[sz - 1 - i].x != xx){
            return false;
        }
    }
    return true;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d", &T);
    while (T--){
        scanf("%d", &n);
        vec.resize(n);
        for (int i = 0; i < n; ++i){
            scanf("%d %d", &vec[i].x, &vec[i].y);
        }
        printf("%s\n", isSymmetric(vec) ? "YES" : "NO");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,c++)