problem1 link

如果两个循环之内可以跳完,那么我们只要让这些步数之内的数字组成两个数字$p,q,p\leq q$,使得$p,q,x$组成三角形即可($p+q\geq x,p+x\geq q$)。

否则,若$x$是所有数字之和的很多倍,则一开始是一直直着向前跳$m$次,剩下$r=x-m\sum_{i=0}^{n-1}t_{i}$,然后找到一个前缀和大于等于$r$即可。

problem2 link

对于某个节点$u$,假如最后一定选择该节点,那么对于节点$p$,若选它,那么$p$到$u$路径上的点都必须选,这样就成了一个最大权闭合图问题。可以用最小割来计算。

problem3 link

对于不同的素数,可以分开考虑。

对于某一个素数p:用$min[i],max[i]$计算$n$个数字中每个数字最少最多含有多少个$p$。然后第$x$个数字含有的$p$要么取$min[x]$,要么取$max[x]$,因此可用2sat解决。

code for problem1

  1. #include <algorithm>
  2. #include <vector>
  3.  
  4. class PeriodicJumping {
  5. public:
  6. int minimalTime(int x, const std::vector<int> &jumps) {
  7. std::vector<int> copys = jumps;
  8. int n = static_cast<int>(copys.size());
  9. for (int i = 0; i < n; ++i) {
  10. copys.emplace_back(copys[i]);
  11. }
  12. n *= 2;
  13. if (x < 0) {
  14. x *= -1;
  15. }
  16. if (x == 0) {
  17. return 0;
  18. }
  19. long long s = 0;
  20. int max_jump = 0;
  21. for (int i = 0; i < n; ++i) {
  22. max_jump = std::max(max_jump, copys[i]);
  23. s += copys[i];
  24. long long min = std::max(0ll, max_jump - (s - max_jump));
  25. if (min <= x && x <= s) return i + 1;
  26. }
  27. int result = static_cast<int>(x / s * n);
  28. int remain = x % s;
  29. for (int i = 0; i < n && remain > 0; ++i) {
  30. remain -= copys[i];
  31. ++result;
  32. }
  33. return result;
  34. }
  35. };

