topcoder srm 681 div1
problem1 link
二分答案。然后判断。将所有的机器按照$a_{i}$排序,$a_{i}$相同的按照$b_{i}$排序。用一个优先队列维护这些机器。这样对于第$i$个部分,拿出队列开始的机器来生产该部分;如果队列开头的机器生产的部分没用完,则将其左区间$a_{t}$设置为$a_{t}+1$然后重新塞到队列里。
problem2 link
首先由$A_{i}$得到$A_{i-1}$的方式为$A_{i-1}=((A_{i}+2^{50}-b)$^$a)$&$(2^{50}-1)$。然后就是一个数字一个数字暴力计算$B$值。总的累积复杂度是$O(n)$的。
problem3 link
令$f[i][j]$ 表示$vals[i]*vals[j]$对答案有贡献的概率。
令$dp[i][j][n]$表示$(i,j)$对答案有贡献且序列中总的元素个数为$n$的概率(那么$f[i][j]=dp[i][j][n]$)。考虑整个序列的长度从长度为 $n-1$转移到$n$。那么对于$dp[i][j][n]$来说,可以从下面三种情况转移而来:
(1)$dp[i-1][j-1][n-1]$: 在前面新增一个元素
(2)$dp[i][j-1][n-1]$: 在中间新增一个元素
(3)$dp[i][j][n-1]$: 在后面新增一个元素
code for problem1
#include <algorithm>
#include <queue>
#include <vector> class FleetFunding {
public:
int maxShips(int m, const std::vector<int> &k, const std::vector<int> &a,
const std::vector<int> &b) {
int n = static_cast<int>(k.size());
struct node {
int L, R, cnt; node() = default;
node(int L, int R, int cnt) : L(L), R(R), cnt(cnt) {}
bool operator<(const node &A) const {
if (L != A.L) return L > A.L;
return R > A.R;
}
};
auto Check = [&](int M) {
if (M == 0) {
return true;
}
std::priority_queue<node> Q;
for (int i = 0; i < n; ++i) {
Q.push(node(a[i], b[i], k[i]));
}
for (int i = 1; i <= m; ++i) {
if (Q.empty()) {
return false;
}
int sum = 0;
while (sum < M) {
if (Q.empty() || Q.top().L != i) {
return false;
}
if (sum + Q.top().cnt < M) {
sum += Q.top().cnt;
Q.pop();
} else {
node p = Q.top();
Q.pop();
p.cnt -= M - sum;
++p.L;
if (p.L <= p.R && p.cnt > 0) {
Q.push(p);
}
break;
}
}
while (!Q.empty() && Q.top().L == i) {
node p = Q.top();
Q.pop();
++p.L;
if (p.L <= p.R) {
Q.push(p);
}
}
}
return true;
}; int sum = 0;
for (int i = 0; i < n; ++i) {
sum += k[i];
}
int low = 0, high = sum / m;
int result = 0;
while (low <= high) {
int M = (low + high) >> 1;
if (Check(M)) {
result = std::max(result, M);
low = M + 1;
} else {
high = M - 1;
}
}
return result;
}
};
code for problem2
class LimitedMemorySeries2 {
public:
int getSum(int n, long long x0, long long a, long long b) {
constexpr int kMod = 1000000007;
constexpr long long M = (1ll << 50) - 1;
auto NextX = [&](long long x) { return ((x ^ a) + b) & M; };
auto PreX = [&](long long x) { return ((x + M + 1 - b) ^ a) & M; };
auto Cal = [&](int id, long long x, int n) {
int ll = id, rr = id;
int result = 0;
long long lx = x, rx = x;
while (ll - 1 >= 0 && rr + 1 < n) {
lx = PreX(lx);
rx = NextX(rx);
if (lx < x && x > rx) {
++result;
--ll;
++rr;
} else {
break;
}
}
return result;
};
int result = 0;
long long t = x0;
for (int i = 0; i < n; ++i) {
result = (result + Cal(i, t, n)) % kMod;
t = NextX(t);
}
return result;
}
};
code for problem3
#include <vector>
class CoinFlips {
public:
double getExpectation(const std::vector<int> &vals, int prob) {
const double p = prob / 1000000000.0;
int n = static_cast<int>(vals.size());
std::vector<double> p2(n + 1);
p2[0] = 1.0;
for (int i = 1; i <= n; ++i) {
p2[i] = p2[i - 1] * (1 - p);
}
std::vector<std::vector<double>> prefix(n + 1, std::vector<double>(n + 1));
std::vector<std::vector<double>> prefix_sum(n + 1,
std::vector<double>(n + 1));
for (int len = 3; len <= n; ++len) {
for (int i = 1; i <= len; ++i) {
prefix[len][i] = p2[i - 1] * p;
if (i == 1) {
prefix[len][i] += p2[len];
}
prefix_sum[len][i] = prefix_sum[len][i - 1] + prefix[len][i];
}
}
std::vector<std::vector<std::vector<double>>> f(
2, std::vector<std::vector<double>>(n + 1, std::vector<double>(n + 1)));
int pre = 0, cur = 1;
for (int len = 3; len <= n; ++len) {
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
f[cur][i][j] = 0;
}
}
for (int L = 1; L <= len; ++L) {
for (int R = L + 2; R <= len; ++R) {
f[cur][L][R] =
prefix_sum[len][L - 1] * f[pre][L - 1][R - 1] +
(prefix_sum[len][R - 1] - prefix_sum[len][L]) * f[pre][L][R - 1] +
(1 - prefix_sum[len][R]) * f[pre][L][R];
if (L + 2 == R) {
f[cur][L][R] += p2[L] * p;
}
}
}
pre ^= 1;
cur ^= 1;
}
double result = 0.0;
for (int i = 0; i < n; ++i) {
for (int j = i + 2; j < n; ++j) {
result += vals[i] * vals[j] * f[pre][i + 1][j + 1];
}
}
return result;
}
};
topcoder srm 681 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)其 ...
随机推荐
- C++实现 safaBase64编码跟nonSafeBase64编码的转换
默认Base64编码的字符串,用于网络传输是不安全的,因为Base64编码使用的标准字典含有“+”,“/”. 规则如下: //nonSafeBase64 到 safeBase64'+' ------ ...
- efcore从数据库快速生成实体及context
有些项目开发时先建立数据库,再用codefirst来书写entity和EntityConfiguration,比较耗费功夫. 1.在vs2017中新建个asp.net core的web项目,或者其他项 ...
- SEO经验-如何做到新站上线半个月谷歌收录3万
一个网站在做搜索引擎优化的时候,首先考虑的肯定是百度,这个是毋庸置疑的,毕竟百度占领者差不多七成的搜索市场份额. 第二考虑的是360搜索,这个也是毋庸置疑的,因为360搜索占领者差不多两成的搜索市场份 ...
- 准备MyBatis
MyBatis下载:https://github.com/mybatis/mybatis-3/releases MyBatis文件目录: 中文参考文档:http://www.mybatis.org/m ...
- 设计模式之Interpreter(解释器)(转)
Interpreter定义: 定义语言的文法 ,并且建立一个解释器来解释该语言中的句子. Interpreter似乎使用面不是很广,它描述了一个语言解释器是如何构成的,在实际应用中,我们可能很少去构造 ...
- 转:C#判断ContextMenuStrip右键菜单的来源(从哪个控件弹出来的)
转载自:http://hi.baidu.com/cookiemulan/item/82df8ff867dd53cc531c26c7 有时候,为了提高性能和节约资源,我们会为多个控件,指定同一个右键弹出 ...
- Django后端项目----restful framework 认证源码流程
一.请求到来之后,都要先执行dispatch方法,dispatch方法方法根据请求方式的不同触发get/post/put/delete等方法 注意,APIView中的dispatch方法有很多的功能 ...
- ubuntu 18.04下安装Hadoop
在Ubuntu里装完Java环境后,接下来就开始学习安装Hadoop了,参照的是以下链接 https://blog.csdn.net/xuan314708889/article/details/805 ...
- Linux学习笔记之在 CentOS 里下载 RPM 包及其所有依赖包
0x00 概述 前几天我尝试去创建一个仅包含我们经常在 CentOS 7 下使用的软件的本地仓库.当然,我们可以使用 curl 或者 wget 下载任何软件包,然而这些命令并不能下载要求的依赖软件包. ...
- django ORM聚合函数
在Django中,聚合函数是通过aggregate方法实现的,aggregate方法返回的结果是一个字典 在使用时需要先导入模块from django.db.models import Count,A ...