problem1 link

最优选择一定是在$2n$个端点中选出两个。

problem2 link

分开考虑每个区间。设所有区间的左端点的最大值为$lc$,所有区间的右端点的最小值为$rc$.对于某个区间$L$,其实就是找最少的区间(包括$L$)能够完全覆盖区间$[lc,rc]$.

设$L$的左右端点为$L_{1},L_{2}$。考虑从$L_{1}$向左扩展,每次扩展一定是找一个区间$p$满足$p_{2}\geq L_{1}$且使得$p_{1}$最小。右端点向右扩展类似。

problem3 link

对于后手来说,它一定是每次只放置一个障碍格子在先手目前的位置。否则,如果放置了太多,那么先手就容易决策出向左还是向右比较好。

对于先手来说,假设目前先手在$(r,c)$,那么如果$(r,c)$和$(r+1,c)$之间没有障碍,且轮到先手,应该直接跳到$(r+1,c)$,否则,那么由后手的策略来看,此时已经有的障碍一定是一个包含$(r,c)$的区间,此时先手可以尝试向左或者向右运动到障碍区间外一个格子。

这样就可以用$(r,c,left,right,tag)$来表示一个状态来进行动态规划。$(r,c)$表示先手目前的位置,$(left, right)$表示目前障碍的区间,$tag$指示目前是先手还是后手。

code for problem1

#include <vector>

class EelAndRabbit {
public:
int getmax(std::vector<int> &l, std::vector<int> t) {
const int n = static_cast<int>(l.size());
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2; ++j) {
int p1 = j == 0 ? -t[i] : -t[i] - l[i];
for (int k = 0; k < n; ++k) {
for (int r = 0; r < 2; ++r) {
int p2 = r == 0 ? -t[k] : -t[k] - l[k];
if (p1 != p2) {
int num = 0;
long long mask = 0;
for (int x = 0; x < n; ++x) {
if ((-t[x] - l[x] <= p1 && p1 <= -t[x]) ||
(-t[x] - l[x] <= p2 && p2 <= -t[x])) {
if ((mask & (1ll << x)) == 0) {
++num;
mask |= 1ll << x;
}
}
}
ans = std::max(ans, num);
}
}
}
}
}
return ans;
}
};

code for problem2

#include <algorithm>
#include <string>
#include <vector> constexpr int N = 2500;
constexpr int M = 10000; int s[N], t[N];
int back[M], next[M];
int back_cost[M], next_cost[M];
int sort_idx[N]; class ShoutterDiv1 {
public:
int count(const std::vector<std::string>& s1000,
const std::vector<std::string>& s100,
const std::vector<std::string>& s10,
const std::vector<std::string>& s1,
const std::vector<std::string>& t1000,
const std::vector<std::string>& t100,
const std::vector<std::string>& t10,
const std::vector<std::string>& t1) {
int n = 0;
for (const auto& e : s1000) {
n += static_cast<int>(e.size());
}
for (int i = 0; i < n; ++i) {
s[i] = t[i] = 0;
sort_idx[i] = i;
}
for (int i = 0; i < M; ++i) {
back[i] = next[i] = i;
}
Parse(s1000, s, 1000);
Parse(s100, s, 100);
Parse(s10, s, 10);
Parse(s1, s, 1);
Parse(t1000, t, 1000);
Parse(t100, t, 100);
Parse(t10, t, 10);
Parse(t1, t, 1); int c_left = t[0];
int c_right = s[0];
for (int i = 0; i < n; ++i) {
back[t[i]] = std::min(back[t[i]], s[i]);
next[s[i]] = std::max(next[s[i]], t[i]);
c_left = std::min(c_left, t[i]);
c_right = std::max(c_right, s[i]);
}
for (int i = M - 2; i >= 0; --i) {
back[i] = std::min(back[i], back[i + 1]);
}
for (int i = 1; i < M; ++i) {
next[i] = std::max(next[i], next[i - 1]);
}
for (int i = 0; i < M; ++i) {
if (i <= c_left) {
back_cost[i] = 0;
} else if (i == back[i]) {
back_cost[i] = -1;
} else {
if (back_cost[back[i]] == -1) {
back_cost[i] = -1;
} else {
back_cost[i] = 1 + back_cost[back[i]];
}
}
}
for (int i = M - 1; i >= 0; --i) {
if (i >= c_right) {
next_cost[i] = 0;
} else if (i == next[i]) {
next_cost[i] = -1;
} else {
if (next_cost[next[i]] == -1) {
next_cost[i] = -1;
} else {
next_cost[i] = 1 + next_cost[next[i]];
}
}
}
auto Cost = [&](int left, int right) {
if (back_cost[left] == -1 || next_cost[right] == -1) {
return -1;
}
return back_cost[left] + next_cost[right];
}; std::sort(sort_idx, sort_idx + n, [&](int x, int y) {
return s[x] < s[y] || (s[x] == s[y] && t[x] < t[y]);
}); int result = 0;
for (int i = 0; i < n; ++i) {
int tmp = Cost(s[sort_idx[i]], t[sort_idx[i]]);
for (int j = 0; j < N && s[sort_idx[j]] <= s[sort_idx[i]]; ++j) {
if (t[sort_idx[i]] <= t[sort_idx[j]]) {
int val = Cost(s[sort_idx[j]], t[sort_idx[j]]);
if (val != -1 && (tmp == -1 || tmp > 1 + val)) {
tmp = 1 + val;
}
}
}
if (tmp == -1) {
return -1;
}
result += tmp;
}
return result;
} private:
void Parse(const std::vector<std::string>& d, int* data, int base) {
int idx = 0;
for (size_t i = 0; i < d.size(); ++i) {
for (size_t j = 0; j < d[i].size(); ++j) {
data[idx++] += (d[i][j] - '0') * base;
}
}
}
};

