SRM589
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的更多相关文章
- SRM589 DV1 250 回文字符串
其实这道题挺简单的,不过刚开始我钻了一个错误的死胡同.想明白之后才发现. 题目要求用最少的时间来将一个字符串变成回文字符串.(具体题目参看topcoder srm589 DV1 250分值的题目,不便 ...
随机推荐
- [zz] pgpool-II load balancing from FAQ
It seems my pgpool-II does not do load balancing. Why? First of all, pgpool-II' load balancing is &q ...
- Eclipse经验总结
1.在Project Explorer中显示src.resource目录:通过Project->properties->java Build Path->Source设置 2.解决J ...
- GridView - javascript 触发后台 OnSelectedIndexChanged
1.ASPX <asp:GridView ID="gdvDealers" runat="server" AutoGenerateColumns=" ...
- Spring Cloud OAuth
In this blog post we will create a secure API for external access, using OAuth 2.0, to the microserv ...
- 符合web标准的网页下拉菜单
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> < ...
- C#快速学习笔记(译)续一
6.虚拟和非虚拟函数 下面是一个非虚拟函数 using System; namespace Test2 { class Plane { public double TopSpeed() {return ...
- OS/400相关介绍
OS/400是IBM公司为其AS/400以及AS/400e系列商业计算机开发的操作系统,由于OS/400的设计充分考虑了AS/400的硬件设计,而且通常作为AS/400的一个基本组件被提供,因此几乎没 ...
- 【非原】c语言之声明和定义的区别
原创地址:http://www.cnblogs.com/haore147/p/3647466.html 什么是定义?什么是声明?它们有何区别? 举个例子: 1 2 A)int i; B)extern ...
- DB天气app冲刺二阶段第三天
今天发现自己不能乖乖的按照计划来进行项目了.弄着这个然后不知不觉就弄到那边去了,结果就是哪边都弄不好,但又很纠结.还是要给自己一个计划白班才行,每次只是计划这一两天的根本不行.明天要指定一个计划白板, ...
- Telerik_2012_Q3 RadGrid 汉化
ChineseRadGridLocalizationProvider.cs using System; using System.Collections.Generic; using System.L ...