Description

Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a new fiber-optic network. However, the barns are located in a circle around the edge of a large pond, so he can only connect pairs of adjacent barns. The circular configuration
means that barn N is adjacent to barn 1. 



FJ doesn't need to connect all the barns, though, since only certain pairs of cows wish to communicate with each other. He wants to construct as few 

connections as possible while still enabling all of these pairs to communicate through the network. Given the list of barns that wish to communicate with each other, determine the minimum number of lines that must be laid. To communicate from barn 1 to barn
3, lines must be laid from barn 1 to barn 2 and also from barn 2 to barn 3(or just from barn 3 to 1,if n=3).

Input

* Line 1: Two integers, N and P (the number of communication pairs, 1 <= P <= 10,000) 



* Lines 2..P+1: two integers describing a pair of barns between which communication is desired. No pair is duplicated in the list. 

Output

One line with a single integer which is the minimum number of direct connections FJ needs to make. 

这道题。。。哎,说多了都是泪啊!这道题大意是:有n个谷仓环状连在一起,只有相邻两个之间可以铺一条路。给出p个要求,每个要求x,y表示x和y想联系。当然,因为环形,可以双向铺路。求最少铺多少路。



        一看以为是二分图,然后发现自己不会。。。

一看他人的简要题解,发现思路很简单:首先我们假设只是直线式的,那么像“火烧赤壁”那样的线段树可以轻松解决。然后我们就枚举断点,把效率乘上一个n就行了。

       自己一琢磨,不对啊!因为是枚举断点,所以每个要求可能x和y会变化(倘若我们像破碎的项链把它展开的话)。这样,每次枚举后,就要重新快排,而且之前要p的效率把a拷贝和处理一下。这样,效率是O(n*p*log(p)),看来要爆了。正在吭哧吭哧写代码,晨阳发现一个直接暴力桶排染色的程序,它竟然A了!它的最坏效率应该是O(n^2*p)!!!!因为我那个相对优化的程序比较麻烦,我就毅然决定也写暴力的,关键是常数要压的小一点。

      附注:我们在poj开了个小号:JSBLHYSYC。那里各种的编译器真是令人眼花缭乱。终于,我在C++的编译器中过了!!(以前我都是用G++的)

代码(要求1000ms,压着就969ms过去了)
#include<stdio.h>
#include<cstring>
using namespace std;
struct arr{long x,y;}a[10001];
char f[2001];
long start,end,xx,yy,i,j,k,min,n,p,t;
inline long c(long x)
{
  if (x<start) return x+n;if (x>end) return x-n;
  return x;
}
int main()
{
  scanf("%ld %ld",&n,&p);
  for (i=1;i<=p;i++)
    {
      scanf("%ld %ld",&a[i].x,&a[i].y);
      if (a[i].x>a[i].y) {t=a[i].x;a[i].x=a[i].y;a[i].y=t;}
    }
  long ans=n;
  for (i=1;i<=n;i++)
  {
    start=i;end=start+n-1;min=0;
    memset(f,0,sizeof(f));
    for (j=1;j<=p;j++)
    {
      xx=c(a[j].x);yy=c(a[j].y);
      if (xx>yy) {t=xx;xx=yy;yy=t;}
      for (k=xx;k<yy;k++) f[k]=1;
    }
    for (j=start;j<=end;j++) if (f[j]==1) min++;
    if (min<ans) ans=min;
  }
  printf("%ld",ans);
  //scanf("%ld %ld",&n,&p);
  return 0;
}

usaco 2002 月赛 Fiber Communications 题解的更多相关文章

  1. usaco 2002 月赛 Chores 题解

    Description Farmer John's family pitches in with the chores during milking, doing all the chores as ...

  2. usaco 2008 月赛 lites 开关灯 题解

      题目:     Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是 牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每一 ...

  3. POJ1944 Fiber Communications (USACO 2002 February)

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

  4. TOJ1550: Fiber Communications

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

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

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

  6. POJ 1944:Fiber Communications

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

  7. USACO全部月赛及GateWay数据

    月赛: 以07年open为例,网站如下 http://contest.usaco.org/OPEN07 其他的格式是http://contest.usaco.org/月份(月份的英文前三位,比如1月是 ...

  8. bzoj usaco 金组水题题解(1)

    UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...

  9. ZOJ Monthly, June 2014 月赛BCDEFGH题题解

    比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不 ...

随机推荐

  1. DES加密例子

    Java密码学结构设计遵循两个原则: 1) 算法的独立性和可靠性. 2) 实现的独立性和相互作用性. 算法的独立性是通过定义密码服务类来获得.用户只需了解密码算法的概念,而不用去关心如何实现这些概念. ...

  2. Hibernate与Jpa的关系(1)

    [转自:http://freewind.me/blog/20111129/588.html ] 我知道Jpa是一种规范,而Hibernate是它的一种实现.除了Hibernate,还有EclipseL ...

  3. C# DataTable转换成实体列表 与 实体列表转换成DataTable

    /// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T"&g ...

  4. css中那些容易被我们程序猿所忽略的选择器

    css中那些容易被我们程序猿所忽略的选择器 作为一个程序猿,我们可能会遇到这样的问题,假如:有5个li,要求给第三个li设置背景颜色怎么办?有人会说,用JS啊,循环遍历出第三个不就行了.但是,用JS解 ...

  5. 关于log4.net 错误,求解

    1.上结果 能生成文件 ,但是文件中无内容 2.配置文件 <configSections> <section name="log4net" type=" ...

  6. Linux_Ununtu 16.04 的下载安装并部署.Net Core 网站

    第一次接触Linux也难免有些懵逼,因为公司项目必须用.Net Core 开发一个后端服务应用:第一次用Linux给我的感觉就像在用2000年的手机一样:没用智能的操作:让人崩溃的用户体验.说多了都是 ...

  7. influxdb + Grafana可视化监控平台

    在centos6.5上influxdb + Grafana监控平台配置: 1.RedHat and CentOS users can install the latest stable version ...

  8. Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格

    1.jquery取复选框的值 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  9. VSTO在幻灯片里面添加按钮对象

    //添加Form窗体,窗体中添加Image控件,单击弹出"PPT"信息提示 //命名引用:using MF = Microsoft.Vbe.Interop.Forms; priva ...

  10. GIS与水文分析(1)GIS与水文学

    GIS与水文分析(1)GIS与水文学 对于大部分GIS从业人员或者利用GIS作为研究方向的人员来说,水文学过于专业,更偏重于理论化,很难从GIS的角度来模拟和分析水文的过程.这其实是个普遍性的问题,任 ...