【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解
【比赛链接】
【题解】
Problem A Word Correction【字符串】
不用多说了吧,字符串的基本操作
Problem B Run for your prize【贪心】
我们可以将这个数轴一分为二,小于等于500000的由第一个人领,否则由第二个人领
Problem C Constructing tests【贪心】【数学】
首先我们发现 : N^2 - (N / M)^2 = x (N/M向下取整)
然后我们算出N的上下界,发现: sqrt(x+1)<=N<=sqrt(3/4*x) (sqrt是开根号的意思)
所以我们可以枚举N,算出M
Problem D Buy a ticket【最短路】
首先想到可以跑N遍最短路
但是很显然,这样会超时,那么我们该如何优化呢?
我们不妨先建一个原点,将原点与每个城市连边,权值为在这个城市开演唱会的价格ai,然后再将城市与城市之间连边,
但是每条路径上的权值要乘2,因为是往返的费用
我们发现,只要对这个图跑一遍最短路,就得出了答案。
注意N最大10^5,SPFA不能过,要用dijkstra+堆优化
代码 :
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN = * 1e5; LL i,N,M;
LL dist[MAXN+],a[MAXN+],b[MAXN+],c[MAXN+],w[MAXN+],vis[MAXN+];
vector< pair<LL,LL> > E[MAXN+]; template <typename T> inline void read(T &x) {
LL f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = x * + c - '';
x *= f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline void dijkstra(LL st) {
LL i,x,to,cost;
priority_queue< pair<LL,LL> > q;
for (i = ; i <= N; i++) dist[i] = LONG_LONG_MAX;
q.push(make_pair(,st));
dist[st] = ;
memset(vis,,sizeof(vis));
while (!q.empty()) {
x = q.top().second; q.pop();
if (vis[x]) continue;
vis[x] = ;
for (i = ; i < E[x].size(); i++) {
to = E[x][i].first;
cost = E[x][i].second;
if (dist[x] + cost < dist[to]) {
dist[to] = dist[x] + cost;
q.push(make_pair(-dist[to],to));
}
}
}
} int main() { read(N); read(M);
for (i = ; i <= M; i++) {
read(a[i]); read(b[i]); read(w[i]);
E[a[i]].push_back(make_pair(b[i],w[i]*));
E[b[i]].push_back(make_pair(a[i],w[i]*));
}
for (i = ; i <= N; i++) {
read(c[i]);
E[N+].push_back(make_pair(i,c[i]));
}
dijkstra(N+);
for (i = ; i <= N; i++) {
if (i == ) write(dist[i]);
else { putchar(' '); write(dist[i]); }
}
puts(""); return ;
}
【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解的更多相关文章
- 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 (Rated for Div. 2)
这场打了小号 A. Word Correction time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)
题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...
- Educational Codeforces Round 92 (Rated for Div. 2) B、C题解
TAT 第一场codeforces B. Array Walk #暴力 #贪心 题目链接 题意 有\(a1, a2, ..., an\) 个格子(每个格子有各自分数),最初为1号格(初始分数为\(a1 ...
- Educational Codeforces Round 116 (Rated for Div. 2), problem: (C) Banknotes
传送门 Problem - C - Codeforces 题目 题目重点内容手打翻译:(欢迎批评指正) 在柏林, 使用着n套不同的货币(banknotes).第i个货币面额为10ai 元,货币的第一种 ...
- Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal 题解(思维+逆序对)
题目链接 题目大意 给你一个长度为n的字符串,可以交换相邻两个元素,使得这个字符串翻转,求最少多少种次数改变 题目思路 如果要求数组排序所需要的冒泡次数,那其实就是逆序对 这个也差不多,但是如果是相同 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
随机推荐
- Java中的网络基础
先来一张图记录一下大概思路,之后再更新具体的代码实现.基本上来说,前半部分自己会编写一个基于socket编程的多客户端dos聊天服务器,后半部分可以实现与已有的一些服务器(比如www.google.c ...
- Java中常量定义在interface和class的区别(转)
最终结论:定义常量在interface和class中其实都行,关键是看你的设计和个人爱好. Java中interface中定义变量默认都是"public static final" ...
- Android L中的RecyclerView 、CardView 、Palette的使用
<Material Design>提到,Android L版本中新增了RecyclerView.CardView .Palette.RecyclerView.CardView为用于显示复杂 ...
- 【Todo】UDP P2P打洞原理
参考以下两篇文章: https://my.oschina.net/ososchina/blog/369206 http://m.blog.csdn.net/article/details?id=666 ...
- ubuntu harddisk uuid already exists
就是virtualbox下先用u盘启动的虚拟机,把U盘的vhdk文件拷贝到本机,然后再启动,就有问题,提示什么uuid already exist 找了半天,网上基本都是说windows下如何用的.. ...
- PPAPI+Skia实现的涂鸦板
在PPAPI插件中使用Skia画图介绍了怎样在PPAPI中使用Skia,文末说回头要提供一个简单的涂鸦板插件,这次我来兑现承诺了. foruok原创,关注微信订阅号"程序视界"可联 ...
- hdoj-1856-More is better【并查集】
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) To ...
- 三期_day05_Dao层的准备工作_II
工作文件夹: 实体类:UserInfo.java package com.yc.crm.entity; import java.util.Date; public class UserInfo { p ...
- SolidEdge 如何绘制剖视图
如果要创建剖视图,则点击切割平面按钮,然后绘制剖面线,画好之后点击完成 然后点击剖视图按钮,鼠标单击刚才的剖面线,往要的方向拖动,即可生成剖面视图 剖视图有时也需要用到旋转剖视图 如下图所示, ...
- Meteor 从一个列表页进入详情页怎样高速显示详情
无论是做android开发,还是做网页web开发,都 会有列表,都须要点击列表进入列表项的详情页面,查看具体信息,能常情况下,我们都是将这一列表项的id传到详情页,由详情页再到数据库查询具体信息. 在 ...