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$城市左边城市 ...
随机推荐
- 取汉子拼音首字母的VB.Net方法
'/ <summary> '/ 获得一个字符串的汉语拼音码 '/ </summary> '/ <param name="strText">字符串 ...
- printf行缓冲区的分析总结
最近在客户那调试串口的时候,read串口然后printf打印,单字符printf,发现没有输出,后来想起来printf这些标准输入输出函数也是属于标准C库glibc的, 这里就要区分一下标准库函数和系 ...
- YII 多子域名同步登录
a.meylou.com和b.meylou.com不做登录.c.meylou.com这个专门做用户登录.c站登录之后a,b站点同时登录. 第一步:修改php.ini配置文件,把cookie_domai ...
- jQuery源代码框架思路
開始计划时间读源代码,第一节jQuery框架阅读思路整理 (function(){ jQuery = function(){}; jQuery一些变量和函数和给jQuery对象加入一些方法和属性 ex ...
- Nova虚拟机启动提示libvirtError
OpenStack自动化安装基本折腾完毕,装一次大概也就10分钟,但是装完后今天我的虚拟机起不来,经过查找log发 现如下图提示: 已经到这里,说明已经过了nova-sheduler那一关,跟踪一下代 ...
- 杭电 1596 find the safest road (最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的.仅仅只是是把+改成了*,输入输出有些不一样而已. find t ...
- HDU 6061 RXD and functions NTT
RXD and functions Problem Description RXD has a polynomial function f(x), f(x)=∑ni=0cixiRXD has a tr ...
- 推断View是否显示在界面上
我们都知道ViewController有viewWillAppear和viewDidAppear等关于页面生命周期的方法,用来对视图做一些管理,比方页面出现时怎么样,页面消失时怎么样.. 可是对于Vi ...
- Codeforces Round #254 (Div. 2):B. DZY Loves Chemistry
B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...
- HDU 6108 小C的倍数问题 【数学】 (2017"百度之星"程序设计大赛 - 初赛(A))
小C的倍数问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...