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
刚开始用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; }