250:

给一个串S,可以做这样的操作,可以将串中的一种字母变成另一种字母,代价是该种字母的数量。求解的问题是,最小的代价将串S变成回文串。

根据回文关系,我们可以形成等价对应关系,a与b等价对应说明a和b必须是同种字母,根据这个关系,我们可以得到一个图,每个连通块表示要变成一种相同的字母,而这个操作的最小代价就是将连通块中除出现次数最多的字母全部都转变成出现次数最多的字母。

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std; bool mm[][];
int cnt[];
bool used[]; class GooseTattarrattatDiv1 {
public:
int getmin(string S) {
memset(cnt, , sizeof(cnt));
memset(mm, false, sizeof(mm));
memset(used, false, sizeof(used));
int n = S.size();
for (int i = ; i < n; i++) {
mm[S[i]][S[n - - i]] = true;
mm[S[n - - i]][S[i]] = true;
cnt[S[i]]++;
}
int res = ;
for (int i = ; i < ; i++) {
int sum = , max = ; dfs(i, sum, max);
res += (sum - max);
}
return res;
}
void dfs(int i, int& sum, int& max) {
used[i] = true;
sum += cnt[i];
if (cnt[i] > max) max = cnt[i]; for (int j = ; j < ; j++) {
if (mm[i][j] && used[j] == false) {
dfs(j, sum, max);
}
}
}
};

500:

有R,G,B三种齿轮,不同颜色齿轮之间不会相接,相接的关系通过graph给出,同种颜色齿轮旋转方向相同,旋转方向一共有两种:顺时针与逆时针。求解如何去掉最少的齿轮使得该graph能相容,即可以找到某种旋转顺序使得不会发生矛盾(矛盾指相接的两个齿轮旋转方向相同)。

同种颜色的点归于一个集合,因此我们有三个集合,R,G,B,集合内的点不会有边,这个图由三个二分图组成,因为有3个集合,枚举一下齿轮的旋转方向,一共有三种情况:RG同向,GB同向,BR同向。于是问题转换成这三种情况中旋转最小值,对于每个情况,去掉最少的齿轮使其不矛盾便是一个最小点覆盖问题,二分图的最小点覆盖可以转变成二分图的最大匹配问题。

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; class GearsDiv1 {
private:
vector<int> left, right;
vector<string> G;
bool s[];
int link[];
public:
int getmin(string color, vector <string> graph) {
vector<int> r, g, b;
for (int i = ; i < color.length(); i++) {
if (color[i] == 'R') r.push_back(i);
else if (color[i] == 'G') g.push_back(i);
else if (color[i] == 'B') b.push_back(i);
}
int rg = max_bimatch(r, g, graph);
int gb = max_bimatch(g, b, graph);
int br = max_bimatch(b, r, graph);
return min(rg, min(gb, br));
}
int max_bimatch(vector<int> l, vector<int> r, vector<string> graph) {
//
left = l; right = r; G = graph; int res = ;
memset(link, -, sizeof(link));
for (int i = ; i < left.size(); i++) {
memset(s, false, sizeof(s));
if (dfs(i)) res++;
} return res;
}
bool dfs(int i) {
for (int j = ; j < right.size(); j++) {
if (G[left[i]][right[j]] == 'Y' && s[right[j]] == false) {
s[right[j]] = true;
if (link[right[j]] == - || dfs(link[right[j]])) {
link[right[j]] = i;
return true;
}
}
}
return false;
}
};

SRM589的更多相关文章

  1. SRM589 DV1 250 回文字符串

    其实这道题挺简单的,不过刚开始我钻了一个错误的死胡同.想明白之后才发现. 题目要求用最少的时间来将一个字符串变成回文字符串.(具体题目参看topcoder srm589 DV1 250分值的题目,不便 ...

随机推荐

  1. iOS进阶——App生命周期

    State Description Not running The app has not been launched or was running but was terminated by the ...

  2. asp.net 中使用less

    首先 ,需要知道 whats the less; 实际上less 只是针对css比较难于维护和抽象这种现象,而创造的一个工具. 然后,在抛开语言环境的情况下(例如.net 是vs环境,java是ecl ...

  3. 基于MRG_MyISAM引擎的Mysql分表

    正常情况下的分表,都是直接创建多个相同结构的表,比如table_1.table_2...最近碰到一个特殊需求,需要创建一个主表,所有分表的数据增删改查,全部自动实时更新到主表,这个时候可以使用MRG_ ...

  4. Easyui 生成layout

    Easyui 生成layout var $tabs; var $body; var $south; function appendLayout(title, href) { if (!$body) $ ...

  5. grails导入excel

    grails导入excel,意思是说从excel表中读取多条数据,批量写入数据库. 有2种方案,1是使用grails的excel插件,2是调用java代码使用POI等第三方java控件. 今天比较累, ...

  6. Android 控件收集

    SwipeMenuExpandableListView   https://github.com/tycallen/SwipeMenu-Expandable-ListView

  7. WPF中的文字修饰

    我们知道,文字的修饰包括:空心字.立体字.划线字.阴影字.加粗.倾斜等.这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线.中划线.基线与下划线.如图: 从上至下,分别为上划线(Overl ...

  8. POJ 3701 概率DP

    给定2^n 支足球队进行比赛,n<=7. 队伍两两之间有一个获胜的概率,求每一个队伍赢得最后比赛的概率是多少? 状态其实都是很显然的,一开始觉得这个问题很难啊,不会.dp[i][j] 表示第i支 ...

  9. 【BZOJ 1010】 [HNOI2008]玩具装箱toy

    Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...

  10. 【HDU2222】Keywords Search

    Problem DescriptionIn the modern time, Search engine came into the life of everybody like Google, Ba ...