Codeforces Round #365 (Div. 2) B
Description
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 between aand b you are to find sum of products ca·cb. Will you help her?
The 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.
Print the only integer — summary price of passing each of the roads in XXX.
4 1
2 3 1 2
3
17
5 2
3 5 2 2 4
1 4
71
This 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.
题意:给你一些城市,有些城市是省城,与其他城市都会相连。一般的城市就是 1 — 2 — ... — n — 1这种连接方式,求连接城市相乘积的和。
解法:为了防止重复计算,首先考虑的就是省城的连接,我们先保存所有连接点的和,然后就是 a(省城)*(sum-a(省城)),每计算一次,sum就减一次省城的权值
接下来再考虑一般城市连接,已经计算过的不去考虑,注意一下1和n的情况
#include <bits/stdc++.h>
using namespace std;
int n,m;
long long q[100005];
long long p[100005];
long long ans1[100005],ans2[100005];
map<int,int>c;
set<long long>e;
long long sum=0,ans=0;
long long cot;
int main()
{
cin>>n>>m;
int a,b; for(int i=1; i<=n; i++)
{
cin>>q[i];
sum+=q[i];
}
for(int i=1; i<=m; i++)
{
cin>>p[i];
c[p[i]]=1;
}
if(c[1]==0&&c[n]==0)
{
ans+=(q[1]*q[n]);
}
for(int i=1;i<=m;i++)
{
sum-=q[p[i]];
ans+=(q[p[i]]*sum);
}
for(int i=1;i<n;i++)
{
if(c[i]==0&&c[i+1]==0)
{
ans+=(q[i]*q[i+1]);
}
} cout<<ans<<endl;
return 0;
}
Codeforces Round #365 (Div. 2) B的更多相关文章
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Mishka and Divisors[CodeForces Round #365 Div.2]
http://codeforces.com/contest/703/problem/E 题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和 ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces Round #365 (Div. 2) A 水
A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)
http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces Round #365 (Div. 2) B - Mishka and trip
http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...
- Codeforces Round #365 (Div. 2) A
Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...
随机推荐
- Centos下Yum安装PHP5.5
默认的版本太低了,手动安装有一些麻烦,想采用Yum安装的可以使用下面的方案: 1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包,先删除他们 ...
- 杂项:art-template-loader
ylbtech-杂项:art-template-loader 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://www.npmjs.com/p ...
- Python-requests取消SSL验证的警告InsecureRequestWarning解决办法
使用requests模块请求一个证书无效的网站的话会直接报错 可以设置verify参数为False解决这个问题 # -*- coding:utf-8 -*- __author__ = "Mu ...
- Quartz.NET+TopSelf 实现定时服务
转载http://www.cnblogs.com/jys509/p/4628926.html Quartz.NET 入门 2015-07-09 00:59 by jiangys, 67858 阅读, ...
- Delegate Action<T in> Func<T in,out Tresult> Predicate<T>
action<T> 和 func<T> 都是delegate的简写形式,其中T为可以接受的参数类型 action<T> 指那些只有输入参数,没有返回值 Deleg ...
- Java Learning 000 搭建开发环境
Java Learning 000 搭建开发环境 你需要两个软件: * JDK (Java Develop Kit :Java开发工具包) * eclipse (eclipse 集成开发环境软件) 安 ...
- 手机连接fiddler后,浏览器无法打开网页或者fiddler抓取不到手机应用相关数据的情况
关于手机如何连接fiddler,网上有很多教程,我暂时就不写了 今天在使用fiddler的过程中,发现fiddler突然无法抓取移动端应用的数据包,再三确认连接无误.因此就开始了解决之旅 起因是安卓手 ...
- Django 的认证系统
Django自带的用户认证 auth 模块 from django.contrib import autu django.contrib.auth 中提供了许多方法, 这里主要介绍其中三个: auth ...
- hdu 1576 A/B (求逆元)
题目链接 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Inpu ...
- ExecuteNonQuery(),ExecuteScalar(),ExecuteReader的用法-转
using System.Data.SqlClient;...SqlConnection conn = new SqlConnection(@"server=ws7\leosql;datab ...