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个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...
随机推荐
- java基础05 选择结构
选择结构 public class Demo01Change { public static void main(String[] args) { /** * 实现等量的转换 */ int a = 5 ...
- 【opencv】imread 赋值 深拷贝浅拷贝
import cv2 import copy import os def filter_srcimg(dstimg): ss=3 srcimg=copy.deepcopy(dstimg) #aa=5 ...
- 一.shell基础知识
参考网站:http://billie66.github.io/TLCL/book/chap08.html 1.字符“*”--展开 [me@linuxbox ~]$ echo * Desktop Doc ...
- golang官方实现如何对httpserver做频率限制(最大连接数限制)
一般海量处理服务,都会对服务做个最大连接数限制,超过该限制之后,拒绝服务,避免发生雪崩,压坏服务. 使用golang来编写httpserver时,如何进行呢?官方已经有实现好的包. 使用示例: imp ...
- 利用Octopress在Github上搭建博客及后续问题总汇
首先贴一下我的新博客地址: http://findingsea.github.io 用Octopress在GitHub上搭建博客已经不是什么新鲜事了,网上的教程也多了去了,大题的方法什么都差不多,这篇 ...
- SIP中的 session, dialog 及 transaction 的解释
http://stackoverflow.com/questions/35133331/difference-between-session-dialog-and-transaction-in-sip ...
- Ubuntu apt-get更换阿里云源
sudo vim /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe ...
- 《浅谈CT》总结
注明来自 http://www.ssdfans.com/?p=1941 这里说的CT,不是医院里面的CT,而是闪存的一种技术:Charge Trap. 闪存不只有Floating Gate,还有Cha ...
- JAVA中重写equals()方法为什么要重写hashcode()方法?
object对象中的 public boolean equals(Object obj),对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true:注意:当此方法 ...
- selenium 模块
介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如 ...