code for problem2

  1. #include <limits>
  2. #include <memory>
  3. #include <unordered_map>
  4. #include <vector>
  5.  
  6. template <typename FlowType>
  7. class MaxFlowSolver {
  8. static constexpr FlowType kMaxFlow = std::numeric_limits<FlowType>::max();
  9. static constexpr FlowType kZeroFlow = static_cast<FlowType>(0);
  10. struct node {
  11. int v;
  12. int next;
  13. FlowType cap;
  14. };
  15.  
  16. public:
  17. int VertexNumber() const { return used_index_; }
  18.  
  19. FlowType MaxFlow(int source, int sink) {
  20. source = GetIndex(source);
  21. sink = GetIndex(sink);
  22.  
  23. int n = VertexNumber();
  24. std::vector<int> pre(n);
  25. std::vector<int> cur(n);
  26. std::vector<int> num(n);
  27. std::vector<int> h(n);
  28. for (int i = 0; i < n; ++i) {
  29. cur[i] = head_[i];
  30. num[i] = 0;
  31. h[i] = 0;
  32. }
  33. int u = source;
  34. FlowType result = 0;
  35. while (h[u] < n) {
  36. if (u == sink) {
  37. FlowType min_cap = kMaxFlow;
  38. int v = -1;
  39. for (int i = source; i != sink; i = edges_[cur[i]].v) {
  40. int k = cur[i];
  41. if (edges_[k].cap < min_cap) {
  42. min_cap = edges_[k].cap;
  43. v = i;
  44. }
  45. }
  46. result += min_cap;
  47. u = v;
  48. for (int i = source; i != sink; i = edges_[cur[i]].v) {
  49. int k = cur[i];
  50. edges_[k].cap -= min_cap;
  51. edges_[k ^ 1].cap += min_cap;
  52. }
  53. }
  54. int index = -1;
  55. for (int i = cur[u]; i != -1; i = edges_[i].next) {
  56. if (edges_[i].cap > 0 && h[u] == h[edges_[i].v] + 1) {
  57. index = i;
  58. break;
  59. }
  60. }
  61. if (index != -1) {
  62. cur[u] = index;
  63. pre[edges_[index].v] = u;
  64. u = edges_[index].v;
  65. } else {
  66. if (--num[h[u]] == 0) {
  67. break;
  68. }
  69. int k = n;
  70. cur[u] = head_[u];
  71. for (int i = head_[u]; i != -1; i = edges_[i].next) {
  72. if (edges_[i].cap > 0 && h[edges_[i].v] < k) {
  73. k = h[edges_[i].v];
  74. }
  75. }
  76. if (k + 1 < n) {
  77. num[k + 1] += 1;
  78. }
  79. h[u] = k + 1;
  80. if (u != source) {
  81. u = pre[u];
  82. }
  83. }
  84. }
  85. return result;
  86. }
  87.  
  88. MaxFlowSolver() = default;
  89.  
  90. void Clear() {
  91. edges_.clear();
  92. head_.clear();
  93. vertex_indexer_.clear();
  94. used_index_ = 0;
  95. }
  96.  
  97. void InsertEdge(int from, int to, FlowType cap) {
  98. from = GetIndex(from);
  99. to = GetIndex(to);
  100. AddEdge(from, to, cap);
  101. AddEdge(to, from, kZeroFlow);
  102. }
  103.  
  104. private:
  105. int GetIndex(int idx) {
  106. auto iter = vertex_indexer_.find(idx);
  107. if (iter != vertex_indexer_.end()) {
  108. return iter->second;
  109. }
  110. int map_idx = used_index_++;
  111. head_.push_back(-1);
  112. return vertex_indexer_[idx] = map_idx;
  113. }
  114.  
  115. void AddEdge(int from, int to, FlowType cap) {
  116. node p;
  117. p.v = to;
  118. p.cap = cap;
  119. p.next = head_[from];
  120. head_[from] = static_cast<int>(edges_.size());
  121. edges_.emplace_back(p);
  122. }
  123.  
  124. std::vector<node> edges_;
  125. std::vector<int> head_;
  126.  
  127. std::unordered_map<int, int> vertex_indexer_;
  128. int used_index_ = 0;
  129. };
  130.  
  131. class DoubleTree {
  132. public:
  133. int maximalScore(const std::vector<int> &a, const std::vector<int> &b,
  134. const std::vector<int> &c, const std::vector<int> &d,
  135. const std::vector<int> &score) {
  136. int n = static_cast<int>(a.size() + 1);
  137. std::vector<std::vector<int>> g1(n);
  138. std::vector<std::vector<int>> g2(n);
  139. for (int i = 0; i < n - 1; ++i) {
  140. g1[a[i]].push_back(b[i]);
  141. g1[b[i]].push_back(a[i]);
  142. g2[c[i]].push_back(d[i]);
  143. g2[d[i]].push_back(c[i]);
  144. }
  145.  
  146. constexpr int kInfiniteFlow = 1000000;
  147. std::unique_ptr<MaxFlowSolver<int>> solver(new MaxFlowSolver<int>());
  148. int result = 0;
  149. for (int root = 0; root < n; ++root) {
  150. std::vector<int> father1(n);
  151. std::vector<int> father2(n);
  152. Dfs(root, -1, father1, g1);
  153. Dfs(root, -1, father2, g2);
  154. solver->Clear();
  155. int source = -1;
  156. int sink = -2;
  157. int s = 0;
  158. for (int i = 0; i < n; ++i) {
  159. if (score[i] > 0) {
  160. s += score[i];
  161. solver->InsertEdge(source, i, score[i]);
  162. } else {
  163. solver->InsertEdge(i, sink, -score[i]);
  164. }
  165. if (i != root) {
  166. solver->InsertEdge(i, father1[i], kInfiniteFlow);
  167. solver->InsertEdge(i, father2[i], kInfiniteFlow);
  168. }
  169. }
  170. result = std::max(result, s - solver->MaxFlow(source, sink));
  171. }
  172. return result;
  173. }
  174.  
  175. private:
  176. void Dfs(int u, int pre, std::vector<int> &father,
  177. const std::vector<std::vector<int>> &g) {
  178. father[u] = pre;
  179. for (auto e : g[u]) {
  180. if (e != pre) {
  181. Dfs(e, u, father, g);
  182. }
  183. }
  184. }
  185. };

