topcoder srm 715 div1 -23
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的更多相关文章
- topcoder srm 692 div1 -23
1.给定一个带权有向图.选出一些边满足使得任意两点可相互到达的前提下使得选出的边的权值的最大最小差值最小. 思路:二分答案,然后枚举权值的范围判断是否可行. #include <stdio.h& ...
- topcoder srm 710 div1 -23
1.给定两个长度都为$n$的数组$A,B$,给出一个操作序列将$A$变成$B$.每个操作可以是以下两种之一:(1)选择一个$i,0\leq i <n$且$A_{i} \neq 0$,令$t=A_ ...
- 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 460 div1
problem1 link 设$f[i][j]$表示已经分配了answers中的前$i$个,分配给的问题的状态为 $j$的方案数. 其中状态可以用$n$位的三进制表示,0表示还未分配,1表示已分配是 ...
- topcoder srm 696 div1 -3
1.给定一个50个节点的无向图,有$m$条边.现在以任意一种序列对每个节点染色.染当前节点的代价为染色完当前节点后满足两个端点都被染色的边的数量.求最小的染色代价.$m \leq 20$ 思路:一个直 ...
- 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如果增加 ...
随机推荐
- ajax提交完表单数据依然跳转的解决办法
1. 既然ajax提交数据,就把表单里面submit按钮换掉,因为触发submit他就会跳转页面 提交的时候他会先触发ajax 再触发submit的提交 2.如果确定了表单没有submit,那么把提交 ...
- 原生js---ajax---get方法传数据
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- python 定义函数 调用函数
创建test.py文件 #coding=utf-8 #定义函数 def hello(): print "hello world" #调用函数 hello() 在cmd下面运行
- powerdesign、navacat、ER图、uml、类图、时序图
关于建表和生成实体以及ER图的简便方法 a:用navacat客户端生成简单的ER图,并生成建表sql,执行生成表. b:用powerdesign连接数据库,反向生成带有注释的ER图. c:用ideal ...
- windows假装更新升级
http://fakeupdate.net/ 进入这个网站,选择一款系统界面,按F11进去全屏 比较有趣
- 5.JVM的内存区域划分
一.JVM介绍 1. 什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟 ...
- jQuery事件--keypress([[data],fn])和trigger(type,[data])
keypress([[data],fn]) 概述 当键盘或按钮被按下时,发生 keypress 事件 keypress 事件与 keydown 事件类似.当按钮被按下时,会发生该事件.它发生在当前获得 ...
- Rigid Frameworks (画图二分图规律 + DP + 数学组合容斥)
题意:方格n*m,然后对于每一个格子有3种画法1左对角线2右对角线3不画,求让图形稳定的画法有多少种? 思路:通过手画二分图可以发现当二分图联通时改图满足条件,然后我们对于一个dp[n][m]可以利用 ...
- mybatis源码解析7---MappedStatement初始化过程
上一篇我们了解到了MappedStatement类就是mapper.xml中的一个sql语句,而Configuration初始化的时候会加载所有的mapper接口类,而本篇再分析下是如何将mapper ...
- JS中的函数节流throttle详解和优化
JS中的函数节流throttle详解和优化在前端开发中,有时会为页面绑定resize事件,或者为一个页面元素绑定拖拽事件(mousemove),这种事件有一个特点,在一个正常的操作中,有可能在一个短的 ...