poj 3349(重載比較操作符+排序)

SnowflakeSnowSnowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 27955   Accepted: 7393

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed byn lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.

Sample Input

2
1 2 3 4 5 6
4 3 2 1 6 5

Sample Output

Twin snowflakes found.

Source

CCC 2007

剛開始用set存儲每一個雪片。結果tle了。。。
直接用數組存放,然後排序,排序後只需要與相鄰元素比對就可以判斷是否存在相同雪花。
這裏的技巧是,對6個雪片進行預處理:從最大的角開始,順時針來一遍,逆時針來一遍,獲得兩個序列。然後選擇“值”較大的那個序列放入數組。這裏直接重載了<操作符和==操作符。
這樣保證了數組中的元素不會超過100000。

提交記錄:
1.TLE, 使用set失敗。。。
2.Accepted。使用數組,然後排序。

使用set後超時的代碼:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define oo 0x3f3f3f3f
#define MAX_N 30010
#define MAX_M 510
struct snow {
    int w[6];
};
bool operator < (const snow & a, const snow & b) {
    for (int i = 0; i < 6; i++) {
        if (a.w[i] < b.w[i]) return true;
        else if (a.w[i] > b.w[i]) return false;
    }
    return false;
}
using namespace std;
int main() {
    int n;
    int i, j, k;
    scanf("%d", &n);
    int a[6];
    set s;
    for (i = 0; i < n; i++) {
        int m = 0, mi = 0;
        for (j = 0; j < 6; j++) {
            scanf("%d", &a[j]);
            if (a[j] > m) {m = a[j]; mi = j;}
        }
        snow tmpa;
        for (k = 0, j = mi; k < 6; k++) {
            tmpa.w[k] = a[j];
            if (j == 5) j = 0; 
            else j++;
        }
        snow tmpb;
        for (k = 0, j = mi; k < 6; k++) {
            tmpb.w[k] = a[j];
            if (j == 0) j = 5; 
            else j--;
        }
        if (s.find(tmpb) != s.end()) {
            cout << "Twin snowflakes found." << endl;
            return 0;
        }
        if (tmpa < tmpb) {
            if (s.find(tmpa) != s.end()) {
                cout << "Twin snowflakes found." << endl;
                return 0;
            }
            s.insert(tmpa);
        }
        else {
            if (s.find(tmpb) != s.end()) {
                cout << "Twin snowflakes found." << endl;
                return 0;
            }
            s.insert(tmpb);
        }
    }
    cout << "No two snowflakes are alike." << endl;
    return 0;
}

AC的代碼:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define oo 0x3f3f3f3f
#define MAX_N 30010
#define MAX_M 510
struct snow {
    int w[6];
};
snow s[100010];
bool operator < (const snow & a, const snow & b) {
    for (int i = 0; i < 6; i++) {
        if (a.w[i] < b.w[i]) return true;
        else if (a.w[i] > b.w[i]) return false;
    }
    return false;
}
bool operator == (const snow & a, const snow & b) {
    if (!(a m) {m = a[j]; mi = j;}
        }
        snow tmpa;
        for (k = 0, j = mi; k < 6; k++) {
            tmpa.w[k] = a[j];
            if (j == 5) j = 0; 
            else j++;
        }
        snow tmpb;
        for (k = 0, j = mi; k < 6; k++) {
            tmpb.w[k] = a[j];
            if (j == 0) j = 5; 
            else j--;
        }
        if (tmpa < tmpb) {
            s[i] = tmpa;
        }
        else {
            s[i] = tmpb;
        }
    }
    sort(s, s+n);
    for (i = 0; i <= n-2; i++) {
        if (s[i] == s[i+1]) 
           { cout << "Twin snowflakes found." << endl; return 0;}
    }
    cout << "No two snowflakes are alike." << endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章