Codeforces Round #365 (Div. 2) B - Mishka and trip
http://codeforces.com/contest/703/problem/B
题意:

每个点都有一个值,每条边的权值为这两个点相乘。1~n成环。现在有k个城市,城市与其他所有点都相连,计算出所有边权值和。
思路:
每个城市要与其它所有城市都相连,所以我们可以先去计算城市的,先预处理一下,计算出所有点的值的和,那么每个城市乘以所有点的值的和减去它自身的值。
但是这样的话每两个城市之间就计算了两遍,要减去,具体看代码。
最后再考虑环上的点,如果还有边未计算,就加上去。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL; const int maxn=1e5+; int n,k;
int a[maxn];
int b[maxn];
int vis[maxn];
LL sum;
LL summ; int main()
{
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d%d",&n,&k))
{
sum=; summ=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i]; //所有点的和
vis[i]=;
}
for(int i=;i<=k;i++)
{
scanf("%d",&b[i]);
summ+=a[b[i]]; //城市的和
vis[b[i]]=;
} LL ans=;
for(int i=;i<=k;i++)
{
ans+=a[b[i]]*(sum-a[b[i]]);
} //去重
LL temp=;
for(int i=;i<=k;i++)
{
temp+=a[b[i]]*(summ-a[b[i]]);
}
temp/=;
ans-=temp; //计算环上还未计算的
for(int i=;i<n;i++)
{
if(vis[i]== && vis[i+]==)
ans+=a[i]*a[i+];
}
if(vis[]== && vis[n]==)
ans+=a[]*a[n];
printf("%lld\n",ans);
}
return ;
}
Codeforces Round #365 (Div. 2) B - Mishka and trip的更多相关文章
- 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(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- 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) D.Mishka and Interesting sum 树状数组+离线
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum
题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Codeforces Round #365 (Div. 2) Mishka and trip
Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...
随机推荐
- 如何使用 libtorch 实现 LeNet 网络?
如何使用 libtorch 实现 LeNet 网络? LeNet 网络论文地址: http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf
- cpython解释器内存机制
java虚拟机内存 笼统分为两部分:堆区,栈区 其中,引用在栈区,对象在堆区 详细分为五部分:堆区,虚拟机栈区,本地方法栈区,方法区,程序计数器 cpython解释器内存 笼统分为两部分:堆区,栈区 ...
- vue中获取客户端IP地址(不需要额外引入三方文件)
之前看了几种方法 ,都是引入腾讯,新浪,搜狐等的三方js文件来查询IP地址,但是我自己测试的时候IP地址不准确,所以就找了找,发现了这个方法,准确的获取到了IP地址和cmd的ipconfig获取到的I ...
- 一条SQL引起的雪崩
1.问题描述 MySQL服务器卡死,CPU飚到300%多,命令执行缓慢. 2.问题定位 踩了狗屎运,直接找到了问题缘由 发现了一条SQL写的模糊匹配,将%写在了关键字的前面,这样会造成查询不使用索引, ...
- python web框架 django工程的创建
安装 django pip3 install django pip install django 安装完后出现这两个文件 django-admin 用来创建文件夹 在script目录 执行这个命令 d ...
- centos7提示ifconfig command not found解决
安装centos7时,选择了minimal install的话,没有安装网络组件. yum install net-tools 可以解决问题.
- POJ3903Stock Exchange&&POJ1631Bridging signals最长上升子序列 &&POJ1887Testing the CATCHER(最长下降子序列)(LIS模版题)
题目链接:http://poj.org/problem?id=3903 题目链接:http://poj.org/problem?id=1631 题目链接:http://poj.org/problem? ...
- Webservice介绍
一. Socket和Http通信协议介绍 1. Socket协议简单介绍 Socket位于传输层,它是对Tcp/ip协议的实现,包含TCP/UDP,它是所有通信协议的基础,如下为Socket通 ...
- Look for this newest GS Jordan 6 Floral
Named 'Bulls Over Broadway' and 'Gym Red', the most recent variation from the New Jordans 2015 is fo ...
- angular-selcet
常规用法代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...