topcoder srm 679 div1
problem1 link
$f[u][0],f[u][1]$表示$u$节点表示的子树去掉和不去掉节点$u$的最大权值。
problem2 link
首先预处理计算任意三个蓝点组成的三角形中的蓝点个数以及是否包含红点。凸包可以分割成三角形。首先初始化凸包的三个顶点为$x,y,z$(假设$x,y,z$是逆时针),然后每次增加一个新的点进来,设最后加入凸包的两个点为$p_{1},p_{2}$,新加入的点为$q$,那么$q$需要满足的条件为:
(1)三角形$x,p_{2},q$中没有红点;
(2)$q$在射线$x,p_{2}$的左侧;
(3)$q$在射线$p_{1},p_{2}$的左侧;
(4)$q$在射线$x,y$的左侧.
这样进行动态规划即可。
problem3 link
令$s(i,j)=\sum_{0\leq k <m}count(i,k)[isGood_{j+k}]$,表示某个$bag$中取出的数字为$j$时,$bag$$i$中那些可以与$j$组成$good$的数字的方案数。
那么答案为$answer=\sum_{0\leq t<n}\sum_{0\leq j<m}count(t,j)\left (\sum_{t<i<n}s(i,j) \right )$
code for problem1
#include <algorithm>
#include <stack>
#include <vector> class FiringEmployees {
public:
int fire(const std::vector<int> &manager, const std::vector<int> &salary,
const std::vector<int> &productivity) {
int n = static_cast<int>(manager.size());
std::vector<std::vector<int>> g(n + 1);
std::vector<int> a(n + 1);
std::vector<int> f0(n + 1);
std::vector<int> f1(n + 1);
for (int i = 0; i < n; ++i) {
g[manager[i]].push_back(i + 1);
}
for (int i = 1; i <= n; ++i) {
f1[i] = a[i] = productivity[i - 1] - salary[i - 1];
}
std::stack<std::pair<int, int>> st;
st.push({0, 0});
while (!st.empty()) {
int u = st.top().first;
int tag = st.top().second;
st.pop();
if (tag == 1) {
for (auto son : g[u]) {
f1[u] += std::max(f0[son], f1[son]);
}
} else {
st.push({u, 1});
for (auto son : g[u]) {
st.push({son, 0});
}
}
}
return f1[0];
}
};
code for problem2
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector> namespace geometry { struct Point {
double x, y; Point(double x = 0.0, double y = 0.0) : x(x), y(y) {} Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); } Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); }
double operator*(const Point &a) const { return x * a.y - y * a.x; } double operator^(const Point &a) const { return x * a.x + y * a.y; }
Point operator*(double t) const { return Point(x * t, y * t); }
Point operator/(double t) const { return Point(x / t, y / t); }
}; int SGN(double x) {
constexpr double kEpslion = 1e-12;
if (x > kEpslion) {
return 1;
}
if (x < -kEpslion) {
return -1;
}
return 0;
} double Cross(const Point &a, const Point &b, const Point &p) {
return (b - a) * (p - a);
} bool Left(const Point &a, const Point &b, const Point &p) {
return ((b - a) * (p - a)) > 0;
} double Area(const Point &a, const Point &b, const Point &c) {
return std::fabs(Cross(a, b, c)) * 0.5;
} bool Inside(const Point &a, const Point &b, const Point &c, const Point &d) {
double s1 = Area(a, b, c);
double s2 = Area(a, b, d) + Area(a, c, d) + Area(b, c, d);
return SGN(s1 - s2) == 0;
} } // namespace geometry const int N = 50;
bool tag[N][N][N];
int f[N][N][N];
bool inque[N][N][N];
int g[N][N][N]; class RedAndBluePoints {
public:
int find(const std::vector<int> &blueX, const std::vector<int> &blueY,
const std::vector<int> &redX, const std::vector<int> &redY) {
int n = static_cast<int>(blueX.size());
if (n <= 2) {
return n;
}
std::vector<geometry::Point> p(n);
for (int i = 0; i < n; ++i) {
p[i] = geometry::Point(blueX[i], blueY[i]);
}
int m = static_cast<int>(redX.size());
std::vector<geometry::Point> q(m);
for (int i = 0; i < m; ++i) {
q[i] = geometry::Point(redX[i], redY[i]);
} auto Cal = [&](const geometry::Point &a, const geometry::Point &b,
const geometry::Point &c) {
for (int i = 0; i < m; ++i) {
if (geometry::Inside(a, b, c, q[i])) {
return true;
}
}
return false;
}; for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
if (i != j && i != k && j != k) {
tag[i][j][k] = Cal(p[i], p[j], p[k]);
for (int t = 0; t < n; ++t) {
if (t != i && t != j && t != k &&
geometry::Inside(p[i], p[j], p[k], p[t])) {
++g[i][j][k];
}
}
}
}
}
} int result = 2;
for (int i = 0; i < n; ++i) {
result = std::max(result, Get(i, p));
}
return result;
} private:
int Get(int x, const std::vector<geometry::Point> &p) {
int n = static_cast<int>(p.size()); struct Node {
int i, j, t, cnt; Node() = default;
Node(int i, int j, int t, int cnt) : i(i), j(j), t(t), cnt(cnt) {}
}; memset(f, 0, sizeof(f));
memset(inque, 0, sizeof(inque)); std::queue<Node> que;
int result = 0; for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != x && i != j && j != x && !tag[x][i][j] &&
geometry::Left(p[x], p[i], p[j])) {
f[i][i][j] = 3 + g[x][i][j];
result = std::max(result, f[i][i][j]);
inque[i][i][j] = true;
que.push(Node(i, j, i, f[i][i][j]));
}
}
}
while (!que.empty()) {
Node node = que.front();
que.pop();
int y = node.t;
int p1 = node.i;
int p2 = node.j;
inque[y][p1][p2] = false; for (int q = 0; q < n; ++q) {
if (q == x || q == y || q == p1 || q == p2 || tag[x][p2][q]) {
continue;
}
if (geometry::Left(p[p1], p[p2], p[q]) &&
geometry::Left(p[x], p[p2], p[q]) &&
geometry::Left(p[x], p[y], p[q])) {
int sum = node.cnt + 1 + g[p2][q][x];
if (f[y][p2][q] < sum) {
f[y][p2][q] = sum;
result = std::max(result, sum);
if (!inque[y][p2][q]) {
inque[y][p2][q] = true;
que.push(Node(p2, q, y, sum));
}
}
}
}
}
return result;
}
};
code for problem3
#include <string>
#include <vector> class BagAndCards {
public:
int getHash(int n, int m, int x, int a, int b, int c,
const std::string &isGood) {
constexpr int kMod = 1000000007;
std::vector<std::vector<int>> f(n, std::vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
f[i][j] = x;
x = static_cast<int>(((1ll * x * a + b) ^ c) % kMod);
}
}
auto Add = [&](int &x, int y) {
x += y;
if (x >= kMod) {
x -= kMod;
}
};
int result = 0;
for (int j = 0; j < n; ++j) {
std::vector<int> s(m, 0);
for (int i = 0; i < m; ++i) {
for (int t = 0; t < m; ++t) {
if (isGood[i + t] == 'Y') {
Add(s[i], f[j][t]);
}
}
}
for (int i = 0; i < j; ++i) {
int sum = 0;
for (int k = 0; k < m; ++k) {
Add(sum, static_cast<int>(1ll * f[i][k] * s[k] % kMod));
}
result ^= sum;
}
}
return result;
}
};
topcoder srm 679 div1的更多相关文章
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- topcoder srm 714 div1
problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- Topcoder SRM 602 div1题解
打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
Problem Statement The Happy Letter game is played as follows: At the beginning, several players ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
- topcoder srm 575 div1
problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...
随机推荐
- Linux 服务器配置网站以及绑定域名
Linux 服务器如何配置网站以及绑定域名 转载来源:http://www.xinnet.com/service/cjwt/idc/guanli/1424.html 以下列举一些 主机上常见的 Web ...
- 启用了不安全的HTTP方法【转】
安全风险: 可能会在Web 服务器上上载.修改或删除Web 页面.脚本和文件. 可能原因: Web 服务器或应用程序服务器是以不安全的方式配置的. 修订建议: 如果 ...
- 【转载】C# 中的委托和事件(详解:简单易懂的讲解)
本文转载自http://www.cnblogs.com/SkySoot/archive/2012/04/05/2433639.html C# 中的委托和事件(详解) C# 中的委托和事件 委托和事件在 ...
- JAVA多线程常用例子
详情见该页面ttps://www.cnblogs.com/pureEve/p/6524366.html
- .NET 黑魔法 - asp.net core 身份认证 - Policy
身份认证几乎是每个项目都要集成的功能,在面向接口(Microservice)的系统中,我们需要有跨平台,多终端支持等特性的认证机制,基于token的认证方式无疑是最好的方案.今天我们就来介绍下在.Ne ...
- TP文件上传
一.单文件上传 <form action="__ACTION__" enctype="multipart/form-data" method=" ...
- github上删除一个项目或者reposity
1,点击github的头像,选择如下操作. 2.选择要删除的reposity 3.选择settings 4.复制reposity名字,然后下滑鼠标到底部,选择delete this reposity ...
- OWASP top 10
OWASP Top 10 A1: InjectionSolution+Validate User Input+Never concatenate queries and date+Parameteri ...
- uvm设计分析——factory
uvm的factory机制,通过实例一个static类型default factory,并且通过宏将所有例化extend出来的object,component register到该factory的内部 ...
- 16. 3Sum Closest(双指针)
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...