www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

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

【CF簡(jiǎn)介】

題目鏈接: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個(gè)點(diǎn),k個(gè)點(diǎn)是關(guān)鍵點(diǎn),關(guān)鍵點(diǎn)會(huì)向所有點(diǎn)連邊,非關(guān)鍵點(diǎn)只向兩邊連邊,首尾相接,兩點(diǎn)之間最多只有一條邊,兩點(diǎn)間邊的代價(jià),是兩個(gè)點(diǎn)的值的乘積,問所有邊的總代價(jià)是多少?


解題:

?? 簡(jiǎn)單統(tǒng)計(jì)所有邊的代價(jià)是很簡(jiǎn)單的,但是因?yàn)辄c(diǎn)數(shù)較多,這樣是會(huì)超時(shí)的。可以預(yù)先統(tǒng)計(jì)出所有點(diǎn)的總代價(jià),所有關(guān)鍵點(diǎn)的總代價(jià)。計(jì)算時(shí),將所有邊統(tǒng)計(jì)兩遍,其中非關(guān)鍵點(diǎn)較好處理,只要向兩邊連邊,而關(guān)鍵點(diǎn)向所有點(diǎn)連兩倍邊,關(guān)鍵點(diǎn)之間會(huì)有四條邊,要后期減去,而關(guān)鍵點(diǎn)和不是相鄰的非關(guān)鍵點(diǎn)之間就是兩倍邊,而關(guān)鍵點(diǎn)和相鄰的非關(guān)鍵點(diǎn)(如果是的話)是三倍邊,要減去一次。最后,統(tǒng)計(jì)完之后,將總代價(jià)除以2,即為所求。


代碼:

#include 
#include 
#define LL long long
#define sz 100005
using namespace std;
//每個(gè)點(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];
		  //如果前一個(gè)點(diǎn)不是capital,那么要減去一次,因?yàn)榉莄apital點(diǎn)也會(huì)向兩邊連邊
	      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)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動(dòng)電源

在工業(yè)自動(dòng)化蓬勃發(fā)展的當(dāng)下,工業(yè)電機(jī)作為核心動(dòng)力設(shè)備,其驅(qū)動(dòng)電源的性能直接關(guān)系到整個(gè)系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動(dòng)勢(shì)抑制與過流保護(hù)是驅(qū)動(dòng)電源設(shè)計(jì)中至關(guān)重要的兩個(gè)環(huán)節(jié),集成化方案的設(shè)計(jì)成為提升電機(jī)驅(qū)動(dòng)性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機(jī) 驅(qū)動(dòng)電源

LED 驅(qū)動(dòng)電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個(gè)照明設(shè)備的使用壽命。然而,在實(shí)際應(yīng)用中,LED 驅(qū)動(dòng)電源易損壞的問題卻十分常見,不僅增加了維護(hù)成本,還影響了用戶體驗(yàn)。要解決這一問題,需從設(shè)計(jì)、生...

關(guān)鍵字: 驅(qū)動(dòng)電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動(dòng)電源的公式,電感內(nèi)電流波動(dòng)大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計(jì) 驅(qū)動(dòng)電源

電動(dòng)汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動(dòng)汽車的核心技術(shù)之一是電機(jī)驅(qū)動(dòng)控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機(jī)驅(qū)動(dòng)系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動(dòng)汽車的動(dòng)力性能和...

關(guān)鍵字: 電動(dòng)汽車 新能源 驅(qū)動(dòng)電源

在現(xiàn)代城市建設(shè)中,街道及停車場(chǎng)照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進(jìn)步,高亮度白光發(fā)光二極管(LED)因其獨(dú)特的優(yōu)勢(shì)逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動(dòng)電源 LED

LED通用照明設(shè)計(jì)工程師會(huì)遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動(dòng)電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動(dòng)電源的電磁干擾(EMI)問題成為了一個(gè)不可忽視的挑戰(zhàn)。電磁干擾不僅會(huì)影響LED燈具的正常工作,還可能對(duì)周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動(dòng)電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機(jī)重量也有所下降,所以,現(xiàn)在的LED驅(qū)動(dòng)電源

關(guān)鍵字: LED 驅(qū)動(dòng)電源 開關(guān)電源

LED驅(qū)動(dòng)電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動(dòng)LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動(dòng)電源
關(guān)閉