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$城市左边城市 ...
随机推荐
- jquery 禁用/启用滚动条
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- init.rc文件中面启动c++程序,通过jni调用java实现
</pre><p>注:假设是自己的myself.jar包,还要修改例如以下:</p><p>target/product/core_base.mk PRO ...
- Python机器学习--降维
主成分分析(PCA) 测试 # -*- coding: utf-8 -*- """ Created on Thu Aug 31 14:21:51 2017 @author ...
- 在 Edison 上自动启动 Arduino Sketch
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...
- uva 11488 - Hyper Prefix Sets(字典树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- nginx 配置nginx.conf,负载均衡,逻辑分流
nginx 最重要的配置文件nginx.conf: 一般的配置我不做解释,网上到处都是,主要对主要的几点进行注释(如下) worker_processes ; error_log /data/logs ...
- no matching provisioning profiles found
问题:真机连上,执行这个提示. 解决: 项目->targets->Bulid Settings-> 1,Provisioning Profile->选择配置Bundle Ide ...
- 《Unix网络编程》中的错误处理函数
#include "net.h" #include <syslog.h> // syslog() int daemon_proc; static void err_do ...
- C++中的getopt的用法
getopt的用法 getopt被用来解析命令行选项参数.就不用自己写东东处理argv了. 点击(此处)折叠或打开 #include <unistd.h> extern char *opt ...
- LiberOJ #6210. 「美团 CodeM 决赛」tree 树形DP
题目链接:点这里 题解: 需要证明,所求的路径一定是全部权值都为1或者,路径上权值至多有一个为2其余为1且权值2在路径中央. 然后树形DP 设定dp[i][0/1] 以1为根的情况下,以i 节点下子树 ...

