topcoder srm 689 div1 -3
problem1 link
按照每种字符的数量降序排序,然后从多到少依次放每一种。放的时候一上一下交错放置。
problem2 link
构造的方法如下:(假设$x=25$)
(1)首先构造一个初始答案如下:

现在的'good'子集的个数为15,还需要25-15=10个。下面的每一步添加将不改变之前确定的'good'子集,只会增加新的'good'子集;此时$n=4$
(2)添加一层得到$n=5$

新添加的8个是这样的:从$[0,2]$中任意选出一个集合跟3,4都可以构成一个新的good'集合
(3)再添加一层得到$n=6$

新添加的2个是这样的:从$[0,0]$中任意选出一个集合跟1,2,3,4,5都可以构成一个新的good'集合
problem3 link
每次随机旋转某个角度。 然后假设按照$x$排序前一半跟后一半匹配。每次计算一个凸包,找到一组匹配。去掉这组匹配, 继续计算剩下的凸包。循环这个过程。
code for problem1
#include <algorithm>
#include <string>
#include <vector> class ColorfulGarden {
public:
std::vector<std::string> rearrange(const std::vector<std::string> &A) {
std::vector<std::pair<int, int>> a(26);
for (int i = 0; i < 26; ++i) {
a[i].first = 0;
a[i].second = i;
}
for (const auto &s : A) {
for (char c : s) {
++a[c - 'a'].first;
}
}
std::sort(a.begin(), a.end(), std::greater<std::pair<int, int>>());
int n = static_cast<int>(A[0].size());
if (a[0].first > n) {
return {};
} std::string total; for (const auto &e : a) {
char c = 'a' + e.second;
for (int i = 0; i < e.first; ++i) {
total += c;
}
}
std::string s1 = A[0], s2 = A[1];
for (int i = 0; i < n; ++i) {
if (i % 2 == 1) {
s1[i] = total[i];
} else {
s2[i] = total[i];
}
}
for (int i = n; i < n + n; ++i) {
if ((i - n) % 2 == 1) {
s2[i - n] = total[i];
} else {
s1[i - n] = total[i];
}
}
return {s1, s2};
}
};
code for problem2
#include <algorithm>
#include <vector> class MultiplicationTable3 {
public:
std::vector<int> construct(int x) {
constexpr int kMaxN = 20;
int a[kMaxN][kMaxN];
int n = Get(x);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
a[i][j] = i;
}
}
x -= (1 << n) - 1;
while (x != 0) {
int k = Get(x);
for (int j = n; j >= k; --j) {
a[n][j] = a[j][n] = j - 1;
}
a[n][k] = a[k][n] = k;
for (int j = k - 1; j >= 0; --j) {
a[n][j] = a[j][n] = n;
}
++n;
x -= 1 << k;
}
std::vector<int> ans;
for (int i = 0; i < n; ++i) {
std::copy(a[i], a[i] + n, std::back_inserter(ans));
}
return ans;
} private:
int Get(int x) {
for (int i = 20; i >= 0; --i) {
if ((x & (1 << i)) != 0) {
return i;
}
}
return 0;
}
};
code for problem3
#include <algorithm>
#include <queue>
#include <vector> class ZeroPointSixThreeSix {
public:
std::vector<int> replan(const std::vector<int> &x, const std::vector<int> &y,
const std::vector<int> &match) {
int n = static_cast<int>(x.size());
srand(time(0));
std::vector<Point> points(n);
long double cost = 0;
for (int i = 0; i < n; i++) {
points[i].x = x[i];
points[i].y = y[i];
if (match[i] < i) {
cost += points[match[i]].Distance(points[i]);
}
}
std::vector<int> result(n);
cost = cost * 0.636;
while (true) {
double angle = 2 * M_PI / (rand() + 1) * rand();
std::vector<std::pair<Point, int>> p(n);
for (int i = 0; i < n; ++i) {
p[i].first = points[i].Rotate(angle);
p[i].second = i;
}
std::sort(
p.begin(), p.end(),
[&](const std::pair<Point, int> &a, const std::pair<Point, int> &b) {
return std::make_pair(a.first.x, a.first.y) <
std::make_pair(b.first.x, b.first.y);
});
std::vector<bool> used(n, false);
int num = 0;
double total = 0;
while (num != n) {
std::vector<int> st;
for (int i = 0; i < n; ++i) {
if (used[i]) {
continue;
}
while (st.size() > 1 &&
(p[st[st.size() - 1]].first - p[st[st.size() - 2]].first) *
(p[i].first - p[st[st.size() - 2]].first) <
0) {
st.pop_back();
}
st.emplace_back(i);
}
if (st.empty()) {
break;
}
for (size_t i = 0; i + 1 < st.size(); ++i) {
if (st[i] < n / 2 && st[i + 1] >= n / 2) {
used[st[i]] = used[st[i + 1]] = true;
result[p[st[i]].second] = p[st[i + 1]].second;
result[p[st[i + 1]].second] = p[st[i]].second;
num += 2;
total += p[st[i]].first.Distance(p[st[i + 1]].first);
break;
}
}
}
if (total > cost) {
break;
}
}
return result;
} private:
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {} double Distance(const Point &p) const {
return std::sqrt(std::pow(x - p.x, 2) + std::pow(y - p.y, 2));
}
Point Rotate(double ang) {
double s = sin(ang), c = cos(ang);
return Point(x * c - y * s, x * s + y * c);
}
double operator*(const Point &p) const { return x * p.y - y * p.x; }
Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }
Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); }
};
};
topcoder srm 689 div1 -3的更多相关文章
- 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)其 ...
随机推荐
- (4)Python3笔记 之 流程控制
一.条件控制 # 语法规则 if 变量(或表达式): 语句块1 elif 变量(或表达式): 语句块2 else: 语句块3 #示例 score = 83 if score > 90: prin ...
- Bootstrap-全局样式的文本颜色和背景颜色
.text-五种颜色 文本颜色.text-info文本浅蓝颜色-提示.text-warning文本黄色-警告颜色.text-success文本绿色-成功颜色.text-primary文本深蓝色-警 ...
- Unity shader学习之半兰伯特光照模型
半兰伯特光照模型,为Valve公司在开发游戏<半条命>时提出的一种技术,用于解决漫反射光无法到达区域无任凭明暗变化,丢失模型细节表现的问题. 其公式如下: Cdiffuse = Cligh ...
- Web界面进行用户管理
Web界面进行用户管理 添加用户 Tags:表示账号的角色 Admin:超级管理员 No access :表示没有可以访问的virtual host虚拟机(相当于数据库) ...
- kali linux wifi破解(aircrack)
需要一个能监听的网卡 airmon-ng start wlan0(監聽網卡) airmon-ng check kill(清除其他有影响的環境) airodump-ng mon0 (掃描附近wifi) ...
- centos python2升级为python3 升级旧版本django
阿里云centos python3 及django的配置 安装python3后 pip 会把下载的包安入python2.7 下面解决该问题 python3.5安装1,安装依赖包#yum install ...
- cocos 搭建安卓环境
http://blog.csdn.net/yiye3376/article/details/42219889
- Hinton“深度学习之父”和“神经网络先驱”,新论文Capsule将推翻自己积累了30年的学术成果时
Hinton“深度学习之父”和“神经网络先驱”,新论文Capsule将推翻自己积累了30年的学术成果时 在论文中,Capsule被Hinton大神定义为这样一组神经元:其活动向量所表示的是特定实体类型 ...
- Codeforces 937A - Olympiad
A. Olympiad 题目链接:http://codeforces.com/problemset/problem/937/A time limit per test 1 second memory ...
- [转载]ViewPort <meta>标记
ViewPort <meta>标记用于指定用户是否可以缩放Web页面,如果可以,那么缩放到的最大和最小缩放比例是什么.使用 ViewPort <meta>标记还表示文档针对移动 ...