【打CF,學(xué)算法——二星級】Codeforces 703B Mishka and trip (統(tǒng)計(jì))
【CF簡介】
題目鏈接:CF 703B
題面:
A - Mishka and trip Time Limit:1000MS????Memory Limit:262144KB????64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 703BDescription
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 a and b you are to find sum of products ca·cb. Will you help her?
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.
Sample Input
Input4 1 2 3 1 2 3Output
17Input
5 2 3 5 2 2 4 1 4Output
71
Hint
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-n個點(diǎn),k個點(diǎn)是關(guān)鍵點(diǎn),關(guān)鍵點(diǎn)會向所有點(diǎn)連邊,非關(guān)鍵點(diǎn)只向兩邊連邊,首尾相接,兩點(diǎn)之間最多只有一條邊,兩點(diǎn)間邊的代價,是兩個點(diǎn)的值的乘積,問所有邊的總代價是多少?
解題:
?? 簡單統(tǒng)計(jì)所有邊的代價是很簡單的,但是因?yàn)辄c(diǎn)數(shù)較多,這樣是會超時的。可以預(yù)先統(tǒng)計(jì)出所有點(diǎn)的總代價,所有關(guān)鍵點(diǎn)的總代價。計(jì)算時,將所有邊統(tǒng)計(jì)兩遍,其中非關(guān)鍵點(diǎn)較好處理,只要向兩邊連邊,而關(guān)鍵點(diǎn)向所有點(diǎn)連兩倍邊,關(guān)鍵點(diǎn)之間會有四條邊,要后期減去,而關(guān)鍵點(diǎn)和不是相鄰的非關(guān)鍵點(diǎn)之間就是兩倍邊,而關(guān)鍵點(diǎn)和相鄰的非關(guān)鍵點(diǎn)(如果是的話)是三倍邊,要減去一次。最后,統(tǒng)計(jì)完之后,將總代價除以2,即為所求。
代碼:
#include
#include
#define LL long long
#define sz 100005
using namespace std;
//每個點(diǎn)val
LL val[sz];
//是否是capital
bool vis[sz];
//哪些點(diǎn)是capital
int keyp[sz];
int main()
{
int n,k,tmp;
//sum1是所有點(diǎn)的和,sum2是capital的和
LL sum1=0,ans=0,sum2=0,tmp2=0;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%lldn",&val[i]);
sum1+=val[i];
}
for(int i=1;i<=k;i++)
{
scanf("%d",&tmp);
vis[tmp]=1;
keyp[i]=tmp;
sum2+=val[tmp];
}
//首尾連接
val[0]=val[n];
vis[0]=vis[n];
val[n+1]=val[1];
vis[n+1]=vis[1];
//所有邊計(jì)算兩次
for(int i=1;i<=n;i++)
{
//是capital那么它連出去的邊都計(jì)算兩次
if(vis[i])
{
ans+=2*(sum1-val[i])*val[i];
//如果前一個點(diǎn)不是capital,那么要減去一次,因?yàn)榉莄apital點(diǎn)也會向兩邊連邊
if(!vis[i-1])
ans-=val[i]*val[i-1];
if(!vis[i+1])
ans-=val[i]*val[i+1];
}
//非capital點(diǎn)向兩邊連邊
else
ans+=val[i]*(val[i+1]+val[i-1]);
}
//關(guān)鍵點(diǎn)因?yàn)橄蛩悬c(diǎn)都連了兩次,關(guān)鍵點(diǎn)之間連了4次,故需減去2次
for(int i=1;i<=k;i++)
{
tmp2+=(sum2-val[keyp[i]])*val[keyp[i]];
}
ans-=tmp2;
//因?yàn)樗羞叾冀y(tǒng)計(jì)了兩次
printf("%lldn",ans/2);
return 0;
}