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. js判断当前页面是否有父页面,页面部分跳转解决办法,子页面跳转父页面不跳转解决 (原)

    //如果当前页面存在父页面,则当前页面的父页面重新加载(即子页面父页面连带跳转) if(top.location!=self.location){         window.parent.loca ...

  2. 发布网站配置文件和SSL

    1.将cert下新建一个文件将所有证书文件放在新建的文件下 例如:cert/medcard 2.配置网站的.conf文件 <VirtualHost *:443> ServerName ww ...

  3. laravel中使用event

    https://www.cnblogs.com/ziyouchutuwenwu/p/4274539.html

  4. Unity shader学习之标准的Unity shader

    包含光照,可处理多个光源,有光照衰减和阴影的shader,代码如下: 转载请注明出处:http://www.cnblogs.com/jietian331/p/7199311.html Shader & ...

  5. 擠出線寬(Extrusion width),要怎麼設定?

    擠出線寬(Extrusion width),要怎麼設定? Slic3r的作者,把這邊的%設定,跟"層高"做連結.我個人認為擠出線寬,要以噴頭孔徑當做設定參考才好.層高應該只要設定成 ...

  6. How to export a model from SolidWorks to Google SketchUp

    How to export a model from SolidWorks to Google SketchUp While Google SketchUp is not a professional ...

  7. mysql 对表字段进行长度截取操作

    现在的问题是数据库某一个表中其中的车牌号字段有些数据多了一位,需要把它找出来然后把多的最后一位去掉..... 通过自带的length(字段名)函数把长度过长的数据过滤出来,其中,一个汉字算3个字符,一 ...

  8. 第三方包源码maven 下载

    1,maven导包时候自动下载源码 在eclipse或者Myeclipse中 => window ==> preference ==> Maven ==> 勾选Download ...

  9. SQLServer 创建自己的数据库

    1)进入数据库服务器,创建自己的数据库 use master go create database Dt_Devtest on primary(name=[Dt_new_data],filename= ...

  10. [转载]SQL中EXISTS的用法

    比如在Northwind数据库中有一个查询为SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(SELECT OrderID FR ...