POJ 1944 - Fiber Communications
原题地址:http://poj.org/problem?id=1944
题目大意:有n个点排成一圈,可以连接任意两个相邻的点,给出 p 对点,要求这 p 对点必须直接或间接相连,求最少的连接边数
数据范围:n <= 1000, p <= 10000
算法分析:
一开始当最小生成树做的,才发现自己 SB 了……
先考虑不是环形而是线形的结构,直接贪心连接每两个点之间的所有点就好了。这样我们可以枚举环形的断点,然后逐次贪心,求最小解即可
很多同学在贪心的时候应用了线段树是复杂度高达O(np log n),其实丝毫没有必要,我们只需要每次断点时生成一个数组,在每对点的左边点处加1,再在右边点处减1,然后求一下部分和,部分和中正数的个数即为所求(详见代码)
参考代码:
//date 20140205
#include <cstdio>
#include <cstring> const int maxn = ;
const int maxp = ;
const int INF = 0x7F7F7F7F; inline void swap(int &a, int &b){int x = a; a = b; b = x;}
inline int min(int a, int b){return a < b ? a : b;} int n, p;
int pa[maxp][];
int s[maxn << ]; int main()
{
scanf("%d%d", &n, &p);
for(int i = ; i <= p; ++i)
{
scanf("%d%d", &pa[i][], &pa[i][]);
if(pa[i][] > pa[i][])swap(pa[i][], pa[i][]);
} int ans = INF;
for(int i = ; i <= n; ++i)
{
memset(s, , sizeof s);
for(int j = ; j <= p; ++j)
{
int x = pa[j][], y = pa[j][];
if(x <= i)x += n;
if(y <= i)y += n;
if(x > y)swap(x, y);
++s[x]; --s[y];
}
int now = , res = ;
for(int j = ; j <= n; ++j)
{
now += s[i + j];
if(now > )++res;
}
ans = min(ans, res);
}
printf("%d\n", ans);
return ;
}
POJ 1944 - Fiber Communications的更多相关文章
- POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)
题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...
- POJ 1944:Fiber Communications
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4236 Accepted: 1 ...
- [USACO2002][poj1944]Fiber Communications(枚举)
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3804 Accepted: 1 ...
- POJ1944 Fiber Communications (USACO 2002 February)
Fiber Communications 总时间限制: 1000ms 内存限制: 65536kB 描述 Farmer John wants to connect his N (1 <= N ...
- TOJ1550: Fiber Communications
1550: Fiber Communications Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal ...
- POJ 2579 Fiber Network(状态压缩+Floyd)
Fiber Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3328 Accepted: 1532 Des ...
- usaco 2002 月赛 Fiber Communications 题解
Description Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a ...
- POJ 2570 Fiber Network(最短路 二进制处理)
题目翻译 一些公司决定搭建一个更快的网络.称为"光纤网". 他们已经在全世界建立了很多网站.这 些网站的作用类似于路由器.不幸的是,这些公司在关于网站之间的接线问题上存在争论,这样 ...
- ZOJ 1967 POJ 2570 Fiber Network
枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include< ...
随机推荐
- HDU 1104 Remainder
与前一题类似,也是BFS+记录路径, 但是有很多BUG点, 第一MOD操作与%不同i,其实我做的时候注意到了我们可以这样做(N%K+K)%K就可以化为正数,但是有一点要注意 N%K%M!=N%M%K; ...
- Codeforces Round #243 (Div. 1) A题
http://codeforces.com/contest/425/problem/A 题目链接: 然后拿出这道题目是很多人不会分析题目,被题目吓坏了,其中包括我自己,想出复杂度,一下就出了啊!真是弱 ...
- javascript陷阱,一不小心你就中招了
- spring security 构造函数初始化bean思路
采用有参数的构造方法来解决注入你要的属性例如:public MyInvocationSecurityMetadataSource(RoleService roleService) { this.rol ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- Android activity属性
android:allowTaskReparenting 是否允许activity更换从属的任务,比如从短信息任务 切换到浏览器任务. android:alwaysRetainTaskState 是否 ...
- NTP服务及时间同步(CentOS6.x)
博客分类: linux 今有一小型项目,完全自主弄,原来以为很简单的NTP服务,我给折腾了2个多小时才整撑头(以前都是运维搞,没太注意,所以这技术的东西,在简单都需要亲尝啊),这里记录为以后别再浪 ...
- wordpress学习二:源码目录结构和启动流程
wordpress安装后的文件目录如下: 其中的主要目录和文件用途介绍如下: wp-admin:用于进行博客后台设置的功能目录 wp-content: wordpress的 主题,插件和本地化的存储目 ...
- UML系列02之UML类图(1)
类图介绍 类图,是UML(统一建模语言)中用于描述"类"以及"类与类"之间关系的示意图.它形象的描述出了系统的结构,帮助人们理解系统.类图是在"所有的 ...
- Netty4.x中文教程系列(二) Hello World !
在中国程序界.我们都是学着Hello World !慢慢成长起来的.逐渐从一无所知到熟悉精通的. 第二章就从Hello World 开始讲述Netty的中文教程. 首先创建一个Java项目.引入一个N ...