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$城市左边城市 ...
随机推荐
- ViewPager 无限循环遇到的坑 viewpager.setOffscreenPageLimit(2);
viewpager.setOffscreenPageLimit(limit);这个方法,是表示viewpage除了当前显示的页面外,左右个预加载的页面个数,也就是 为limit=2时表示当前一共加载了 ...
- XP操作系统设置:[82]关机快捷键
磨镰刀不少割麦,掌握了快速关机的多种方法,在尴尬的时候说不定还真能派上用场呢. 工具/原料 手提电脑.台式电脑.Windows 操作系统. 方法一: 1 Windows XP 操作系统中有 ...
- takeLatest 如何接受 this.props.dispatch 传递的参数
1.步骤一 // 获取查询参数 getQueryParams(params){ // 请求月考核分的数据 this.props.dispatch({ type:'getMonthlyAssessmen ...
- UVA 11246 - K-Multiple Free set(数论推理)
UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合.求一个子集合.使得元素个数最多,而且不存在有两个元素x1 * k = x2,求出最多的元素个数是 ...
- AMD的ARM之路前景几何?
http://server.zdnet.com.cn/all-2129330.html#2129333 AMD将于2014年推出基于ARM架构的Opteron(皓龙)处理器,应该是最近一段时间在IT产 ...
- [转载]php 数组 类对象 值传递 引用传递 区别
一般的数据类型(int, float, bool)不做这方面的解说了 这里详细介绍一下数组和的类的对象作为参数进行值传递的区别 数组值传递 实例代码: <?php function main() ...
- bootstrap-Table服务端分页,获取到的数据怎么再页面的表格里显示
<table class="table table-hover" id="userTable" > <thead> <tr> ...
- 【项目发起】千元组装一台大型3D打印机全教程(一)前言
前言 最近又碰到了大尺寸模型打样的需求,我这台17cm直径的kossel mini就捉襟见肘了.怎么办呢,这个时候kossel的好就体现出来了,随意扩展,那么就自己做个kossel-max吧.为了向前 ...
- Xamarin.Android 记事本(二)自定义AlertDialog
导读 1.自定义一个AlertDialog 2.添加一条数据 正文 记事本应当有一个添加功能,这里我打算在右上角放一个item,然后点击这个item弹出一个对话框,输入名称,点击确定跳转到另一个act ...
- Micro Frontends
Micro Frontends - extending the microservice idea to frontend development https://micro-frontends.or ...

