//对于每个朋友圈,我们设立一个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.