题意:龙要制作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. EXTJS 3.0 资料 控件之 GridPanel属性与方法大全

    1.Ext.grid.GridPanel 主要配置项: store:表格的数据集 columns:表格列模式的配置数组,可自动创建ColumnModel列模式 autoExpandColumn:自动充 ...

  2. Unity3d之Socket UDP协议

    原文地址:http://blog.csdn.net/dingkun520wy/article/details/49201245 (一)Socket(套接字)UDP协议的特点 1.是基于无连接的协议,没 ...

  3. 【BZOJ 2005】[Noi2010]能量采集

    Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得 ...

  4. Matlab中添加搜索目录

    一.问题来源 来自于一份大规模hash图像检索代码. 二.问题解析 2.1 添加目录 addpath('./utils/'); 2.2 添加目录及其子目录 addpath(genpath('./uti ...

  5. sql server 2012 5120错误

    把放置放置数据库的文件夹的权限更改为完全控制即可解决这个问题.

  6. hdu 1028

    递推 #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> ...

  7. 1964-NP

    描述 Problems in Computer Science are often classified as belonging to a certain class of problems (e. ...

  8. linux Ubuntu安装后没有引导 解决方案

    用EasyBCD添加ubuntu grub2引导,适用于12.04 及之前版本的ubuntu安装好easybcd后运行,之后看图

  9. oracle 增加字段

    之前很多表增加很多相同的字段,一个一个添加太慢烦了,于是用了以下的方法alter table t_xmlc_batch_out_head_bak add ( SENDRECEIVEFLAG ) , S ...

  10. Ubuntu Server 中resolv.conf重启时被覆盖的问题

    /etc/resolv.conf中设置dns之后每次重启Ubuntu Server时该文件会被覆盖,针对这种情况找了一些个解决方法 防止/etc/resolv.conf被覆盖的方法 方法一 1.需要创 ...