topcoder srm 640 div1 [FINISHED]

problem1 link

首先使用两个端点颜色不同的边进行连通, 假设现在的联通分量的个数是$m$, 那么答案是$n-m$。

problem2 link

首先假设$n_{1} \leq n_{2}$

首先构造一个最小割的模型。左边的$n_{1}$个点与源点相连,右边的$n_{2}$个点与汇点相连。小割为$ans$.

假设有$x$个割边出现在源点和$n_{1}$之间,那么$y=ans-x$个出现在$n_{2}$和汇点之间。

连线的规则是:左边的$x$个顶点与右侧的$n_{2}$个顶点都连边, 左侧剩下的$n_{1}-x$个顶点与右侧割边对应的$y$个顶点连边.

假设$ans=3,x=2,y=1$,那么连边如下图所示

首先,$x$需要满足的条件为$x\geq d,ans-x\geq d \Rightarrow d\leq x \leq ans-d$

其次,边的个数为$n_{2}x+(n_{1}-x)(ans-x)=x^{2}-(n_{1}+ans-n_{2})x+n_{1}a$

为了得到最大值,可以枚举$x$的各种边界值. 令$a=\left \lfloor \frac{n_{1}+ans-n_{2}}{2} \right \rfloor,b=a+1$.其实就是分类讨论$[a,b],[d,ans-d]$的关系.

problem3 link

如果两个数字$x,y$的差$x-y$能被数$p$整除,那么有$x\equiv y(mod(p))$。

所以可以对每个素数计算有那些数对$(A_{i},B_{j})$的余数相等,他们的差值就含有素数$p$。最后就剩下那些素数特别大的。

这个题目应该有几组特别***钻的测试数据,代码一直超时。

code for problem1

#include <vector>

class ChristmasTreeDecoration {
 public:
  int solve(const std::vector<int> &col, const std::vector<int> &x,
            const std::vector<int> &y) {
    int n = static_cast<int>(col.size());
    int m = static_cast<int>(x.size());
    father_.resize(n);
    for (int i = 0; i < n; ++i) {
      father_[i] = i;
    }
    int number = 0;
    for (int i = 0; i < m; ++i) {
      int u = x[i] - 1;
      int v = y[i] - 1;
      if (col[u] != col[v]) {
        int pu = GetRoot(u);
        int pv = GetRoot(v);
        if (pu != pv) {
          father_[pu] = pv;
          ++number;
        }
      }
    }
    return n - 1 - number;
  }

 private:
  int GetRoot(int x) {
    if (father_[x] == x) {
      return x;
    }
    return father_[x] = GetRoot(father_[x]);
  }

  std::vector<int> father_;
};

code for problem2

#include <algorithm>

class MaximumBipartiteMatchingProblem {
 public:
  long long solve(int n1, int n2, int ans, int d) {
    long long result = -1;
    auto Update = [&](int x) {
      if (x < d || ans - x < d) {
        return;
      }
      result = std::max(result, 1ll * n2 * x + 1ll * (n1 - x) * (ans - x));

    };

    if (n1 > n2) {
      std::swap(n1, n2);
    }
    if (n1 == ans) {
      return 1ll * n1 * n2;
    }
    Update(0);
    Update(d);
    Update(ans - d);
    Update(n1);
    Update(n2);
    Update((ans + n1 - n2) / 2);
    Update((ans + n1 - n2) / 2 + 1);
    return result;
  }
};

code for problem3

#include <algorithm>
#include <vector>

constexpr int kMax = 1000;
constexpr int kMaxPrime = 31622;

int diff[kMax][kMax];
int values[kMax][kMax];

int nxt[kMax];
int head[kMaxPrime];
int tag[kMaxPrime];

int prime_tag[kMaxPrime];

class TwoNumberGroups {
  static constexpr int kMod = 1000000007;

public:
  int solve(const std::vector<int> &A, const std::vector<int> &numA,
            const std::vector<int> &B, const std::vector<int> &numB) {
    int n = static_cast<int>(A.size());
    int m = static_cast<int>(B.size());
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        diff[i][j] = std::abs(A[i] - B[j]);
      }
    }
    int result = 0;
    for (int p = 2; p < kMaxPrime; ++p) {
      if (prime_tag[p] != 1) {
        for (int x = p + p; x < kMaxPrime; x += p) {
          prime_tag[x] = 1;
        }
        for (int i = 0; i < n; ++i) {
          int t = A[i] % p;
          if (tag[t] != p) {
            head[t] = -1;
            tag[t] = p;
          }
          nxt[i] = head[t];
          head[t] = i;
        }
        for (int i = 0; i < m; ++i) {
          int t = B[i] % p;
          if (tag[t] == p) {
            for (int j = head[t]; j != -1; j = nxt[j]) {
              if (A[j] != B[i]) {
                values[j][i] += p;
                while (diff[j][i] % p == 0) {
                  diff[j][i] /= p;
                }
              }
            }
          }
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (diff[i][j] > 1) {
          values[i][j] += diff[i][j];
        }
        result += 1ll * numA[i] * numB[j] % kMod * values[i][j] % kMod;
        if (result >= kMod) {
          result -= kMod;
        }
      }
    }
    return result;
  }
};
posted @ 2018-12-15 14:21  朝拜明天19891101  阅读(274)  评论(0编辑  收藏  举报