problem1 link

选择所有的'+'或者所有的‘-’,一定是这两种中的一种最大。

problem2 link

首先,第$n$个盘子初始时所在的柱子一定的最后所有的盘子都应该挪到的柱子。所以,可以枚举第$n$个盘子在哪个柱子上。

假设目前枚举第$n$个盘子在第三个柱子上,那么假设要求解的问题为 $F(k,x,y,z,3)$, 其中$x=count[0],y=count[1],z=count[2]-1$,表示处理剩下的$n-1=x+y+z$个盘子,三根柱子上的个数为$(x,y,z)$。

那么对于第$n-1$个盘子,如果也放到第三根柱子上,那么接下来就是求解$F(k,x,y,z-1,3)$。如果将其放置到第二根柱子上,那么要首先把把前面的$n-2$个盘子挪到第一根柱子上,然后挪$n-1$盘子,然后再把$n-2$个盘子挪到第三根上,这三步分别需要:$F(k^{'},x,y-1,z,1),1,2^{n-2}-1$ ,所以$k^{'}=k-2^{n-2}=k-2^{x+y+z-1}$,那么接下来要求解的是$F(k-2^{x+y+z-1},x,y-1,z,1)$。

按照上面的思路可以得到结论如果$k\geq 2^{x+y+z-1}$,那么此时的盘子一定要换一根柱子。

problem3 link

这个就是直接搜索就可以了。把原来的树构造出来。

code for problem1

#include <algorithm>
#include <string> class MaximumRange {
public:
int findMax(const std::string &s) {
int n = static_cast<int>(s.size());
int x = 0;
for (auto c : s) {
if (c == '+') {
++x;
}
}
return std::max(x, n - x);
}
};

code for problem2

#include <string>
#include <vector> class ClassicTowers {
public:
std::string findTowers(long long k, const std::vector<int> &count) {
if (Check(k, count[0], count[1], count[2], "ABC")) {
return result;
}
if (Check(k, count[0], count[2], count[1], "ACB")) {
return result;
}
if (Check(k, count[1], count[2], count[0], "BCA")) {
return result;
}
return "";
} private:
std::string result; std::vector<int> all; bool Dfs(long long k, int x, int y, int z, int idx) {
if (x + y + z == 0) {
return k == 0;
}
int curr = x + y + z - 1;
if ((k & (1ll << curr)) == 0) {
all[curr] = idx;
if (idx == 1 && x > 0) {
return Dfs(k, x - 1, y, z, idx);
} else if (idx == 2 && y > 0) {
return Dfs(k, x, y - 1, z, idx);
} else if (idx == 3 && z > 0) {
return Dfs(k, x, y, z - 1, idx);
} else {
return false;
}
} else {
for (int i = 1; i <= 3; ++i) {
if (i != idx) {
all[curr] = i;
if (i == 1 && x > 0 &&
Dfs(k ^ (1ll << curr), x - 1, y, z, 6 - idx - i)) {
return true;
}
if (i == 2 && y > 0 &&
Dfs(k ^ (1ll << curr), x, y - 1, z, 6 - idx - i)) {
return true;
}
if (i == 3 && z > 0 &&
Dfs(k ^ (1ll << curr), x, y, z - 1, 6 - idx - i)) {
return true;
}
}
}
return false;
}
} bool Check(long long k, int x, int y, int z, const std::string &table) {
if (z == 0) {
return false;
}
long long maxk = (1ll << (x + y + z - 1)) - 1;
if (k > maxk) {
return false;
}
all.resize(x + y + z);
all.back() = 3;
if (Dfs(k, x, y, z - 1, 3)) {
result = "";
for (int i = 0; i < x + y + z; ++i) {
result += table[all[i] - 1];
}
return true;
}
return false;
}
};

code for problem3

