cf B. Mishka and trip (数学)
题意
Mishka想要去一个国家旅行,这个国家共有个城市,城市通过道路形成一个环,即第i个城市和第个城市之间有一条道路,此外城市和之间有一条道路。这个城市中有个首中心城市,中心城市与每个城市(除了自己)之间有一条道路。第城市个城市有一个魅力值,经过一条连接第个和第个城市的道路的费用是,求所有道路的费用之和是多少?
注意:任何两个城市之间最多只有一条路。
思路
先考虑所有中心城市:每条路只能算一遍,那么我们可以得到如下公式:
其中,代表所有城市的魅力值的和,代表已经计算过的中心城市的魅力值,那么就代表第个中心城市连接其他城市道路的魅力值之和(无重复计算)。
最后再枚举环中的条边,如果某条边连接的两个城市中有一个是中心城市,说明这条路已经计算过,不要重复计算。
注意:答案可能超出int,用long long
AC代码
#include <stdio.h>
#include <string.h>
typedef long long LL;
const int maxn = 100000+5;
int n, k;
int c[maxn], d[maxn];
bool cp[maxn];
int main() {
while(scanf("%d%d", &n, &k) == 2) {
memset(cp, 0, sizeof(cp));
LL sum = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
sum += c[i];
}
int id;
for(int i = 0; i < k; i++) {
scanf("%d", &d[i]);
cp[d[i]] = true;
}
//all capital cities
int tol = 0;
LL ans = 0;
for(int i = 0; i < k; i++) {
id = d[i];
tol += c[id];
ans += 1LL*c[id]*(sum-tol);
}
//the road bettwen two cities which aren't capital
if(!cp[1] && !cp[n]) ans += c[1]*c[n];
for(int i = 1; i < n; i++) {
if(!cp[i] && !cp[i+1]) ans += c[i]*c[i+1];
}
printf("%lld\n", ans);
}
return 0;
}
如有不当之处欢迎指出!
cf B. Mishka and trip (数学)的更多相关文章
- codeforces 703B B. Mishka and trip(数学)
题目链接: B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 暑假练习赛 003 F Mishka and trip
F - Mishka and trip Sample Output Hint In the first sample test: In Peter's first test, there's on ...
- Codeforces Round #365 (Div. 2) Mishka and trip
Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...
- cf703B Mishka and trip
B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 703B. Mishka and trip 模拟
B. Mishka and trip time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- CodeForces 703A Mishka and trip
Description Little Mishka is a great traveller and she visited many countries. After thinking about ...
- B. Mishka and trip
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- CF A.Mishka and Contest【双指针/模拟】
[链接]:CF/4892 [题意]: 一个人解决n个问题,这个问题的值比k小, 每次只能解决最左边的或者最右边的问题 解决了就消失了.问这个人能解决多少个问题. [代码]: #include<b ...
- CF 990A. Commentary Boxes【数学/模拟】
[链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...
随机推荐
- linux_inotify
什么是inotify? 拥有强大.粒细粒度.异步文件系统事件监控机制,监控文件系统中添加.删除.修改.移动等各种事件 版本支持: 内核 2.6.13以上版本,inotify-tools 是实施监控的软 ...
- scrapy-redis功能简介
connection:连接redis最基本文件 default:默认值设置文件 dupefiler_key 保存指纹 dupefilter:替换scrapy默认的url去重器 piklecompat: ...
- Servlet--Servlet接口
servlet主要数据结构 Servlet 接口:主要定义了servlet的生命周期方法 ServletConfig接口:为servlet提供了使用容器服务的若干重要对象和方法. ServletCon ...
- linkin大话数据结构--字符串,数组,list之间的互转
在实际开发中,我们经常会用到字符串,字符数组,字符list,当然也会不可避免的进行这3者之间的互相转换. 在使用到Apache和Google下的common包,可以这样子实现: package tz. ...
- linkin大话面向对象--方法详解
1,方法的参数传递机制:值传递. 首先弄懂2个概念:形参和实参. 形参(形式参数):相当于函数(Java中也把函数称之为方法)中的局部变量,在函数被调用时创建,并以传入的实参作为起始值,函数调用结束时 ...
- Oracle 视图 (待更新, 缓存)
参考: 视图.索引.存储过程优缺点: http://www.cnblogs.com/SanMaoSpace/p/3147059.html oracle视图总结(转):http://tianwei013 ...
- JavaScript数据结构 (手打代码)
array: 数组创建: ); //创建一个长度为6的数组 ,,,,,); 数组方法: var str="I love javascript"; var single=str.sp ...
- tensorflow Image 解码函数
觉得有用的话,欢迎一起讨论相互学习~Follow Me tf.image.decode_png(contents, channels=None, name=None) Decode a PNG-enc ...
- 前端 js技术
1.JavaScript代码存在形式 <!-- 方式一 --> <script type"text/javascript" src="JS文件" ...
- 如何将top命令输出重定向为文件
命令: # top -b -n 2 -d 3 > /tmp/top.out 解析: -b: batch 模式,可以重定向到文件中 -n:一共取2次top数据 -d:每次top数据间隔为3秒