problem1 link

倒着想。每次添加一个右括号再添加一个左括号,直到还原。那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号。

problem2 link

令$h(x)=\sum_{i=1}^{x}g(i)$,那么答案为$h(R)-h(L-1)$。对于$h(x)$:

(1)如果$x\leq K$,那么$h(x)=0$

(2)否则对于$[K+1,x]$之间的所有偶数来说,对答案的贡献为$even+h(\frac{x}{2})-h(\frac{K}{2})$,其中$even=\frac{x}{2}-\frac{K}{2}$,$h(\frac{K}{2})=0$。奇数对答案的贡献为$odd*2+h(\frac{x+K}{2})$,$odd=x-K-even$。其中$[\frac{x}{2}+1,\frac{x+K}{2}]$之间的数字并不多,可以暴力。

problem3 link

下面第二个链接有关于714-div2-hard的题目的线形解法。它的思路记录过往的supply剩余总和以及demand的总和(如果supply大于 demand就抵消)。同时如果demand大于0还要记录最早的demand需要的位置。这样当出现新的supply并且足够抵消过去的demand时就回退回去满足demand然后返回。直到最后。

这道题与上面的区别是坐标会有负数并且可以在任意地方停止。所以可以假设先到达最左侧的某个地方,然后就跟上面类似了。对于在任何地方停止,可以贪心地计算。具体实现细节看代码以及注释。

code for problem1

#include <string>

class ParenthesisRemoval {
public:
int countWays(const std::string &s) {
constexpr int kMod = 1000000007;
int n = static_cast<int>(s.size());
long long ans = 1;
for (int i = n - 1, t = 0; i >= 0; --i) {
if (s[i] == ')') {
++t;
} else {
ans = ans * t % kMod;
--t;
}
}
return ans;
}
};

code for problem2

class NAddOdd {
public:
long long solve(long long L, long long R, int K) {
return Dfs(R, K) - Dfs(L - 1, K);
} private:
long long g(long long x, int K) {
long long ans = 0;
while (x > K) {
++ans;
if (x % 2 == 1) {
x += K;
} else {
x >>= 1;
}
}
return ans;
}
long long Dfs(long long x, int k) {
if (x <= k) {
return 0;
} long long even = (x >> 1) - (k >> 1);
long long odd = x - k - even;
long long ans = even + (odd << 1) + (Dfs(x >> 1, k) << 1);
for (long long i = (x >> 1) + 1; i <= ((x + k) >> 1); ++i) {
ans += g(i, k);
}
return ans;
}
};

code for problem3

#include <algorithm>
#include <vector> class Salesman {
public:
int minMoves(std::vector<int> pos, std::vector<int> delta) {
int result = Compute(pos, delta);
std::reverse(pos.begin(), pos.end());
std::reverse(delta.begin(), delta.end());
for (auto &x : pos) {
x *= -1;
}
result = std::min(result, Compute(pos, delta));
return result;
} private:
int Compute(const std::vector<int> &pos, const std::vector<int> &delta) {
int n = static_cast<int>(pos.size());
int last_need = n - 1;
while (last_need >= 0 && delta[last_need] >= 0) {
--last_need;
}
if (last_need < 0) {
return 0;
}
std::vector<int> next_need(n + 1, n);
for (int i = n - 1; i >= 0; --i) {
if (delta[i] < 0) {
next_need[i] = i;
} else {
next_need[i] = next_need[i + 1];
}
}
int result = std::numeric_limits<int>::max();
for (int left = 0; left < n; ++left) {
int right = last_need;
int sum = 0;
for (int i = left; i <= right; ++i) {
sum += delta[i];
}
while (sum < 0 && right + 1 < n) {
sum += delta[++right];
}
if (sum < 0) {
break;
}
// The left is start and must visit right to get all supplys.
int length = std::abs(pos[left]) + pos[right] - pos[left];
int supply = 0;
int demand = 0;
int go_back_pos = 0;
for (int i = left; i <= right; ++i) {
if (delta[i] < 0) {
int curr_demand = -delta[i];
if (demand == 0 && supply >= curr_demand) {
supply -= curr_demand;
} else {
if (demand == 0) {
// If the pos[i] is negative, then just keep go_back_pos as
// origin and need goto right and back to pos[i]
go_back_pos = std::max(go_back_pos, pos[i]);
}
demand += curr_demand;
}
} else {
supply += delta[i];
if (demand > 0 && supply >= demand) {
supply -= demand;
demand = 0;
// Here you have a choose that return to previous demand pos
// immediately and back and in future you will no need to return
// back
length += 2 * std::max(0, pos[i] - go_back_pos);
}
}
int final_stop_pos =
demand > 0 ? go_back_pos : pos[std::min(right, next_need[i + 1])];
result =
std::min(result, length + std::max(0, pos[right] - final_stop_pos));
}
if (delta[left] < 0) {
break;
}
}
return result;
}
};

  

