當(dāng)前位置:首頁 > 芯聞號 > 充電吧
[導(dǎo)讀]【CF簡介】 題目鏈接:CF 703B 題面: A - Mishka and trip Time Limit:1000MS????Memory Limit:262144KB????64bit I

【CF簡介】

題目鏈接:CF 703B

題面:

A - Mishka and trip Time Limit:1000MS????Memory Limit:262144KB????64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 703B

Description

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

Input
4 1
2 3 1 2
3
Output
17
Input
5 2
3 5 2 2 4
1 4
Output
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;
}


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運(yùn)營商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學(xué)會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團(tuán))股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