#include <algorithm>
#include <map>
#include <memory>
#include <string>
#include <vector> class PreInPost {
public:
std::vector<int> findMissing(const std::vector<std::string> &s,
const std::vector<int> &a1,
const std::vector<int> &a2,
const std::string &e1, const std::string &e2) {
table_s = s;
auto status = Dfs(a1, a2, e1, e2);
if (!status.flag) {
return {};
}
std::vector<int> result;
std::string mode = "pre";
for (int i = 0; i < 6; i += 2) {
if (s[i] != e1 && s[i] != e2) {
Order(status.root.get(), s[i], &result);
break;
}
}
return result;
} private:
struct Node {
int idx = -1;
std::shared_ptr<Node> left = nullptr;
std::shared_ptr<Node> right = nullptr; void SetRoot(int x) {
if (idx == -1) {
idx = x;
} else if (idx != x) {
idx = -2;
}
}
}; struct Status {
std::shared_ptr<Node> root = nullptr;
bool flag = false;
}; struct KeyNode {
std::vector<int> a1;
std::vector<int> a2;
std::string e1;
std::string e2; KeyNode() = default;
KeyNode(const std::vector<int> &a1, const std::vector<int> &a2,
const std::string &e1, const std::string &e2)
: a1(a1), a2(a2), e1(e1), e2(e2) {} bool operator<(const KeyNode &key) const {
if (a1 != key.a1) {
return a1 < key.a1;
}
if (a2 != key.a2) {
return a2 < key.a2;
}
if (e1 != key.e1) {
return e1 < key.e1;
}
return e2 < key.e2;
}
};
std::map<KeyNode, Status> visited_states;
std::vector<std::string> table_s; int LeftModeIdx(const std::string &e) {
if (e == "pre") {
return 0;
} else if (e == "in") {
return 2;
} else {
return 4;
}
} void Order(const Node *node, const std::string &e, std::vector<int> *result) {
if (node == nullptr) {
return;
}
if (e == "pre") {
result->push_back(node->idx);
Order(node->left.get(), table_s[0], result);
Order(node->right.get(), table_s[1], result);
} else if (e == "in") {
Order(node->left.get(), table_s[2], result);
result->push_back(node->idx);
Order(node->right.get(), table_s[3], result);
} else {
Order(node->left.get(), table_s[4], result);
Order(node->right.get(), table_s[5], result);
result->push_back(node->idx);
}
} bool SameSet(const std::vector<int> &a1, const std::vector<int> &a2) {
long long s[4] = {0, 0, 0, 0};
constexpr int kEach = 60;
for (size_t i = 0; i < a1.size(); ++i) {
s[a1[i] / kEach] ^= 1ll << (a1[i] % kEach);
s[a2[i] / kEach] ^= 1ll << (a2[i] % kEach);
}
return s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0;
} Status Dfs(const std::vector<int> &a1, const std::vector<int> &a2,
const std::string &e1, const std::string &e2) {
Status status;
if (a1.empty()) {
status.flag = true;
return status;
}
status.root = std::shared_ptr<Node>(new Node);
auto Set = [&](const std::vector<int> &a, const std::string &e) {
if (e == "pre" || e == "post") {
status.root->SetRoot(e == "pre" ? a.front() : a.back());
}
};
Set(a1, e1);
Set(a2, e2);
if (status.root->idx == -2) {
return status;
}
KeyNode key_node(a1, a2, e1, e2);
if (visited_states.find(key_node) != visited_states.end()) {
return visited_states[key_node];
}
std::vector<int> new_a1 = a1;
std::vector<int> new_a2 = a2;
int m = -1;
auto RemoveRoot = [&](const std::string &e, std::vector<int> *a) {
if (e == "pre") {
a->erase(a->begin());
} else if (e == "post") {
a->pop_back();
} else {
m = static_cast<int>(std::find(a->begin(), a->end(), status.root->idx) -
a->begin());
a->erase(a->begin() + m);
}
};
RemoveRoot(e1, &new_a1);
RemoveRoot(e2, &new_a2);
if (!SameSet(new_a1, new_a2)) {
return visited_states[key_node] = status;
}
int n = static_cast<int>(new_a1.size());
std::vector<int> right1;
std::vector<int> right2;
auto Check = [&]() {
if (SameSet(new_a1, new_a2) && SameSet(right1, right2)) {
Status left = Dfs(new_a1, new_a2, table_s[LeftModeIdx(e1)],
table_s[LeftModeIdx(e2)]);
Status right = Dfs(right1, right2, table_s[LeftModeIdx(e1) + 1],
table_s[LeftModeIdx(e2) + 1]);
if (left.flag && right.flag) {
status.root->left = left.root;
status.root->right = right.root;
status.flag = true;
return true;
}
}
return false;
}; if (m == -1) {
if (!Check()) {
for (int i = n - 1; i >= 0; --i) {
right1.insert(right1.begin(), new_a1.back());
right2.insert(right2.begin(), new_a2.back());
new_a1.pop_back();
new_a2.pop_back();
if (Check()) {
break;
}
}
}
} else {
for (int i = m; i < n; ++i) {
right1.push_back(new_a1[i]);
right2.push_back(new_a2[i]);
}
new_a1.erase(new_a1.begin() + m, new_a1.end());
new_a2.erase(new_a2.begin() + m, new_a2.end());
Check();
}
return visited_states[key_node] = status;
}
};