参考:

https://codeforces.com/blog/entry/50602

https://www.topcoder.com/blog/single-round-match-714-editorials/

topcoder srm 714 div1的更多相关文章

  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 738 div1 FindThePerfectTriangle(枚举)

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

  4. Topcoder SRM 602 div1题解

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

  5. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

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

  6. Topcoder SRM 584 DIV1 600

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

  7. TopCoder SRM 605 DIV1

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

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

  9. topcoder srm 640 div1

    problem1 link 首先使用两个端点颜色不同的边进行连通.答案是$n-1-m$.其中$m$是联通分量的个数. problem2 link 首先构造一个最小割的模型.左边的$n_{1}$个点与源 ...

随机推荐

  1. ajax提交完表单数据依然跳转的解决办法

    1. 既然ajax提交数据,就把表单里面submit按钮换掉,因为触发submit他就会跳转页面 提交的时候他会先触发ajax 再触发submit的提交 2.如果确定了表单没有submit,那么把提交 ...

  2. Nginx查看并发链接数

    一.通过界面查看通过web界面查看时Nginx需要开启status模块,也就是安装Nginx时加上 --with-http_stub_status_module 一.通过界面查看 通过web界面查看时 ...

  3. php开启慢日志

    默认是以本机的安装路径 cd /usr/local/php/etc/ vim php-fpm.conf ; The log file for slow requests ; Default Value ...

  4. XML小结

    一.因为某些字符在xml格式中,具有特殊意义,所以当我们需要使用它本身的意思的时候,就要用其他东西来代替它,否则会产生错误 < < less than > > greater ...

  5. CSS选择符-----属性选择符

       Element[att] 选择具有att属性的E元素 <!DOCTYPE html> <html> <head> <meta charset=" ...

  6. Life Winner Bo (博弈论)

    kind:维持让对手处于(奇数,奇数)的状态,就能赢. rook:维持让对手处于(A,A)相等的状态,就能赢. knight:画图找规律,没有到达终点的就是平局. queen:威佐夫博弈论,终点不一样 ...

  7. Python大神成长之路: 第二次学习记录

    数据类型          数据操作 bytes 类型 "".encode() 编码-->二进制 "".decode() 解码 判断字符串里的字符是否全为 ...

  8. flask用宏渲染表单模板时,表单提交后,如果form.validate_on_submit()返回的是false的可能原因

    flask用宏渲染表单模板时,表单提交后,提交的内容符合DataRequired()校验, 但是form.validate_on_submit()返回的是False, 原因可能是表单模板中的<f ...

  9. python递归的例子

    例子1:递归实现嵌套列表求和 #encoding=utf-8 a=[[1,2,3],  [4,5,6],  [7,8,9]]def listsum(L):    result=0    for i i ...

  10. (Review cs231n) Gradient Calculation and Backward

    ---恢复内容开始--- 昨日之补充web. 求解下图的梯度的流动,反向更新参数的过程,表示为 输入与损失梯度的关系,借助链式法则,当前输入与损失之间的梯度关系为局部梯度乘以后一层的梯度. ---恢复 ...