[关闭]
@cww97 2017-12-14T22:00:20.000000Z 字数 3887 阅读 860

骚猪代码仓库v2补充包

ACM


线段相交

  1. struct point{
  2. double x, y;
  3. point():x(0), y(0){}
  4. point(double _x, double _y): x(_x),y(_y){}
  5. inline void read(){
  6. scanf("%lf%lf", &x, &y);
  7. }
  8. point operator + (const point &a) const {
  9. return point(x+a.x, y+a.y);
  10. }
  11. point operator - (const point &a) const {
  12. return point(x-a.x, y-a.y);
  13. }
  14. point operator * (const double &k) const {
  15. return point(x * k, y * k);
  16. }
  17. double operator ^ (const point &a) const { // det
  18. return x*a.y - y*a.x;
  19. }
  20. double dis(const point &b) {
  21. return (x-b.x)*(x-b.x) + (y-b.y)*(y-b.y);
  22. }
  23. inline void print(){
  24. printf("point: (%.2lf, %.2lf)\n", x, y);
  25. }
  26. bool onSeg(point P1, point P2){
  27. point Q = point(x, y);
  28. if (min(P1.x,P2.x) > Q.x || Q.x > max(P1.x,P2.x)) return false;
  29. if (min(P1.y,P2.y) > Q.y || Q.y > max(P1.y,P2.y)) return false;
  30. double ans = fabs((Q - P1) ^ (Q - P2));
  31. //printf("??? = %.4lf\n", ans);
  32. if (fabs((Q - P1) ^ (Q - P2)) > 0) return false;
  33. return true;
  34. }
  35. } A, B, C;
  36. double s(point p, point a, point b){
  37. return fabs((p - a) ^ (p - b));
  38. }
  39. bool inTra(point P, point A, point B, point C){
  40. double s1 = s(P, A, B), s2 = s(P, B, C), s3 = s(P, C, A);
  41. if (s1 == 0 || s2 == 0 || s3 == 0) return false;
  42. double a = s1 + s2 + s3;
  43. double b = s(A, B, C);
  44. //printf("%.2lf\n", a-b);
  45. return fabs(a - b) == 0.;
  46. }
  47. bool segXseg(point p1, point p2, point p3, point p4){
  48. // attention, not good
  49. if (p1.onSeg(p3, p4) || p2.onSeg(p3, p4)) return false;
  50. if (p3.onSeg(p1, p2) || p4.onSeg(p1, p2)) return false;
  51. //---------------------------------------------
  52. if (max(p1.x, p2.x) < min(p3.x, p4.x)) return 0;
  53. if (max(p1.y, p2.y) < min(p3.y, p4.y)) return 0;
  54. if (max(p3.x, p4.x) < min(p1.x, p2.x)) return 0;
  55. if (max(p3.y, p4.y) < min(p1.y, p2.y)) return 0;//ju xin shi yan
  56. double x = (p3 - p1) ^ (p2 - p1);
  57. double y = (p4 - p1) ^ (p2 - p1);
  58. double z = (p1 - p3) ^ (p4 - p3);
  59. double w = (p2 - p3) ^ (p4 - p3); //KuaLi
  60. return x*y <= 0 && z*w <= 0;
  61. }
  62. bool xj(point p1, point p2, point A, point B, point C){ //xiangjiao
  63. //for (double k = 0.01; k < 1; k += 0.01) // seg & tra
  64. //if (inTra((p1*k + p2*(1-k)), A, B, C)) return true;
  65. if (inTra(p1*0.01 + p2*0.99, A, B, C)) return true;
  66. if (inTra(p1*0.99 + p2*0.01, A, B, C)) return true;
  67. if (segXseg(p1, p2, A, B)) return true;
  68. if (segXseg(p1, p2, A, C)) return true;
  69. if (segXseg(p1, p2, C, B)) return true;
  70. return false;
  71. }

