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 部分题解的更多相关文章

  1. Educational Codeforces Round 64 部分题解

    Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...

  2. Educational Codeforces Round 64部分题解

    Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...

  3. Educational Codeforces Round 63部分题解

    Educational Codeforces Round 63 A 题目大意就不写了. 挺简单的,若果字符本来就单调不降,那么就不需要修改 否则找到第一次下降的位置和前面的换就好了. #include ...

  4. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  5. Educational Codeforces Round 38

    http://codeforces.com/contest/938 A:sb题 //#pragma comment(linker, "/stack:200000000") //#p ...

  6. Educational Codeforces Round 16---部分题解

    710A. King Moves 给你图中一点求出它周围有几个可达的点: 除边界之外都是8个,边界处理一下即可: #include<iostream> #include<cstdio ...

  7. 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 ...

  8. 【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$,那么答案为$ ...

  9. 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 ...

随机推荐

  1. form:select form:options 标签数据回显

    在jsp页面中经常会使用到 form:select form:options 遍历后台List集合生成 select 下拉选择器,但是 form:options 标签并没有提供一个可以回显数据的属性. ...

  2. HDU 1034 Candy Sharing Game (模拟)

    题目链接 Problem Description A number of students sit in a circle facing their teacher in the center. Ea ...

  3. 数据库管理软件 Navicat Premium12 破解步骤

    数据库管理软件  Navicat Premium12B    https://pan.baidu.com/s/1QnAQwW-q0SQ1JglpFGxKOA   密码 : mwqc 里面的软件和补丁是 ...

  4. tp5 r3 一个简单的SQL语句调试实例

    tp5 r3 一个简单的SQL语句调试实例先看效果核心代码 public function index() { if (IS_AJAX && session("uid&quo ...

  5. C++ 内联函数inline

    http://blog.csdn.net/u011327981/article/details/50601800 1.  内联函数 在C++中我们通常定义以下函数来求两个整数的最大值: 复制代码 代码 ...

  6. python基础===string模块常量

    In [8]: import string In [9]: dir(string) In [10]: string.ascii_letters Out[10]: 'abcdefghijklmnopqr ...

  7. 123.Best Time to Buy and Sell Stock III---dp

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ 题目大意:与122题类似,只是这 ...

  8. 在html页面中引入公共的头部和底部

    参考链接: http://www.cnblogs.com/jason-star/p/3345225.html http://blog.csdn.net/jsxzzliang/article/detai ...

  9. sendEmail实现邮件报警

    安装 wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz 或者点击下载 tar -xf sen ...

  10. ImageNet Classification with Deep Convolutional Neural Network(转)

    这篇论文主要讲了CNN的很多技巧,参考这位博主的笔记:http://blog.csdn.net/whiteinblue/article/details/43202399 https://blog.ac ...