//對于每個朋友圈,我們設立一個boss,代表這個朋友圈的頭頭
//并且設立一個全局數組用來存儲每個人對應的boss資訊
//我們想得到的結果是朋友圈中所有人的boss是同一個人
全局變量:circle數組//用于存儲boss資訊
find函數:int x //這是傳參,下同,函數功能是查找x的boss
查找x在circle中的boss,如果x本身就是一個boss,傳回x;
如果不是,繼續遞歸find(circle[x]),一級一級地找x的最終boss,并将boss的值傳回,賦予circle[x]
//将傳回的boss的值賦予circle[x]很重要,因為所有圈中的boss可能合并導緻boss改變
傳回x的boss
unit函數:int person1,int person2//功能:聯合person1和person2所在的朋友圈
find函數查找person1的boss
find函數查找person2的boss
如果兩人的boss不相同,就讓person2的boss做person1的boss的小弟
//也就是令person2的boss在數組中對應的值為person1的boss
main函數:
cin >> m,n
根據m對circle數組初始化
while(n--)//讀入朋友圈
讀取第一個人bigBoss作為這個朋友圈的boss
讀入剩下的所有人,并用unit函數将他們聯合為一個朋友圈
//在未聯合之前,每個人都是自己的boss
static int result[]//統計每個boss小弟人數
int max = 0//最大值
for i = 1 to m:
int b = i 的boss
result[b]++
if result[b] > max:
max=result
cout << max
main函數:
int wood數組
for i = 1 to n:
讀入每根木頭的長度
sort函數對wood數組進行快速排序//畢竟C++有,就直接用了。不然冒泡半天
for i = 0 to n-2:
将wood[i]和wood[i+1]相加得到和plus
sum += plus //sum為總消費
将和按照升序插入到後面的數組中//本想着這裡也用sort但是後面實驗了發現會逾時
cout << sum
//讀取人物關系資訊
map<string, string> relation
stack<string> family
while(1)
讀取一個人名name
if 空棧:
直接入棧
continue
if 棧頂是 name的父親:
relation[name] = 棧頂//建立關系
入棧name
else
不斷退棧直到棧頂是name的父親
relation[name] = 棧頂
入棧name
//解決輸入的問題,采用遞歸
solve函數:string name1,name2,re//re是關系
switch(re[0])
case c:判斷name2是否是name1的父親,傳回判斷結果//判斷是不是兒子
case p:同上//判斷是不是父母
case s:同上//判斷是不是兄弟
case a:GrandSearch(name1,name2,re)//判斷是不是祖先
case d:GrandSearch(name2,name1,re)//判斷是不是祖孫
GrandSearch函數:
if name1是唯一的祖先:
return false
if name1的父親是name2:
return true
else return GrandSearch(name1的父親,name2)//遞歸函數不好講,,就是不斷找name1的父親,直到找不到或者name1的父親就是name2為止
It is known that there are N cats in Quasrain's home. The weight of cat i is A[i]. As Quasrain is evil, he wants to do something to the cats.
Practicing for two years and a half, the cats have gotten the commands of Quasrain. Whenever Quasrain say a a color, the cats in the actual color would come out, and Quasrain would cost sum of their weight. For example, now Quasrain has three cats in red, weights of {1,2,3}, and two cats in green, weights of {7,8} .If he says red, he would cost 6 (green for 15). Then he would choose some of them, would never stop until there isn't any pair of cats with the same color.
Initially, the cats are all in color white. Quasrain want to know, how much he wanld cost at least.
Input
The first line is a number N(2<=N<=10^5)
Then one line with N numbers, A[i] for the weight of cats i.(1<=A[i]<=10^9)
Output
Only one number, representing the least cost of Quasrain.