B. Mishka and trip
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX — beautiful, but little-known northern country.
Here are some interesting facts about XXX:
- XXX consists of n cities, k of whose (just imagine!) are capital cities.
- All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to ci.
- All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 — 2 — ... — n — 1. Formally, for every 1 ≤ i < n there is a road between i-th and i + 1-th city, and another one between 1-st and n-th city.
- Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every 1 ≤ i ≤ n, i ≠ x, there is a road between cities x and i.
- There is at most one road between any two cities.
- Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i and j, price of passing it equals ci·cj.
Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities a and b (a < b), such that there is a road betweena and b you are to find sum of products ca·cb. Will you help her?
InputThe first line of the input contains two integers n and k (3 ≤ n ≤ 100 000, 1 ≤ k ≤ n) — the number of cities in XXX and the number of capital cities among them.
The second line of the input contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 10 000) — beauty values of the cities.
The third line of the input contains k distinct integers id1, id2, ..., idk (1 ≤ idi ≤ n) — indices of capital cities. Indices are given in ascending order.
OutputPrint the only integer — summary price of passing each of the roads in XXX.
Examplesinput4 1
2 3 1 2
3output17input5 2
3 5 2 2 4
1 4output71NoteThis image describes first sample case:
It is easy to see that summary price is equal to 17.
This image describes second sample case:
It is easy to see that summary price is equal to 71.
水完A题艰难的刷出了B,看到10e5整个人都不好了 这难度梯度也太大了吧摔! 后来想到了乘法分配律O(n)暴力....
首先记录首都节点 然后非首都节点每次连接下一个城市,首都节点则连接除自己和前一个城市外的所有城市,注意重复的情况和i==1,i==n的情况。
附AC代码:
#include<iostream>
#include<cstring>
using namespace std; int a[];
int v[]; int main(){
int n,k;
memset(a,,sizeof(a));
memset(v,,sizeof(v));//初始化为0
cin>>n>>k;
long long sum1=,sum2=;
for(int i=;i<=n;i++){
cin>>a[i];
sum1+=a[i];//所有点值的和
}
int x;
for(int i=;i<k;i++){
cin>>x;
v[x]=;//记录首都节点
}
for(int i=;i<=n;i++){
if(v[i]){//判断是否是首都
if(i==){//1和n时特判
sum2+=a[]*(sum1-a[n]-a[]);
if(v[n]){
sum2+=a[]*a[n];
}
}
else{//连接除自身和前一个点外的所有点
sum2+=a[i]*(sum1-a[i]-a[i-]);
if(v[i-]){//若前一个点也为首都则连接两点
sum2+=a[i]*a[i-];
}
}
sum1-=a[i];//减掉已全连过的首都节点 避免重复
}
else{//连接自己和下一个点 n时特判
if(i==n){
sum2+=a[]*a[n];
}
else{
sum2+=a[i]*a[i+];
}
}
}
cout<<sum2<<endl;
return ;
}
B. Mishka and trip的更多相关文章
- 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 ...
- 暑假练习赛 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 703B. Mishka and trip 模拟
B. Mishka and trip time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- codeforces 703B B. Mishka and trip(数学)
题目链接: B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 703A Mishka and trip
Description Little Mishka is a great traveller and she visited many countries. After thinking about ...
- cf B. Mishka and trip (数学)
题意 Mishka想要去一个国家旅行,这个国家共有个城市,城市通过道路形成一个环,即第i个城市和第个城市之间有一条道路,此外城市和之间有一条道路.这个城市中有个首中心城市,中心城市与每个城市(除了 ...
- Codeforces 703B (模拟) Mishka and trip
题目:这里 题意:n个城市,每个城市有个魅力值vi,首先,有n条路将这n个城市连成一个环,1号城市连2号城市,2号连3号****n号连1号城市,每条路的魅力值是其连接的两个城市 的魅力值的乘积,这n个 ...
- CodeForces 703B Mishka and trip
简单题. 先把环上的贡献都计算好.然后再计算每一个$capital$ $city$额外做出的贡献值. 假设$A$城市为$capital$ $city$,那么$A$城市做出的额外贡献:$A$城市左边城市 ...
随机推荐
- kafka-0.8.1.1总结
文件夹 一. 基础篇 1. 开篇说明 2. 概念说明 3. 配置说明 4. znode分类 5. kafka协议分类 6. Kafka线 ...
- 【转】 使用 Python 获取 Linux 系统信息
在本文中,我们将会探索使用Python编程语言工具来检索Linux系统各种信息.走你. 哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相 ...
- POJ 1083 && HDU 1050 Moving Tables (贪心)
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- php操作xml的方法
xml源文件 <?xml version="1.0 encoding="UTF-8"?> <humans> <zhangying> & ...
- 全文索引--自己定义chinese_lexer词典
本文来具体解释一下怎样自己定义chinese_lexer此法分析器的词典 初始化数据 create table test2 (str1 varchar2(2000),str2varchar2(2000 ...
- saltstack安装配置(syndic)
syndic是saltstack用来做集群部署的,一般结构如图: syndic是一个特殊的minion,syndic类继承于minion类,syndic可以看作一个代理,只做数据传递. CentOS上 ...
- WebSocket 和 Socket 的区别
WebSocket 和 Socket 的区别 英文:TheAlchemist 链接:http://www.jianshu.com/p/59b5594ffbb0 <刨根问底 HTTP 和 We ...
- 基础才是重中之重~用好configSections让配置信息更规范
对于小型项目来说,配置信息可以通过appSettings进行配置,而如果配置信息太多,appSettings显得有些乱,而且在开发人员调用时,也不够友好,节点名称很容易写错,这时,我们有几种解决方案 ...
- HDOJ_1000
#include int main() { int i, j; while(scanf("%d%d", &i, &j) == 2) printf("%d\ ...
- sim的准确识别技术
几个月钱,我换了一个手机,本着工科男动手能力强的原则,自己用✂️把sim卡剪成了一个小卡,然后成功的可以使用了. 然而就在昨天,我将卡拿出之后,再放回去,却无法识别我的sim卡了. 我上网查了方法,怀 ...