Codeforces 703B Mishka and trip
来源:互联网 发布:淘宝旺铺有什么好处 编辑:程序博客网 时间:2024/06/06 01:04
题意:有n个城市,编号为1~n,相邻的两个城市之间有一条路,路的权值是两个端点城市的权值的乘积,在这n个城市中选出k个中心城市,中心城市与其余的所有城市之间都有一条路径,求整张地图所有路径的权值和
解题思路:模拟.如果直接枚举相邻两个城市间的路径会超时。可以用a[k]*a[1]+a[k]*a[2]+a[k]*a[3]+...=a[k]*(a[1]+a[2]+a[3]+...),求出n个城市的权值和和k个中心城市的权值和,如果i是普通城市,就要与与它相邻的两个城市和中心城市的权值和相乘,这时还要判断与它相邻的两座城市里有没有中心城市,如果有的话要减掉,避免算重复了,如果i是中心城市,直接乘以除了它本身以外的所有城市的权值和即可,最后的总和要除以2,因为每条路都算了两遍,注意要用long long,否则会WA
代码:
#include <iostream>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <cstdio>using namespace std;typedef long long ll;int main(){ ll n,k; while(scanf("%I64d%I64d",&n,&k)==2) { ll a[100005],kk[100005]; ll sum=0,sumk=0; for(ll i=0; i<n; i++) { scanf("%I64d",&a[i]); sum+=a[i]; } for(ll i=0; i<k; i++) { scanf("%I64d",&kk[i]); kk[i]--; sumk+=a[kk[i]]; } ll temp=0,ans=0; for(ll i=0,j=0; i<n; i++) { ll p,q; if(i==0){p=n-1;q=1;} else if(i==n-1){p=n-2;q=0;} else {p=i-1;q=i+1;} if(i==kk[j]) { temp=sum-a[i]; j++; } else { temp=a[p]+a[q]+sumk; int pos1=lower_bound(kk,kk+k,p)-kk; if(kk[pos1]==p)temp-=a[p]; int pos2=lower_bound(kk,kk+k,q)-kk; if(kk[pos2]==q)temp-=a[q]; } ans+=a[i]*temp; } printf("%I64d\n",ans/2); } return 0;}
阅读全文
0 0
- codeforces 703B Mishka and trip
- 【Codeforces 703B - Mishka and trip】
- Codeforces-703B Mishka and trip
- CodeForces 703B Mishka and trip(图论)
- CodeForces 703B Mishka and trip【水题】
- CodeForces 703B Mishka and trip
- CodeForces 703B Mishka and trip
- codeforces 703B. Mishka and trip
- Codeforces 703B Mishka and trip
- codeforces B. Mishka and trip
- 703B - Mishka and trip
- (模拟+数学)codeforces - 703B Mishka and trip
- Mishka and trip(CF 703B)
- Coderforces 703B Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- Codeforces Round #365 (Div. 2) -- B. Mishka and trip
- Codeforces Round #365 (Div. 2) [B] Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- 分页插件PageHelper使用方法
- 【链表】查找链表倒数第k个结点,要求只能遍历一次
- Python: TypeError: 'int' object is not callable
- Codeforces Round #424 (Div. 2) A Unimodal Array 思维题
- 机器学习实战读书笔记-第一章k-近邻算法
- Codeforces 703B Mishka and trip
- gpio子系统和pinctrl子系统(一)
- 文章标题
- 【更新】Word控件 Spire.Doc 6.0大版本发布 | 形状文件和Word XML文档极大改进
- springmvc出现:No mapping found for HTTP
- HTML5网页录音和压缩
- Nginx反向代理,负载均衡,redis session共享,keepalived高可用
- 学习笔记
- (56)优先级以及yield用法