原题地址: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的更多相关文章

  1. POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)

    题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...

  2. POJ 1944:Fiber Communications

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4236   Accepted: 1 ...

  3. [USACO2002][poj1944]Fiber Communications(枚举)

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3804   Accepted: 1 ...

  4. POJ1944 Fiber Communications (USACO 2002 February)

    Fiber Communications 总时间限制:  1000ms 内存限制:  65536kB 描述 Farmer John wants to connect his N (1 <= N ...

  5. TOJ1550: Fiber Communications

    1550: Fiber Communications  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal ...

  6. POJ 2579 Fiber Network(状态压缩+Floyd)

    Fiber Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3328   Accepted: 1532 Des ...

  7. usaco 2002 月赛 Fiber Communications 题解

    Description Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a ...

  8. POJ 2570 Fiber Network(最短路 二进制处理)

    题目翻译 一些公司决定搭建一个更快的网络.称为"光纤网". 他们已经在全世界建立了很多网站.这 些网站的作用类似于路由器.不幸的是,这些公司在关于网站之间的接线问题上存在争论,这样 ...

  9. ZOJ 1967 POJ 2570 Fiber Network

    枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include< ...

随机推荐

  1. HTML页面加载和解析流程详细介绍

    浏览器加载和渲染html的顺序 1. IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的. 2. 在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(并不是说所有相关联的元 ...

  2. Hbase的连接池--HTablePool被Deprecated之后

      说明: 最近两天在调研HBase的连接池,有了一些收获,特此记录下来. 本文先将官方文档(http://hbase.apache.org/book.html)9.3.1.1节翻译,方便大家阅读,然 ...

  3. 您可能不知道的ASP.Net小技巧

    <!-- 页码和简介 --> 1.  在提交页面之后,保持滚动条的位置 可以在page指令上加上MaintainScrollPositionOnPostback指令 <%@ Page ...

  4. javascript 关于函数的返回值

    在javascript中根据调用方式的不同返回的内容也不同 1.  以函数的形式调用 当以函数的形式调用时, 返回值和函数定义时的 ruturn 有关, return的是数字就number类型, re ...

  5. Unity上使用Linq To XML

    using UnityEngine; using System.Collections; using System.Linq; using System.Xml.Linq; using System; ...

  6. Grid分组汇总

    Ext.onReady(function () {                Ext.define('personInfo', {                    extend: 'Ext. ...

  7. 用ScriptEngine在java中和javascript交互的例子(JDK6新特性)

    package demo7; import java.util.Arrays; import java.util.List; import javax.script.Invocable; import ...

  8. [STL]heap和priority_queue

    一.heap 在STL中,priority_queue(优先权队列)的底层机制是最大堆,因此有必要先来了解一下heap.heap采用完全二叉树的结构,当然不是真正的binary tree,因为对于完全 ...

  9. 学习android的博客

    http://www.cnblogs.com/hll2008/http://svn.apache.org/repos/asf/http://blog.csdn.net/chenzheng_javaht ...

  10. linq 常用语句

    自己练习的 switch (productDataAnalysisQuery.DataType) { : var data = (from hp in GPEcontext.hbl_product j ...