比赛链接:http://hihocoder.com/contest/hihointerview26
A.排序后枚举两个点,确定一个矩形后二分剩下两个点。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int maxn = 1010; 6 typedef struct Point { 7 int x, y; 8 Point() {} 9 Point(int xx, int yy) : x(xx), y(yy) {} 10 }Point; 11 int n; 12 LL ans; 13 Point p[maxn]; 14 15 bool cmp(Point a, Point b) { 16 if(a.x == b.x) return a.y < b.y; 17 return a.x < b.x; 18 } 19 20 bool bs(int x, int y) { 21 int ll = 0, rr = n, mm; 22 while(ll <= rr) { 23 mm = (ll + rr) >> 1; 24 if(p[mm].x == x && p[mm].y == y) return 1; 25 else if(cmp(p[mm], Point(x, y))) ll = mm + 1; 26 else rr = mm - 1; 27 28 } 29 return 0; 30 } 31 32 int main() { 33 // freopen("in", "r", stdin); 34 int x3, y3, x4, y4; 35 while(~scanf("%d", &n) && n) { 36 ans = 1000000LL * 1001000LL; 37 for(int i = 0; i < n; i++) { 38 scanf("%d%d", &p[i].x, &p[i].y); 39 } 40 sort(p, p+n, cmp); 41 for(int i = 0; i < n; i++) { 42 for(int j = i + 1; j < n; j++) { 43 x3 = p[i].x; y3 = p[j].y; 44 x4 = p[j].x; y4 = p[i].y; 45 if(bs(x3, y3) && bs(x4, y4)) { 46 int a = x3 - x4; 47 int b = y3 - y4; 48 if((LL)a * b == 0) continue; 49 ans = min(ans, abs((LL)a*b)); 50 } 51 } 52 } 53 printf("%lld ", ans == 1000000LL * 1001000LL ? -1 : ans); 54 } 55 return 0; 56 }
B.按题要求爆搜
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 10; 5 int n; 6 set<string> ret; 7 set<string>::iterator it; 8 int num[maxn]; 9 set<int> pos; 10 11 int main() { 12 // freopen("in", "r", stdin); 13 // freopen("out", "w", stdout); 14 for(int i = 1; i < maxn; i++) num[i] = i; 15 while(~scanf("%d", &n)) { 16 ret.clear(); 17 do { 18 int nn = (1 << (n)); 19 for(int i = 0; i < nn; i++) { 20 pos.clear(); 21 int tmp = i; 22 int cnt = 0; 23 while(tmp) { 24 if(tmp & 1) pos.insert(cnt+1); 25 tmp >>= 1; cnt++; 26 } 27 string t = ""; 28 bool ex = 0; 29 t += (num[1] + '0'); 30 int pre = num[1]; 31 for(int i = 2; i <= n; i++) { 32 if(ex) break; 33 if(pos.find(i) != pos.end()) { 34 t += '-'; 35 pre = -1; 36 } 37 if(pre > num[i]) { 38 ex = 1; 39 break; 40 } 41 t += (num[i] + '0'); 42 pre = num[i]; 43 } 44 if(!ex) ret.insert(t); 45 } 46 // for(int i = 1; i <= n; i++) printf("%d", num[i]); 47 // printf(" "); 48 }while(next_permutation(num+1, num+n+1)); 49 // printf("%d ", ret.size()); 50 for(it = ret.begin(); it != ret.end(); it++) { 51 cout << *it << endl; 52 } 53 } 54 return 0; 55 }
C.找到规律后斯特灵数胡搞(好像没必要?)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int maxn = 1010; 6 const LL mod = 1000000007LL; 7 LL f[maxn], a[maxn]; 8 LL S[maxn][maxn]; 9 int n; 10 11 LL exgcd(LL a, LL b, LL &x, LL &y) { 12 if(b == 0) { 13 x = 1; 14 y = 0; 15 return a; 16 } 17 else { 18 LL ret = exgcd(b, a%b, x, y); 19 LL tmp = x; 20 x = y; 21 y = tmp - a / b * y; 22 return ret; 23 } 24 } 25 26 LL inv(LL a) { 27 LL x, y; 28 exgcd(a, mod, x, y); 29 return (x % mod + mod) % mod; 30 } 31 32 LL C(LL n, LL m) { 33 return f[n] * inv(f[m]) % mod * inv(f[n-m]) % mod; 34 } 35 36 LL mul(LL x, LL n) { 37 LL ret = 1; 38 while(n) { 39 if(n & 1) ret = ret * x % mod; 40 n >>= 1; 41 x = x * x % mod; 42 } 43 return ret; 44 } 45 46 int main() { 47 // freopen("in", "r", stdin); 48 // freopen("out", "w", stdout); 49 memset(S, 0, sizeof(S)); 50 S[0][0] = 1; 51 for(int p = 1; p < maxn; p++) { 52 for(int k = 0; k < maxn; k++) { 53 S[p][k] = (LL)k * S[p-1][k] % mod + S[p-1][k-1] % mod; 54 } 55 } 56 f[0] = 1; 57 for(int i = 1; i < maxn; i++) { 58 f[i] = f[i-1] * (LL)i % mod; 59 } 60 memset(a, 0, sizeof(a)); 61 a[0] = 1; a[1] = 1; 62 for(int i = 2; i <= 1000; i++) { 63 for(int k = 1; k <= i; k++) { 64 a[i] = (a[i] + f[k] * S[i][k] % mod) % mod; 65 } 66 } 67 while(~scanf("%d", &n)) { 68 printf("%lld ", a[n]); 69 } 70 return 0; 71 }