code for problem3

#include <cstring>
#include <limits>
#include <string>
#include <vector> #include <iostream> constexpr int N = 50;
constexpr int kEachUseBit = 16;
constexpr int kRabbitTagBit = 31;
constexpr int kEelTagBit = 15;
constexpr int kMask = (1 << (kEachUseBit - 1)) - 1; using TType = unsigned int; TType rabbit_eel[N][N][N][N + 1];
int cost[N][N];
int h, w; class WallGameDiv1 {
public:
int play(const std::vector<std::string> &costs) {
h = static_cast<int>(costs.size());
w = static_cast<int>(costs[0].size());
for (int i = 0; i < h; ++i) {
cost[i][0] = costs[i][0] - '0';
for (int j = 1; j < w; ++j) {
cost[i][j] = cost[i][j - 1] + costs[i][j] - '0';
}
} memset(rabbit_eel, 0, sizeof(rabbit_eel));
// Iterator each row to avoid more depths of recursion.
for (int i = h - 1; i >= 0; --i) {
for (int j = 0; j < w; ++j) {
Eel(i, j, j, j);
}
}
int result = std::numeric_limits<int>::max();
for (int i = 0; i < w; ++i) {
result = std::min(result, Cost(0, i, i) + Eel(0, i, i, i));
}
return result;
} private:
bool Computed(const TType &val, bool is_rabbit) {
if (is_rabbit) {
return (val & (1u << kRabbitTagBit)) != 0;
} else {
return (val & (1u << kEelTagBit)) != 0;
}
} int GetCost(const TType &val, bool is_rabbit) {
if (!Computed(val, is_rabbit)) {
return -1;
}
if (is_rabbit) {
return static_cast<int>((val >> kEachUseBit) & kMask);
} else {
return static_cast<int>(val & kMask);
}
} int SetCost(TType &val, bool is_rabbit, int cost) {
if (is_rabbit) {
val |= (static_cast<TType>(cost)) << kEachUseBit;
val |= 1u << kRabbitTagBit;
} else {
val |= cost;
val |= 1u << kEelTagBit;
}
return cost;
} int Eel(int row, int col, int left, int right) {
TType &val = rabbit_eel[row][col][left][right];
int result = GetCost(val, false);
if (result != -1) {
return result;
}
if (row == h - 1) {
return SetCost(val, false, 0);
}
result = Rabbit(row, col, left, right); if (right - left < w - 1) {
result = std::max(result, Rabbit(row, col, std::min(col, left),
std::max(col + 1, right)));
} return SetCost(val, false, result);
} int Rabbit(int row, int col, int left, int right) {
TType &val = rabbit_eel[row][col][left][right];
int result = GetCost(val, true);
if (result != -1) {
return result;
}
if ((left <= col) && (col < right)) {
result = std::numeric_limits<int>::max();
if (left > 0) {
result = Cost(row, left - 1, col - 1) + Eel(row, left - 1, left, right);
}
if (right < w) {
result = std::min(
result, Cost(row, col + 1, right) + Eel(row, right, left, right));
}
} else {
result = Cost(row + 1, col, col) + Eel(row + 1, col, col, col);
}
return SetCost(val, true, result);
} int Cost(int row, int left, int right) {
if (left <= right) {
if (left == 0) {
return cost[row][right];
}
return cost[row][right] - cost[row][left - 1];
}
return 0;
}
};

topcoder srm 580 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 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. 【1】public

    [面向对象] 李坤是不是人?(人类) 飞飞是不是人?(人类) 扎心是不是人?(人类) 是:特指某一个事物 属于:同一的类型 什么是对象: 就是特指的某一个东西,万物皆对象 什么是类: 具有一批相同属性 ...

  2. easyui textbox 输入小写自动变大写,easyui textbox 绑定oninput事件 easyui textbox 绑定propertychange事件

    <input id="id" class="easyui-textbox" name="id" value="@Model. ...

  3. Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]

    题目链接:https://codeforces.com/contest/1090/problem/D Vasya had an array of n integers, each element of ...

  4. [No0000198]swagger api一键导入postman

    在用postman进行接口测试时,对于参数较多的接口时第一次添加接口参数是比较繁琐的,可利用swagger一键导入api接口,事例如下: 1.获取swagger地址 2.打开postman,点击imp ...

  5. Java随机字符串:随机数字字符串,工具类

    Java中生成随机数,字符串的工具类 1. 调用方法需要传入生成字符串的长度和需要的类型 生成随机数字 生成随机字母字符串 生成随机字符串+数字等 ......... 2. 总共8种类型,具体看工具类 ...

  6. WordCount扩展与优化

    合作者:201631062327,201631062128码云地址:https://gitee.com/LIUJIA6/WordCount3 一:项目说明 本次项目是在上次作业WorldCount的基 ...

  7. python-颜色显示

    格式:\033[显示方式;字体色;背景色m......[\033[0m] ------------------------------------------- 字体色 | 背景色 | 颜色描述 -- ...

  8. you-get模块

    You-Get是一个基于 Python 3 的下载工具.使用 You-Get 可以很轻松的下载到网络上的视频.图片及音乐. 转载https://www.cnblogs.com/wangchuanyan ...

  9. bugfree3.0.1-BUG解决方案修改

    该篇内容来自文档“masterBugFree2.pdf”,记录在这里. 1.如何将解决方案改为中文 在\Bugfree\Lang\ZH_CN_UTF-8 \_COMMON.php 文件中做如下修改/* ...

  10. [js]面向对象2

    delete删除属性 删除对象的属性 删除未用var定义的变量. delete返回布尔 删除不存在的属性,返回true 无法删除原形中的属性 如 delete obj.toString() resu= ...