codeforces703B
Mishka and trip
小米什卡是一个伟大的旅行者,她访问了许多国家。在这次考虑去哪里旅行之后,她选择了XXX--这个美丽,但鲜为人知的北方国家。
以下是关于XXX的一些有趣事实:
XXX由n个城市组成,其中k个(只是想象!)是省会城市。
这个国家的所有城市都很漂亮,但每个城市都很独特。第i个城市的美丽值等于ci。
所有城市通过道路连续连接,包括第1和第n个城市,形成循环路线1 - 2 - … - n - 1
每个省会城市都直接与其他所有城市相连。
任何两个城市之间最多只有一条公路。
通过道路的价格直接取决于它所连接的城市的美丽值。因此,如果城市i和j之间存在道路,则通过它的价格等于ci·cj。
米什卡开始收集她的东西去旅行,但是还没有决定走哪条路线,因此她请求你帮助她确定通过XXX中每条道路的总价格。你会帮她吗?
Input
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.
Output
Print the only integer — summary price of passing each of the roads in XXX.
Example
4 1
2 3 1 2
3
17
5 2
3 5 2 2 4
1 4
71
Note
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.
sol:比较坑,先O(n)扫一遍求出无省会城市的边权和,然后计算每个省会城市的所有边权和,但这样两个省会城市之间的边权会被统计两次,要把他们去掉
Ps:要小心,把所有相邻的都减掉
/*
一组hack数据
input
3 3
1 1 1
1 2 3
output
3
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,K,Shengh[N];
ll Cost[N];
int main()
{
int i;
ll Sum=,SS=,ans=;
R(n); R(K);
for(i=;i<=n;i++)
{
Sum+=(Cost[i]=read());
if(i>=) ans+=Cost[i]*Cost[i-];
}
ans+=Cost[]*Cost[n];
for(i=;i<=K;i++)
{
SS+=Cost[Shengh[i]=read()];
ans+=Cost[Shengh[i]]*(Sum-Cost[(Shengh[i]-+n)%n+]-Cost[Shengh[i]%n+]-Cost[Shengh[i]]);
}
sort(Shengh+,Shengh+K+);
for(i=;i<K;i++)
{
ll oo=(SS-=Cost[Shengh[i]]);
if(Shengh[i+]==Shengh[i]+) oo-=Cost[Shengh[i+]];
if(Shengh[K]==(Shengh[i]-+n)%n+) oo-=Cost[Shengh[K]];
ans-=Cost[Shengh[i]]*oo;
}
Wl(ans);
return ;
}
/*
input
4 1
2 3 1 2
3
output
17 input
5 2
3 5 2 2 4
1 4
output
71 input
3 3
1 1 1
1 2 3
output
3
*/
codeforces703B的更多相关文章
- Codeforces703B Mishka and trip
题意: 就是有n个点,本来相邻点之间就有一条边,1和n之间也有一条,然后给你几个特殊点,说这些特殊点和其他所有点都连起来了,然后算一个所有边的权值和,每条边的权值等于两个点的c相乘. 思路: 水题啊- ...
随机推荐
- 使用Git进行代码管理的心得--github for windows
首先简述一下Git进行代码管理的情况 我使用的是github for windows,官网下载的速度太慢,所以用了离线安装包.安装之后会有GitHub和GitShell两个软件,其中Github采用图 ...
- .NET Core 中的路径问题
NET Core 应用程序相对于以前的.NET Framework 应用程序在启动运行的方式上有一定的差异,今天就来谈一谈这个获取应用程序启动路径的问题. 1.工作路径 WorkingDirector ...
- 《了不起的 nodejs》中 TwitterWeb 案例 bug 解决
了不起的nodejs算是一本不错的入门书,不过书中个别案例存在bug,按照书中源码无法做出和书中相同效果,原本兴奋的心情掺杂着些许失落. 现在我们看一下第七章HTTP,一个 Twitter Web 客 ...
- 安卓权限申请处理框架Android-UsesPermission
安卓权限申请封装处理框架.测试支持4.0+.项目源于正式处理Android权限问题时,没找到简单.能满足被拒绝权限自动会到系统设置处理的框架,按自己的编程习惯造一个熟悉的轮子还是蛮好的.第一次使用An ...
- 【C#复习总结】细说 Lambda表达式
1 前言 本系列会将[委托] [匿名方法][Lambda表达式] [泛型委托] [表达式树] [事件]等基础知识总结一下.(本人小白一枚,有错误的地方希望大佬指正) 系类1:细说委托 系类2:细说匿名 ...
- ubuntu 添加开机启动服务
新建umpserver.service [Unit] Description=UMPServer After=syslog.target network.target remote-fs.target ...
- Python全栈开发之路 【第四篇】:Python基础之函数
本节内容 函数def: 1.位置参数,默认参数 2.位置参数,关键参数 3.如果参数中出现 *users,传递的参数就可以不再是固定的个数, 传过来的所有元素进行打包成元组 *args,**kwarg ...
- Python_每日习题_0007_copy
题目:将一个列表的数据复制到另一个列表中. 程序分析:使用列表[:],拿不准可以调用copy模块 import copy a = [,,,,['a','b']] b = a #赋值 c = a[:] ...
- [2019BUAA软工助教]第一次阅读 - 小结
[2019BUAA软工助教]第一次阅读 - 小结 一.评分规则 总分 16 分,附加 2 分,共 18 分 markdown格式统一且正确 - 2分 不统一:扣 1 分 不正确:扣 1 分(例如使用代 ...
- An error occurred while updating the entries. See the inner exception for details.
EF插入或更新数据时出现错误提示:An error occurred while updating the entries. See the inner exception for details.的 ...