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的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  3. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  4. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  5. Topcoder SRM 602 div1题解

    打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...

  6. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  7. Topcoder SRM 584 DIV1 600

    思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...

  8. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

  9. 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)其 ...

随机推荐

  1. MFC 显示图片

    //定义成员变量 CStatic m_picture; m_picture.Create(L"XXX",WS_VISIBLE|WS_CHILD|SS_BITMAP ,CRect(, ...

  2. DateTime.Compare(t1,t2)比较两个日期大小

    DateTime.Compare(t1,t2)比较两个日期大小,排前面的小,排在后面的大,比如:2011-2-1就小于2012-3-2返回值小于零:  t1 小于 t2. 返回值等于零 : t1 等于 ...

  3. 关于windows中80端口被占用

    很奇怪,windows7系统中的80端口被pid 为4 的system进程监听. 尝试关闭IIS功能,并在这期间进行过多次重启,均无效. 后来依照这篇文件解决的:https://www.jianshu ...

  4. [ English ] Ping sb.

    What does "Ping Me" mean?   Recently, when I asked a colleague to ping me, he responded wi ...

  5. Rpgmakermv(32) Yep_mainmenumanager

    ============================================================================ Introduction ========== ...

  6. Azure IoT 技术研究系列2-设备注册到Azure IoT Hub

    上篇博文中,我们主要介绍了Azure IoT Hub的基本概念.架构.特性: Azure IoT 技术研究系列1-入门篇 本文中,我们继续深入研究,做一个起步示例程序:模拟设备注册到Azure IoT ...

  7. Spring boot FastJson

    介绍:FastJson 是ailibaba 的一款解析Json的开源框架 使用方式1 引入jar 包 <dependency>    <groupId>com.alibaba& ...

  8. python 将文件描述符包装成文件对象

    有一个对应于操作系统上一个已打开的I/O 通道(比如文件.管道.套接字等)的整型文件描述符,你想将它包装成一个更高层的Python 文件对象. 一个文件描述符和一个打开的普通文件是不一样的.文件描述符 ...

  9. JSVC技术

    如果我们的某个项目时web项目,我们很容易就可以放置在Tomcat中进行启动. 可是如果我们的项目不是web项目,我们又需要在单独启动时,我们又应该怎么办呢?     引出了我们今天的主人公:JSVC ...

  10. 51nod 1130 N的阶乘的长度 V2(斯特林近似)

    输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3.   Input 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 1000) 第2 - T + ...