topcoder srm 715 div1 -23的更多相关文章

  1. topcoder srm 692 div1 -23

    1.给定一个带权有向图.选出一些边满足使得任意两点可相互到达的前提下使得选出的边的权值的最大最小差值最小. 思路:二分答案,然后枚举权值的范围判断是否可行. #include <stdio.h& ...

  2. topcoder srm 710 div1 -23

    1.给定两个长度都为$n$的数组$A,B$,给出一个操作序列将$A$变成$B$.每个操作可以是以下两种之一:(1)选择一个$i,0\leq i <n$且$A_{i} \neq 0$,令$t=A_ ...

  3. Topcoder SRM 643 Div1 250<peter_pan>

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

  4. Topcoder Srm 726 Div1 Hard

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

  5. topcoder srm 460 div1

    problem1 link 设$f[i][j]$表示已经分配了answers中的前$i$个,分配给的问题的状态为 $j$的方案数. 其中状态可以用$n$位的三进制表示,0表示还未分配,1表示已分配是 ...

  6. topcoder srm 696 div1 -3

    1.给定一个50个节点的无向图,有$m$条边.现在以任意一种序列对每个节点染色.染当前节点的代价为染色完当前节点后满足两个端点都被染色的边的数量.求最小的染色代价.$m \leq 20$ 思路:一个直 ...

  7. topcoder srm 714 div1

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

  8. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

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

  9. Topcoder SRM 602 div1题解

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

随机推荐

  1. Hello world!(内含自己编写的C语言二叉树同学录)

      修改:刷了一段时间的题,水平渐涨,发现同学录真的要做成市面可行的应用的话,应该按学号建立二叉平衡树,红黑树是一个可行的选择. 在同学的推荐下,来到博客园来找志同道合的人交流代码.3个月后参加蓝桥杯 ...

  2. Ajax-创建ajax

    UNSENT : 未发送,刚开始创建完成AJAX对象,默认的状态就是0 OPENED : 已打开,执行了xhr.open之后状态变为1 HEADERS_RECEIVED :响应头信息已经成功的返回并且 ...

  3. 【AngularJS】解决ng-if中的ng-model值无效的问题(转)

    from:http://blog.csdn.net/u013451157/article/details/60866210 与其他指令一样,ng-if指令也会创建一个子级作用域,因此,如果在ng-if ...

  4. sql语句,加引号和不加引号的区别

    今天碰到个问题,查询数据的时候,显示表不存在,在可视化窗口确实能看见.试着给表名加个引号,发现能成功查询数据了.上网查询原因如下: 1.oracle表和字段是有大小写的区别.oracle默认是大写,如 ...

  5. import caffe报错问题

    在搭建好的caffe环境下运行Python报错:ImportError:No module named _caffe 报错原因:由于caffe的Python环境变量未配置好 解决方案: 方法1 imp ...

  6. OWASP top 10

    OWASP Top 10 A1: InjectionSolution+Validate User Input+Never concatenate queries and date+Parameteri ...

  7. Lua 可变参数之arg与select

    function TestFunc(...) local arg = { ... } --Lua .2以后不再支持默认arg参数,{}与...之间要有空格 print("输入的参数个数:&q ...

  8. Sitecore CMS中配置项目图标

    在Sitecore中,图标通常用于通过各种不同的模板类型快速区分项目.文章可能使用红色图标,而列表页面可能使用蓝色.项目上设置的图标可以在内容树中看到,也可以在选择项目时在内容编辑器的顶部看到. 从功 ...

  9. Django admin模块无法调用css样式文件

    在使用Django Admin开发时,发现admin模块css样式文件丢失,无法调用,使火狐浏览器提示: 此 URL 的资源不是文本: http://127.0.0.1:8000/statics/ad ...

  10. GitHub 代码上传

    方法一 登录GitHub后,点击下面的图 New responsitory 按钮 或者点击绿色按钮 New repository,新建一个新建一个远程仓库(remote repository),点击后 ...