topcoder srm 515 div1
problem1 link
暴力枚举即可。
problem2 link
一共有24小时,所以最多有24个顾客。设$f[x][y][z]$表示还剩下$x$把刀,现在时间是$y$,以及来过的顾客集合为$z$可以获得的最大值。
那假设第$y$小时来的顾客为$t$,来的概率为$p$,有三种情况:
(1)之前它来过,那么$f[x][y][z]=f[x][y+1][z]$
(2)之前没来过,现在也没来,$f[x][y][z]=(1-p)*f[x][y+1][z]$
(3)之前没来过,现在来了。可以选择买给他一把刀,$c_{1}=f[x-1][y+1][z|2^{t}]+v_{y}$;或者不卖给他,$c_{2}=f[x][y+1][z|2^{t}]$,所以$f[x][y][z]=max(c_{1},c_{2})*p$
另外,假设一个人第一次,第二次来的概率分别为0.2和0.4。那么上面使用的$p$是假设他之前没来过。如果第一次没来,那么第二次来的概率为$\frac{0.4}{1-0.2}$
problem3 link
首先枚举字母'R'和‘F’的位置$p_{1},p_{2}$。然后计算和其他所有‘L’的答案。
首先,对于每个位置$(x,y)$,计算其函数值$g(x,y)=distance(p_{1},p_{x,y})+distance(p_{2},p_{x,y})$
然后新建一系列节点$S_{0},S_{1},S_{2},...$。
对于每个$(x,y)$,设$t=g(x,y)$。那么节点$S_{t-1}$向点$(x,y)$连有向边。
另外节点$S_{i}$到$S_{i+1}$有一条有向边。每个点$(x,y)$向四周的格子有四条有向边。
所有边的长度均为1.然后从节点$S_{0}$开始进行bfs,计算到达每个点的距离。每个‘L’字母计算的距离就是选择$p_{1},p_{2}$和该位置的答案。
code for problem1
class RotatedClock {
public:
string getEarliest(int hourHand, int minuteHand) {
for (int i = 0; i < 12; ++i) {
for (int j = 0; j < 60; j += 2) {
for (int k = 0; k < 12; ++k) {
if (Check(i, j, k, hourHand, minuteHand)) {
return ToString(i) + ":" + ToString(j);
}
}
}
}
return "";
} string ToString(int x) {
stringstream ss;
if (x < 10) {
ss << "0" << x;
} else {
ss << x;
}
return ss.str();
} bool Check(int h, int m, int b, int hHand, int mHand) {
return GetHourHand(h, m, b) == hHand && GetMinuteHand(m, b) == mHand;
} int GetHourHand(int h, int m, int b) {
int result = 0;
if (h >= b) {
result = (h - b) * 30 + m / 2;
} else {
result = 360 - (b - h) * 30 + m / 2;
}
return result;
}
int GetMinuteHand(int m, int b) {
b *= 5;
if (m >= b) {
return (m - b) * 6;
}
else {
return 360 - (b - m) * 6;
}
} };
code for problem2
double cache[25][24][1 << 12];
int visited[25][24][1 << 12]; class NewItemShop {
public:
double getMaximum(int swords, vector<string> customers) {
Init(customers);
memset(visited, 0, sizeof(visited));
return dfs(swords, 0, 0);
} double dfs(int swords, int hour, int mask) {
if (swords == 0 || hour == 24) {
return 0;
}
if (visited[swords][hour][mask] != 0) {
return cache[swords][hour][mask];
}
visited[swords][hour][mask] = 1;
if (!hour_[hour].inited_) {
return cache[swords][hour][mask] = dfs(swords, hour + 1, mask);
} int cMask = hour_[hour].mask();
double p = hour_[hour].probility_;
int val = hour_[hour].value_; if ((mask & cMask) > 0) {
return cache[swords][hour][mask] = dfs(swords, hour + 1, mask);
} cache[swords][hour][mask] = (1 - p) * dfs(swords, hour + 1, mask); double c0 = dfs(swords, hour + 1, mask | cMask);
double c1 = dfs(swords - 1, hour + 1, mask | cMask) + val; cache[swords][hour][mask] += max(c0, c1) * p; return cache[swords][hour][mask];
} private: struct HourInfo {
bool is_multiple_times_;
int multiple_index_;
double probility_;
int value_; bool inited_; HourInfo():inited_(false) {} int mask() const {
if (!is_multiple_times_) {
return 0;
}
return 1 << multiple_index_;
} }hour_[24]; void Init(const vector<string>& customers) {
for (int i = 0; i < 24; ++ i) {
hour_[i].inited_ = false;
}
int multiple_num = 0;
for (int i = 0; i < (int)customers.size(); ++ i) {
const vector<int> a = Split(customers[i]);
const int n = (int)a.size();
const int idx = n > 3 ? multiple_num ++ : 0;
double cur = 1.0;
for (int j = 0; j < n; j += 3) {
int h = a[j];
int v = a[j + 1];
double p = a[j + 2] / 100.0 / cur;
cur -= a[j + 2] / 100.0;
if (n > 3) {
hour_[h].is_multiple_times_ = true;
hour_[h].multiple_index_ = idx;
}
else {
hour_[h].is_multiple_times_ = false;
}
hour_[h].probility_ = p;
hour_[h].value_ = v;
hour_[h].inited_ = true;
}
}
} std::vector<int> Split(const std::string& s) {
std::vector<int> result;
const int len = (int)s.length();
int idx = 0;
while (idx < len) {
while (idx < len && !IsDigit(s[idx])) {
++ idx;
}
if (idx >= len) {
break;
}
int x = 0;
while (idx < len && IsDigit(s[idx])) {
x = x * 10 + s[idx++] - '0';
}
result.push_back(x);
}
return result;
} static bool IsDigit(char c) {
return '0' <= c && c <= '9';
}
};
code for problem3
class MeetInTheMaze {
public:
string getExpected(vector<string> maze) {
Initialize(maze);
std::vector<std::pair<int, int>> all_f_positions;
std::vector<std::pair<int, int>> all_r_positions;
int l_number = 0;
for (int i = 0; i < height_; ++i) {
for (int j = 0; j < width_; ++j) {
char ch = maze_[i][j];
if (ch == 'F') {
all_f_positions.push_back(std::make_pair(i, j));
} else if (ch == 'R') {
all_r_positions.push_back(std::make_pair(i, j));
} else if (ch == 'L') {
++l_number;
}
}
}
int total = 0;
for (auto f : all_f_positions) {
for (auto r : all_r_positions) {
int t = Calculate(f.first, f.second, r.first, r.second);
if (t == -1) {
return "";
}
total += t;
}
}
int d = (int)all_f_positions.size() * (int)all_r_positions.size() *
l_number;
int g = Gcd(total, d);
total /= g;
d /= g;
std::stringstream ss;
ss << total << "/" << d;
return ss.str();
} ~MeetInTheMaze() { delete[] distance_; } private:
int Calculate(const int f_x, const int f_y, const int r_x, const int r_y) {
std::vector<std::vector<std::pair<int, int>>> new_graph(
(width_ + height_) << 1);
for (int i = 0; i < height_; ++i) {
for (int j = 0; j < width_; ++j) {
if (maze_[i][j] != '#') {
int d1 = GetDistance(f_x, f_y, i, j);
int d2 = GetDistance(r_x, r_y, i, j);
if (d1 == -1 || d2 == -1) {
continue;
}
int d = d1 + d2;
if (d > (int)new_graph.size()) {
new_graph.resize(d);
}
new_graph[d - 1].push_back(std::make_pair(i, j));
}
}
}
const int nodes = (int)new_graph.size();
std::vector<int> dist(height_ * width_ + nodes, -1); dist[height_ * width_] = 0;
std::queue<int> a_queue;
a_queue.push(height_ * width_);
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
while (!a_queue.empty()) {
const int start = a_queue.front();
a_queue.pop();
if (start < width_ * height_) {
const int sx = start / width_;
const int sy = start % width_;
for (int i = 0; i < 4; ++i) {
int x = sx + dx[i];
int y = sy + dy[i];
if (0 <= x && x < height_ && 0 <= y && y < width_) {
if (maze_[x][y] == '#') {
continue;
}
if (dist[x * width_ + y] == -1) {
dist[x * width_ + y] = dist[start] + 1;
a_queue.push(x * width_ + y);
}
}
}
} else {
const int new_graph_index = start - width_ * height_;
for (auto son : new_graph[new_graph_index]) {
int x = son.first;
int y = son.second;
int key = x * width_ + y;
if (dist[key] == -1) {
dist[key] = dist[start] + 1;
a_queue.push(key);
}
}
if (new_graph_index + 1 < nodes) {
dist[start + 1] = dist[start] + 1;
a_queue.push(start + 1);
}
}
}
int total = 0;
for (int i = 0; i < height_; ++i) {
for (int j = 0; j < width_; ++j) {
if (maze_[i][j] == 'L') {
if (dist[i * width_ + j] == -1) {
return -1;
}
total += dist[i * width_ + j];
}
}
}
return total;
} int Gcd(int x, int y) { return y == 0 ? x : Gcd(y, x % y); } void Initialize(const std::vector<std::string> &maze) {
maze_ = maze;
height_ = (int)maze_.size();
width_ = (int)maze_[0].size();
const int total_size = width_ * width_ * height_ * height_;
distance_ = new int[total_size];
memset(distance_, -1, sizeof(int) * total_size);
} int GetDistance(int start_x, int start_y, int end_x, int end_y) { if (start_x > end_x || (start_x == end_x && start_y > end_y)) {
std::swap(start_x, end_x);
std::swap(start_y, end_y);
} int key = GetKey(start_x, start_y, end_x, end_y);
if (distance_[key] == -1) {
CalculateDistanceFrom(start_x, start_y);
}
return distance_[key];
} void CalculateDistanceFrom(const int start_x, const int start_y) {
std::queue<std::pair<int, int>> a_queue;
a_queue.push(std::make_pair(start_x, start_y));
distance_[GetKey(start_x, start_y, start_x, start_y)] = 0;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
while (!a_queue.empty()) {
const int sx = a_queue.front().first;
const int sy = a_queue.front().second;
a_queue.pop();
const int from_key = GetKey(start_x, start_y, sx, sy);
for (int i = 0; i < 4; ++i) {
int x = sx + dx[i];
int y = sy + dy[i];
if (0 <= x && x < height_ && 0 <= y && y < width_) {
if (maze_[x][y] == '#') {
continue;
}
int key = GetKey(start_x, start_y, x, y);
if (distance_[key] != -1) {
continue;
}
distance_[key] = distance_[from_key] + 1;
a_queue.push(std::make_pair(x, y));
}
}
}
} int GetKey(int x1, int y1, int x2, int y2) const {
return x1 * width_ * height_ * width_ + y1 * height_ * width_ +
x2 * width_ + y2;
} int width_;
int height_;
std::vector<std::string> maze_;
int *distance_;
};
topcoder srm 515 div1的更多相关文章
- 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 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如果增加 ...
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
Problem Statement The Happy Letter game is played as follows: At the beginning, several players ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
- 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)其 ...
随机推荐
- 不同版本Hibernate.获取SessionFactory的方式
不同版本Hibernate.获取SessionFactory的方式 Hibernate 版本说明: 我当前使用的是 Hibernate 5.x ,(hibernate-release-5.3.6.Fi ...
- 通过Referer设置来防盗链
在Servlet中需要设置防盗链功能时可以通过以下代码: String referer = request.getHeader("Referer"); if(referer == ...
- uvm设计分析——field automation
uvm中的field_automation主要实现了class中的基础元素的copy,compare等函数, 实现方式分为两种:1)用户注册,field系列宏:uvm内部调用static status ...
- Python全栈-异常处理
一.异常 1.异常的定义 异常是错误发生的信号,程序一旦出错就会抛出错误信息,如果不及时处理就会程序就会随之停止运行 异常有三部分组成: 1)异常类型 2)异常追踪 3)异常的值 2.异常的分类 1) ...
- 8.Thread的join方法
1.Thread类的join方法表示:当前线程执行结束再执行其它线程!在Thread类中有三个重载的方法分别是: public final synchronized void join(long mi ...
- html5-文件的基本格式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Robot - 1. robot framework环境搭建
Fom:https://www.cnblogs.com/puresoul/p/3854963.html 一. robot framework环境搭建: 官网:http://robotframework ...
- [博客迁移]探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据
上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...
- [博客迁移]探索Windows Azure 监控和自动伸缩系列1 - 连接中国区Azure
最近准备基于Microsoft Azure Management Libraries 实现虚拟机的监控.主要的需求就是获取虚拟机内置的性能计数器数据,基于性能计数器实现后续的监控和自动伸缩. 作为这一 ...
- Python学习记录之----网络通信(二)
网络通信 socket 这一节太难了,还是看TA的吧 http://www.cnblogs.com/alex3714/articles/5830365.html 不能执行top等类似的 会持续输出 ...