Educational Codeforces Round 38 部分题解
D. Buy a Ticket
分析
建一个源点,连向所有结点,边的花费为那个结点的花费,图中原有的边花费翻倍,最后跑一遍最短路即可。
code
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long INF = 2e18;
struct Edge { int to; long long w; };
vector<Edge> G[N];
long long d[N];
priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > q;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++) {
int u, v;
long long w;
scanf("%d%d%I64d", &u, &v, &w);
G[u].push_back(Edge{v, 2 * w});
G[v].push_back(Edge{u, 2 * w});
}
for(int i = 0; i < n; i++) {
long long x;
scanf("%I64d", &x);
G[0].push_back(Edge{i + 1, x});
}
fill(d, d + N, INF);
d[0] = 0;
q.push(pair<long long, int>(0, 0));
while(!q.empty()) {
pair<long long, int> now = q.top(); q.pop();
if(d[now.second] < now.first) continue;
for(Edge e : G[now.second]) {
if(now.first + e.w < d[e.to]) {
d[e.to] = now.first + e.w;
q.push(pair<long long, int>(d[e.to], e.to));
}
}
}
for(int i = 1; i <= n; i++) printf("%I64d%c", d[i], " \n"[i == n]);
return 0;
}
E. Max History
分析
推公式。
过程这里很详细了。
code
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1;
const int MOD = 1e9 + 7;
int a[N];
long long qpow(long long x, long long k) {
long long res = 1;
while(k) {
if(k & 1) res = res * x % MOD;
x = x * x % MOD;
k >>= 1;
}
return res;
}
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
long long x = 1, ans = 0;
for(int i = 0; i < n; i++) x = x * (i + 1) % MOD;
for(int i = 0; i < n - 1; i++) {
if(a[i] == a[n - 1]) break;
ans = (ans + (a[i] * x % MOD) * qpow(n - (lower_bound(a, a + n, a[i]) - a), MOD - 2) % MOD) % MOD;
}
cout << ans << endl;
return 0;
}
F. Erasing Substrings
分析
这道题很神奇。
设 \(n\) 为字符串长, \(m\) 为不超过 \(n\) 的最大的 \(2\) 的倍数。
那么最后答案长度为 \(n-m+1\),组成答案的这些字符中,第一个字符一定是在原子符串区间 \([0,m-1]\) 内取到的,第二个在区间 \([1,m]\) 内取到的,后面同理。
\(f[i]\) 为 \(1\) 则表示可以转移到 \(i\) 这个状态,其中 $ 0\leq i < m$,二进制位为 \(1\) 对应某个长度的删除操作已经执行过了。
每次右移一个字符,然后我们选择要转移到的状态,因为前面已经有一个字符成为答案中的一个字符了,所以比较的时候应该是 \(s[i + j]\) 而不是 \(s[j]\)。
每次我们都要更新出下次可以转移到的合法状态,如果现在的状态是 \(1001\) ,即前面已经删过了长度为 \(1\) 和 \(8\) 的子串,我们可以转移到 \(1001, 1101, 1011, 1111\) ,即我们可以选择删掉长度为 \(2\) 或 \(4\) 的子串,或者不删,直接取接下来的字符。
code
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int f[N], g[N];
char ans[N];
int main() {
string s;
cin >> s;
int n = s.length();
int m = 1;
while(m <= n) m <<= 1; m >>= 1;
for(int i = 0; i < m; i++) f[i] = 1;
for(int i = 0; i <= n - m; i++) {
ans[i] = 'z';
for(int j = 0; j < m; j++) {
if(f[j] && s[i + j] < ans[i]) ans[i] = s[i + j];
}
memset(g, 0, sizeof g);
for(int j = 0; j < m; j++) {
if(!g[j] && f[j] && ans[i] == s[i + j]) {
int w = m - 1 - j;
for(int k = w; k; k = w & (k - 1)) g[m - 1 - k] = 1;
g[m - 1] = 1;
}
}
for(int j = 0; j < m; j++) f[j] = g[j];
}
ans[n - m + 1] = 0;
printf("%s", ans);
return 0;
}
Educational Codeforces Round 38 部分题解的更多相关文章
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- Educational Codeforces Round 64部分题解
Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...
- Educational Codeforces Round 63部分题解
Educational Codeforces Round 63 A 题目大意就不写了. 挺简单的,若果字符本来就单调不降,那么就不需要修改 否则找到第一次下降的位置和前面的换就好了. #include ...
- 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解
[比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B Run for your prize[贪心] ...
- Educational Codeforces Round 38
http://codeforces.com/contest/938 A:sb题 //#pragma comment(linker, "/stack:200000000") //#p ...
- Educational Codeforces Round 16---部分题解
710A. King Moves 给你图中一点求出它周围有几个可达的点: 除边界之外都是8个,边界处理一下即可: #include<iostream> #include<cstdio ...
- Educational Codeforces Round 38 (Rated for Div. 2) C
C. Constructing Tests time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 【Educational Codeforces Round 38】D. Buy a Ticket 堆优化Dijkstra
题意 给定一张无向图,对每个点$i\in S$求$\min_{j\in S} {2\times d(i,j)+a_j}$ 考虑多源多汇最短路会超时,换个角度考虑每个$j$,如果$j=i$,那么答案为$ ...
- Educational Codeforces Round 38 (Rated for Div. 2)
这场打了小号 A. Word Correction time limit per test 1 second memory limit per test 256 megabytes input sta ...
随机推荐
- Bootstrap 文件上传插件 FileInput的使用问题
: 在使用bootstrap的文件上传插件fileinput http://plugins.krajee.com/file-input的预览功能时,删除预览图片在 bootstrap 模态框中没有用, ...
- Debian最完美安装flash的教程//适用于所有linux版本
话说不管是新手还是老手,都离不开flash.没有flash的支持,菜鸟们也少了一些把玩linux的动力. flash有很多安装的方法,不过性能相差很大.这里的缘由就不重要了. 下面我介绍在chromi ...
- vector的哈希值 Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C
http://codeforces.com/contest/757/problem/C 题目大意:有n个导管,每个体育馆有k种神奇宝贝,然后所有的n个体育馆中,一共有m中神奇宝贝.可知,每个神奇宝贝中 ...
- HDU 1402 FFT 大数乘法
$A * B$ FFT模板题,找到了一个看起来很清爽的模板 /** @Date : 2017-09-19 22:12:08 * @FileName: HDU 1402 FFT 大整数乘法.cpp * ...
- asp.net mvc 站点优化
基于上篇:IIS网站日志分析 现象 服务端:IIS 日志, #Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-u ...
- 利用Addon Domain和A记录使两个域名同时指向同一个网站
今天碰到这样的需求:已有网站A.com, 以及新注册的域名B.net, 现需要将B.net指向与A.com相同的内容. 这里提出的方法是在空间后台添加Addon domain, 以及在域名B.net后 ...
- 游戏的物理和数学:Unity中的弹道和移动目标提前量计算
下载地址:https://www.jianguoyun.com/p/DZPN6ocQ2siRBhihnx8 弹道计算是游戏里常见的问题,其中关于击中移动目标的自动计算提前量的话题,看似简单,其实还是挺 ...
- cookie 跨域的问题
今天研究一天发现cookie无法设置除当前域名或者其父域名之外的其他domain. 这个是浏览器出于对cookie的保护造成的,也就是cookie无法跨域设置. 对于子域名也有如下规则,当前域名只能设 ...
- [002] delete_duplication_of_linked_list
[Description] Given a unsort linked list, delete all the duplication from them, no temporary space p ...
- 85.Maximal Rectangle---dp
题目链接:https://leetcode.com/problems/maximal-rectangle/description/ 题目大意:给出一个二维矩阵,计算最大的矩形面积(矩形由1组成).例子 ...