code for problem3

  1. #include <algorithm>
  2. #include <memory>
  3. #include <stack>
  4. #include <unordered_map>
  5. #include <unordered_set>
  6. #include <vector>
  7.  
  8. class StronglyConnectedComponentSolver {
  9. public:
  10. StronglyConnectedComponentSolver() = default;
  11.  
  12. void Initialize(int n) { edges_.resize(n); }
  13.  
  14. std::vector<int> Solve() {
  15. total_ = static_cast<int>(edges_.size());
  16. if (total_ == 0) {
  17. return {};
  18. }
  19. visited_.resize(total_, false);
  20. low_indices_.resize(total_, 0);
  21. dfs_indices_.resize(total_, 0);
  22. connected_component_indices_.resize(total_, 0);
  23. for (int i = 0; i < total_; ++i) {
  24. if (0 == dfs_indices_[i]) {
  25. Dfs(i);
  26. }
  27. }
  28. return connected_component_indices_;
  29. }
  30.  
  31. int VertexNumber() const { return static_cast<int>(edges_.size()); }
  32.  
  33. inline void AddEdge(int from, int to) { edges_[from].push_back(to); }
  34.  
  35. const std::vector<int> &Tos(int u) const { return edges_[u]; }
  36.  
  37. private:
  38. void Dfs(const int u) {
  39. low_indices_[u] = dfs_indices_[u] = ++index_;
  40. stack_.push(u);
  41. visited_[u] = true;
  42. for (auto v : edges_[u]) {
  43. if (0 == dfs_indices_[v]) {
  44. Dfs(v);
  45. low_indices_[u] = std::min(low_indices_[u], low_indices_[v]);
  46. } else if (visited_[v]) {
  47. low_indices_[u] = std::min(low_indices_[u], dfs_indices_[v]);
  48. }
  49. }
  50. if (dfs_indices_[u] == low_indices_[u]) {
  51. int v = 0;
  52. do {
  53. v = stack_.top();
  54. stack_.pop();
  55. visited_[v] = false;
  56. connected_component_indices_[v] = connected_component_index_;
  57. } while (u != v);
  58. ++connected_component_index_;
  59. }
  60. }
  61.  
  62. std::vector<std::vector<int>> edges_;
  63. int total_ = 0;
  64. std::vector<bool> visited_;
  65. std::vector<int> low_indices_;
  66. std::vector<int> dfs_indices_;
  67. std::stack<int> stack_;
  68. int index_ = 0;
  69. int connected_component_index_ = 0;
  70. std::vector<int> connected_component_indices_;
  71. };
  72.  
  73. class TwoSatisfiabilitySolver {
  74. public:
  75. void Initialize(int total_vertex_number) {
  76. scc_solver_.Initialize(total_vertex_number);
  77. }
  78.  
  79. // If idx1 is type1, then idx2 must be type2.
  80. void AddConstraint(int idx1, bool type1, int idx2, bool type2) {
  81. int from = idx1 * 2 + (type1 ? 1 : 0);
  82. int to = idx2 * 2 + (type2 ? 1 : 0);
  83. scc_solver_.AddEdge(from, to);
  84. }
  85.  
  86. void AddConflict(int idx1, bool type1, int idx2, bool type2) {
  87. AddConstraint(idx1, type1, idx2, !type2);
  88. AddConstraint(idx2, type2, idx1, !type1);
  89. }
  90.  
  91. void AddLead(int idx1, bool type1, int idx2, bool type2) {
  92. AddConstraint(idx1, type1, idx2, type2);
  93. AddConstraint(idx2, !type2, idx1, !type1);
  94. }
  95.  
  96. // The idx must not be type
  97. void SetFalse(int idx, bool type) { SetTrue(idx, !type); }
  98.  
  99. // The idx must be type
  100. void SetTrue(int idx, bool type) { AddConstraint(idx, !type, idx, type); }
  101.  
  102. bool ExistSolution() {
  103. if (scc_indices_.empty()) {
  104. scc_indices_ = scc_solver_.Solve();
  105. total_scc_number_ =
  106. *std::max_element(scc_indices_.begin(), scc_indices_.end()) + 1;
  107. }
  108. for (int i = 0; i < scc_solver_.VertexNumber() / 2; ++i) {
  109. if (scc_indices_[i * 2] == scc_indices_[i * 2 + 1]) {
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115.  
  116. std::vector<bool> GetOneSolution() {
  117. if (!ExistSolution()) {
  118. return {};
  119. }
  120. BuildNewGraph();
  121. TopSort();
  122. int total = scc_solver_.VertexNumber();
  123. std::vector<bool> result(total / 2);
  124. for (int e = 0; e < total / 2; ++e) {
  125. if (last_color_[scc_indices_[e * 2]] == 0) {
  126. result[e] = false;
  127. } else {
  128. result[e] = true;
  129. }
  130. }
  131. return std::move(result);
  132. }
  133.  
  134. private:
  135. void BuildNewGraph() {
  136. new_edges_.resize(total_scc_number_);
  137. new_graph_node_in_degree_.resize(total_scc_number_, 0);
  138. int total = scc_solver_.VertexNumber();
  139. for (int i = 0; i < total; ++i) {
  140. int scc0 = scc_indices_[i];
  141. for (auto e : scc_solver_.Tos(i)) {
  142. int scc1 = scc_indices_[e];
  143. if (scc0 != scc1 &&
  144. new_edges_[scc1].find(scc0) == new_edges_[scc1].end()) {
  145. new_edges_[scc1].insert(scc0);
  146. ++new_graph_node_in_degree_[scc0];
  147. }
  148. }
  149. }
  150. }
  151.  
  152. void TopSort() {
  153. std::vector<int> conflict(total_scc_number_);
  154. int total = scc_solver_.VertexNumber() / 2;
  155. for (int i = 0; i < total; ++i) {
  156. conflict[scc_indices_[i * 2]] = scc_indices_[i * 2 + 1];
  157. conflict[scc_indices_[i * 2 + 1]] = scc_indices_[i * 2];
  158. }
  159. last_color_.resize(total_scc_number_, -1);
  160. std::stack<int> st;
  161. for (int i = 0; i < total_scc_number_; ++i) {
  162. if (0 == new_graph_node_in_degree_[i]) {
  163. st.push(i);
  164. }
  165. }
  166. while (!st.empty()) {
  167. int u = st.top();
  168. st.pop();
  169. if (last_color_[u] == -1) {
  170. last_color_[u] = 0;
  171. last_color_[conflict[u]] = 1;
  172. }
  173. for (auto e : new_edges_[u]) {
  174. int cur = --new_graph_node_in_degree_[e];
  175. if (cur == 0) {
  176. st.push(e);
  177. }
  178. }
  179. }
  180. }
  181.  
  182. std::vector<int> scc_indices_;
  183. int total_scc_number_ = 0;
  184. std::vector<std::unordered_set<int>> new_edges_;
  185. std::vector<int> new_graph_node_in_degree_;
  186. std::vector<int> last_color_;
  187.  
  188. StronglyConnectedComponentSolver scc_solver_;
  189. };
  190.  
  191. class GCDLCM {
  192. public:
  193. std::string possible(int n, const std::string &type,
  194. const std::vector<int> &A, const std::vector<int> &B,
  195. const std::vector<int> &C) {
  196. std::unordered_set<int> primes;
  197. for (auto c : C) {
  198. for (int i = 2; i * i <= c; ++i) {
  199. if (c % i == 0) {
  200. primes.insert(i);
  201. while (c % i == 0) {
  202. c /= i;
  203. }
  204. }
  205. }
  206. if (c > 1) {
  207. primes.insert(c);
  208. }
  209. }
  210. int m = static_cast<int>(C.size());
  211. auto Check = [&](int p) {
  212. std::vector<int> min(n, 0);
  213. std::vector<int> max(n, 1000);
  214. std::vector<int> number(m, 0);
  215.  
  216. for (int i = 0; i < m; ++i) {
  217. int t = C[i];
  218. while (t % p == 0) {
  219. ++number[i];
  220. t /= p;
  221. }
  222. if (type[i] == 'G') {
  223. min[A[i]] = std::max(min[A[i]], number[i]);
  224. min[B[i]] = std::max(min[B[i]], number[i]);
  225. } else {
  226. max[A[i]] = std::min(max[A[i]], number[i]);
  227. max[B[i]] = std::min(max[B[i]], number[i]);
  228. }
  229. }
  230. for (int i = 0; i < n; ++i) {
  231. if (min[i] > max[i]) {
  232. return false;
  233. }
  234. }
  235. std::unique_ptr<TwoSatisfiabilitySolver> solver(
  236. new TwoSatisfiabilitySolver());
  237. solver->Initialize(n * 2);
  238. for (int i = 0; i < m; i++) {
  239. int u = A[i];
  240. int v = B[i];
  241. if (type[i] == 'G') {
  242. bool x = min[u] > number[i];
  243. bool y = min[v] > number[i];
  244. if (x && y) {
  245. return false;
  246. } else if (x) {
  247. if (min[v] != max[v]) {
  248. solver->SetTrue(v, false);
  249. }
  250. } else if (y) {
  251. if (min[u] != max[u]) {
  252. solver->SetTrue(u, false);
  253. }
  254. } else {
  255. if (min[v] != max[v] && min[u] != max[u]) {
  256. solver->AddConstraint(v, true, u, false);
  257. solver->AddConstraint(u, true, v, false);
  258. }
  259. }
  260. } else {
  261. bool x = max[u] < number[i];
  262. bool y = max[v] < number[i];
  263. if (x && y) {
  264. return false;
  265. } else if (x) {
  266. if (min[v] != max[v]) {
  267. solver->SetTrue(v, true);
  268. }
  269. } else if (y) {
  270. if (min[u] != max[u]) {
  271. solver->SetTrue(u, true);
  272. }
  273. } else {
  274. if (min[v] != max[v] && min[u] != max[u]) {
  275. solver->AddConstraint(v, false, u, true);
  276. solver->AddConstraint(u, false, v, true);
  277. }
  278. }
  279. }
  280. }
  281. return solver->ExistSolution();
  282. };
  283. for (auto p : primes) {
  284. if (!Check(p)) {
  285. return "Solution does not exist";
  286. }
  287. }
  288. return "Solution exists";
  289. }
  290. };

TC SRM633的更多相关文章

  1. H TC並沒有成為下一個摩托羅拉或諾基亞。

    關於2014年第四季度,H T C在三季度財報說明中提到,“年度旗艦H T CO ne(M 8)與中端機型H T C D esire系列在競爭日趨激烈的智能手機市場保持穩定的銷售,市占率有所提升,延續 ...

  2. TC(Total Commander)文件管理神器

    TC文件管理神器 Total Commander是一个会显著提高文件操作效率的工具,而文件操作是应用计算机最基本的功夫,也是伴随一生的操作.因此花一点时间学习,而会受益一世. Total Comman ...

  3. Linux TC基于CBQ队列的流量管理范例

    参考了TC的很多文档,自己也整理了一篇配置记录.在实际使用过程中效果还不错,在此分享给大家以备参考.环境:局域网规模不是很大40多台机器. NAT共享上网(内网:eth0 外网:eth2)CBQ是通过 ...

  4. Linux TC流量控制HOWTO中文版

    <本文摘自Linux的高级路由和流量控制HOWTO中文版 第9章节>网人郭工进行再次编译: 利用队列,我们可以控制数据发送的方式.记住我们只能对发送数据进行控制(或称为整形).其实,我们无 ...

  5. 蒟蒻修养之tc蓝名计划

    开一个新坑......(听说tc是智商高的人才能玩的QAQ显然我是被屠的... 1 [645DIV2]这个能说是裸模拟吗... 弃坑= =做了一些题感觉没必要放上来了= =等div1先吧....... ...

  6. 使用 TC 对LInux中vpn 上传下载进行限速(转)

    TC 无需安装,Linux 内核自带 例:将vpn IP地址段192.168.1.0/24 上传下载限速为 5M 将以下内容添加到/etc/ppp/ip-up文件exit 0上面. down=5Mbi ...

  7. 电影TS、TC、SCR、R5、BD、HD等版本是什么意思

    在很多电影下载网站的影片标题中我们都能看到,比如<刺杀希特勒BD版>.<游龙戏凤TS版>等,这些英文缩写都是什么意思呢?都代表什么画质?以下就是各个版本的具体含义: 1.CAM ...

  8. Linux下TC使用说明

    Linux下TC使用说明   一.TC原理介绍 Linux操作系统中的流量控制器TC(Traffic Control)用于Linux内核的流量控制,主要是通过在输出端口处建立一个队列来实现流量控制. ...

  9. TC Hash Filter

    Overview The u32 filter allows you to match on any bit field within a packet, so it is in some ways ...

随机推荐

  1. Deep Learning 深度学习 学习教程网站集锦

    http://blog.sciencenet.cn/blog-517721-852551.html 学习笔记:深度学习是机器学习的突破 2006-2007年,加拿大多伦多大学教授.机器学习领域的泰斗G ...

  2. linux中编译git时提示找不到ssl.h头文件

    在centos中的解决方案是安装一个叫 openssl-devel 的包.

  3. Openstack的HA解决方案【haproxy和keepalived】

    1. 安装haproxy,keepalived, httpd,3台机器一致. yum install haproxy keepalived httpd -y 2. 修改httpd的默认页面. 在/va ...

  4. 批处理命令——echo 和 @

    [1]echo 命令简介 echo 命令的常见用法(必须掌握)分为以下几种情况: 一.无参数 作用:显示当前echo的状态:处于打开或关闭状态. 新建一个文本文件,命名为echo,修改类型为bat,用 ...

  5. laravel 目录结构

    图 1.1 显示了 Laravel 项目目录结构是什么样子: 图1.1 Laravel 项目目录结构 就如你看到这样,laravel下面只包含了4个文件夹,这4个文件夹下面有一些子文件夹,这种丰富的子 ...

  6. [转]Jexus的常用操作和基本配置

    转自http://www.cnblogs.com/xiaodiejinghong/archive/2013/04/05/3000404.html 3.Jexus的操作 经过两个章节关于Jexus的介绍 ...

  7. 使用Symfony 2在三小时内开发一个寻人平台

    简介 Symfony2是一个基于PHP语言的Web开发框架,有着开发速度快.性能高等特点.但Symfony2的学习曲线也比 较陡峭,没有经验的初学者往往需要一些练习才能掌握其特性. 本文通过一个快速开 ...

  8. 在PostgreSQL中使用oracle_fdw访问Oracle

    本文讲述如何在PostgreSQL中使用oracle_fdw访问Oracle上的数据. 1. 安装oracle_fdw 可以参照:oracle_fdw in github 编译安装oracle_fdw ...

  9. ACM题目————数独

    描述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个3*3宫内的 数字均含1-9,不重复. 每一道合格的数独谜题都有 ...

  10. Ubuntu中root用户和user用户的相互切换(转)

    Ubuntu是最近很流行的一款Linux系统,因为Ubuntu默认是不启动root用户,现在介绍如何进入root的方法. (1)从user用户切换到root用户 不管是用图形模式登录Ubuntu,还是 ...