题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值。

解法:克鲁斯卡尔。将茶的配方作为点,将每两个点之间的差值作为边权,求最小生成树,这棵树中最大的边即为答案。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
string s[1005];
int cal(int i, int j)
{
int res = 0;
for(int k = 0; k < s[i].size(); k++)
res = max(res, abs(s[i][k] - s[j][k]));
return res;
}
struct node
{
int u, v;
int val;
node(int u, int v, int val) : u(u), v(v), val(val) {}
node() {}
bool operator < (const node &tmp) const
{
return val < tmp.val;
}
};
vector <node> edge;
int father[1005];
int Find(int a)
{
if(a != father[a])
father[a] = Find(father[a]);
return father[a];
}
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m))
{
for(int i = 0; i < 1005; i++)
father[i] = i;
for(int i = 0; i < n; i++)
{
cin >> s[i];
}
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++)
{
edge.push_back(node(i, j, cal(i, j)));
}
int ans = 0;
sort(edge.begin(), edge.end());
int len = edge.size();
for(int i = 0; i < len; i++)
{
int a = Find(edge[i].u), b = Find(edge[i].v);
if(a != b)
{
father[a] = b;
ans = edge[i].val;
}
}
printf("%d\n", ans);
}
return 0;
}

  

CF GYM 100703A Tea-drinking的更多相关文章

  1. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  2. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  3. cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

    题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...

  4. CF Gym 100685A Ariel

    传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  6. CF GYM 100703B Energy Saving

    题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...

  7. CF GYM 100703F Game of words

    题意:两个人玩n个游戏,给出每人玩每个游戏的时间,两个人需要在n个游戏中挑m个轮流玩,求最短时间. 解法:dp.(这场dp真多啊……话说也可以用最小费用最大流做……然而并不会XD)dp[i][j][k ...

  8. CF GYM 100703G Game of numbers

    题意:给n个数,一开始基数为0,用这n个数依次对基数做加法或减法,使基数不超过k且不小于0,输出最远能运算到的数字个数,输出策略. 解法:dp.dp[i][j]表示做完第i个数字的运算后结果为j的可能 ...

  9. CF GYM 100703I Endeavor for perfection

    题意:有n个学习领域,每个领域有m个课程,学习第i个领域的第j个课程可以获得sij个技能点,在每个领域中选择一个课程,要求获得的n个技能点的最大值减最小值最小,输出符合要求的策略. 解法:尺取法.将课 ...

随机推荐

  1. easyui使用时出现这个Uncaught TypeError: Cannot read property 'combo' of undefined

    easyui使用时出现这个Uncaught TypeError: Cannot read property 'nodeName' of undefined 最后检查发现是必须给select一个id,光 ...

  2. eclipse *.vm 文件,语法高亮

    eclipse *.vm 文件,语法高亮按如下方式不起作用,原因不明.设置文件打开时使用的编辑器General>>Editors>>File Associations 下述方试 ...

  3. dictionary 和 hashtable 区别

    区别:1,Dictionary支持泛型,而Hashtable不支持. 2,Dictionary没有装填因子(Load Facto)概念,当容量不够时才扩容(扩容跟Hashtable一样,也是两倍于当前 ...

  4. 漫话C++0x(五)—- thread, mutex, condition_variable

    熟悉C++98的朋友,应该都知道,在C++98中没有thread, mutex, condition_variable这些与concurrency相关的特性支持,如果需要写多线程相关程序,都要借助于不 ...

  5. hdu 1847 Good Luck in CET-4 Everybody! 博弈论

    方法一:找规律,很容易知道 #include<stdio.h> int main(){ int n; while(scanf("%d",&n)!=EOF){ p ...

  6. VMware linux与windows文件共享

    将要共享的文件做成一个iso文件,然后打开VMware

  7. PowerDesigner将name自动添加到Comment注释的方法 VB代码

    Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' the current model ' get t ...

  8. 安卓从业者应该关注:Android 6.0的运行时权限

    Android 6.0,代号棉花糖,自发布伊始,其主要的特征运行时权限就很受关注.因为这一特征不仅改善了用户对于应用的使用体验,还使得应用开发者在实践开发中需要做出改变. 没有深入了解运行时权限的开发 ...

  9. Centos后台运行jar

    jar后台运行 nohup java -jar xx.jar >/dev/null & 此处的">/dev/null"作用是将终端输出信息输出到空洞中,即不保存 ...

  10. svn的merge使用例子

    先说说什么是branch.按照Subversion的说法,一个branch是某个development line(通常是主线也即trunk)的一个拷贝,见下图: branch存在的意义在于,在不干扰t ...