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 ...
随机推荐
- JSTL获取当日时间与数据时间比较
<jsp:useBean id="now" class="java.util.Date" /> <fmt:formatDate value=& ...
- Asp.Net MVC 自定义登录过滤器
1.新建类BaseController用于统一所有控制器继承扩展,方便扩展登录等过滤器.示例如下: using CloudWave.JustBeHere.JBH_H5.Controllers.Attr ...
- Latent Semantic Analysis(LSA/ LSI)原理简介
LSA的工作原理: How Latent Semantic Analysis Works LSA被广泛用于文献检索,文本分类,垃圾邮件过滤,语言识别,模式检索以及文章评估自动化等场景. LSA其中一个 ...
- tap事件的原理详解
点击事件延迟问题所在: 在移动端中,由于两次触摸是放大操作,,所以当你点击一次的时候,浏览器会等待300ms,看用户是否会进行第二次点击,如果没有的话,才会执行点击事件 为什么要解决: 随着h5游戏, ...
- Fire Net(深度优先搜索)
ZOJ Problem Set - 1002 Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we ha ...
- PHP扩展--opcache安装及配置
简介 Optimizer+ 是 Zend 开发的闭源但可以免费使用的 PHP 优化加速组件,是第一个也是最快的 opcode 缓存工具.现在,Zend 科技公司将 Optimizer+ 在 PHP L ...
- JVM学习十一:JVM之深入分析ClassLoader
本章节准备写的是对类加载器ClassLoader的剖析,但因为前面已经对类加载器做过一些简单的分析和双亲委派机制的分析:因此本章节的侧重点在于实例演示和自定义加载器. 一.什么是ClassLoader ...
- JQuery对RadioButton和CheckButton的操作
js对RadioButton和CheckButton的操作,在网站开发中会经常遇到,而JQuery操作RadioButton和CheckButton非常便捷.小编觉得网站开发人员有必要熟练掌握.所以小 ...
- Spring Data JPA笔记
1. Spring Data JPA是什么 Spring Data JPA是Spring Data大家族中的一员,它对对持久层做了简化,用户只需要声明方法的接口,不需要实现该接口,Spring Dat ...
- C++ 内联函数inline
http://blog.csdn.net/u011327981/article/details/50601800 1. 内联函数 在C++中我们通常定义以下函数来求两个整数的最大值: 复制代码 代码 ...