匈牙利

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N = 200 + 7;
  4. const int INF = 0x3f3f3f3f;
  5. struct Hungary{
  6. vector <int> G[N];
  7. bool used[N];// main里面记得memset
  8. int girl[N], n;
  9. inline void init(int _n){
  10. n = _n;
  11. for (int i = 0; i <= n; i++) G[i].clear();
  12. }
  13. inline void addEdge(const int &u, const int &v){
  14. G[u].push_back(v);
  15. //printf("addEdge %d %d\n", u, v);
  16. }
  17. bool Find(int x){
  18. for (int i = 0; i < G[x].size(); i++){ //扫描每个妹子
  19. int j = G[x][i];
  20. if (used[j]) continue;
  21. // 如果有暧昧并且还没有标记过
  22. // (这里标记的意思是这次查找曾试图改变过该妹子的归属问题,
  23. // 是没有成功,所以就不用瞎费工夫了)
  24. used[j] = 1;
  25. if (girl[j] == -1 || Find(girl[j])) {
  26. //名花无主或者能腾出个位置来,这里使用递归
  27. girl[j] = x;
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. inline int hungary(const int &n, const int &m){
  34. int all = 0;
  35. memset(girl, -1, sizeof girl);
  36. for (int i = 0; i < n; i++) {
  37. memset(used, 0, sizeof(used)); //这个在每一步中清空
  38. if (Find(i)) all += 1;
  39. }
  40. //for (int i = 0; i < m; i++) printf("girl[%d] = %d\n", i, girl[i]);
  41. //printf("all = %d\n", all);
  42. return all;
  43. }
  44. } hg;

一个哈希版和一个stdio

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const LL MAXN = 5e4 + 5;
  5. const LL MAXNN = 3e5 + 5;
  6. const LL base = 29;
  7. const LL base2 = 31;
  8. const LL MOD = 1e9 + 7;
  9. LL p[MAXNN], Hash[MAXN], len[MAXN];
  10. string s[MAXN];
  11. vector<LL> vec[MAXN];
  12. LL getval(LL i, LL j, LL m) {
  13. return ((vec[i][j+m] - vec[i][j] * p[m]) % MOD + MOD) % MOD;
  14. }
  15. bool ok(int m, int n) {
  16. if(m > len[0]) return false;
  17. set<LL> myset;
  18. for(int i = 1; i < n; i++) {
  19. if(len[i] < m) continue;
  20. for(int j = 0; j <= len[i] - m; j++) {
  21. myset.insert(getval(i, j, m));
  22. }
  23. }
  24. for(int i = 0; i <= len[0] - m; i++) {
  25. if(myset.find(getval(0, i, m)) == myset.end()) return true;
  26. }
  27. return false;
  28. }
  29. void init() {
  30. p[0] = 1;
  31. for(LL i = 1; i < MAXNN; i++) {
  32. p[i] = (p[i - 1] * base) % MOD;
  33. }
  34. }
  35. int main(){
  36. std::ios::sync_with_stdio(false);
  37. init();
  38. LL _;
  39. cin >> _;
  40. for (LL n, cas = 1; cas <= _; cas++){
  41. cin >> n;
  42. for(int i = 0; i <= n; i++) vec[i].clear();
  43. for(LL i = 0; i < n; i++) {
  44. cin >> s[i];
  45. len[i] = s[i].length();
  46. LL tmp = 0;
  47. vec[i].push_back(0);
  48. for(int j = 0; j < len[i]; j++) {
  49. tmp = (tmp * base + s[i][j] - 'a') % MOD;
  50. vec[i].push_back(tmp);
  51. }
  52. }
  53. LL l = 0, r = len[0] + 1, mc = log(len[0]) / log(2) + 2;
  54. for(LL i = 0; i < mmc; i++) {
  55. LL m = (l + r) / 2;
  56. if(ok(m, n)) r = m;
  57. else l = m;
  58. }
  59. if(r > len[0]) cout << "Impossible" << endl;
  60. else cout << getans(r, n) << endl;
  61. }
  62. return 0;
  63. }

双指针尺取法

  1. int cal(int n, int m, char c) {
  2. int ans = 0;
  3. int cnt = 0;
  4. int l = 0, k = 0, mx = 0;
  5. for(int r = 0; r < n; r++) {
  6. if(s[r] != c) k++;
  7. while(k > m) {
  8. if(s[l++] != c) k--;
  9. }
  10. mx = max(mx, r - l + 1);
  11. }
  12. return mx;
  13. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注